diff options
author | Matthew Dempsky <mdempsky@google.com> | 2021-07-01 00:28:05 -0700 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2021-07-01 08:26:25 +0000 |
commit | 706c580ee1db800353752629882209ef6509a0b4 (patch) | |
tree | e6c5d770ca5023d22d9360172ac1a910b9bfe052 /src/cmd/compile/internal | |
parent | 372b31273539b988fe887d7b7cc2ed14439278e6 (diff) | |
download | go-706c580ee1db800353752629882209ef6509a0b4.tar.gz go-706c580ee1db800353752629882209ef6509a0b4.zip |
[dev.typeparams] cmd/compile: simplify autotmpname
Rather than manually formatting a byte-string and then using a map
lookup to convert it to string, we can just use a slice. This avoids
both the overhead of formatting the byte slice and the map lookup.
Change-Id: Ia7b883632ea990ce9ee848dd4b4e4cdfbd611212
Reviewed-on: https://go-review.googlesource.com/c/go/+/332191
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Diffstat (limited to 'src/cmd/compile/internal')
-rw-r--r-- | src/cmd/compile/internal/typecheck/dcl.go | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/src/cmd/compile/internal/typecheck/dcl.go b/src/cmd/compile/internal/typecheck/dcl.go index 5f8b8b3d41..f3ccbb4ac0 100644 --- a/src/cmd/compile/internal/typecheck/dcl.go +++ b/src/cmd/compile/internal/typecheck/dcl.go @@ -6,7 +6,7 @@ package typecheck import ( "fmt" - "strconv" + "sync" "cmd/compile/internal/base" "cmd/compile/internal/ir" @@ -430,15 +430,30 @@ func TempAt(pos src.XPos, curfn *ir.Func, t *types.Type) *ir.Name { return n } +var ( + autotmpnamesmu sync.Mutex + autotmpnames []string +) + // autotmpname returns the name for an autotmp variable numbered n. func autotmpname(n int) string { - // Give each tmp a different name so that they can be registerized. - // Add a preceding . to avoid clashing with legal names. - const prefix = ".autotmp_" - // Start with a buffer big enough to hold a large n. - b := []byte(prefix + " ")[:len(prefix)] - b = strconv.AppendInt(b, int64(n), 10) - return types.InternString(b) + autotmpnamesmu.Lock() + defer autotmpnamesmu.Unlock() + + // Grow autotmpnames, if needed. + if n >= len(autotmpnames) { + autotmpnames = append(autotmpnames, make([]string, n+1-len(autotmpnames))...) + autotmpnames = autotmpnames[:cap(autotmpnames)] + } + + s := autotmpnames[n] + if s == "" { + // Give each tmp a different name so that they can be registerized. + // Add a preceding . to avoid clashing with legal names. + s = fmt.Sprintf(".autotmp_%d", n) + autotmpnames[n] = s + } + return s } // f is method type, with receiver. |