From: Alan Donovan Date: Mon, 15 Aug 2022 17:05:01 +0000 (-0400) Subject: go/constant: share the empty string X-Git-Tag: go1.20rc1~1637 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1f833e4a1bfd77c2dfa91305f90ade9f9ceb66de;p=gostls13.git go/constant: share the empty string This saves 11,000 allocations when loading the standard library in golang.org/x/tools/go/ssa form. Change-Id: I8aa32b0641c1a3dde29e6ee76c760006035dd56d Reviewed-on: https://go-review.googlesource.com/c/go/+/423934 TryBot-Result: Gopher Robot Reviewed-by: Cuong Manh Le Run-TryBot: Alan Donovan Run-TryBot: Robert Griesemer Reviewed-by: Robert Griesemer Auto-Submit: Alan Donovan --- diff --git a/src/go/constant/value.go b/src/go/constant/value.go index 36c29d8c27..f8d03cf375 100644 --- a/src/go/constant/value.go +++ b/src/go/constant/value.go @@ -380,7 +380,14 @@ func MakeUnknown() Value { return unknownVal{} } func MakeBool(b bool) Value { return boolVal(b) } // MakeString returns the String value for s. -func MakeString(s string) Value { return &stringVal{s: s} } +func MakeString(s string) Value { + if s == "" { + return &emptyString // common case + } + return &stringVal{s: s} +} + +var emptyString stringVal // MakeInt64 returns the Int value for x. func MakeInt64(x int64) Value { return int64Val(x) }