mirror of
https://github.com/golang/go
synced 2024-09-04 23:44:16 +00:00
[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>
This commit is contained in:
parent
372b312735
commit
706c580ee1
|
@ -6,7 +6,7 @@ package typecheck
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"sync"
|
||||||
|
|
||||||
"cmd/compile/internal/base"
|
"cmd/compile/internal/base"
|
||||||
"cmd/compile/internal/ir"
|
"cmd/compile/internal/ir"
|
||||||
|
@ -430,15 +430,30 @@ func TempAt(pos src.XPos, curfn *ir.Func, t *types.Type) *ir.Name {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
autotmpnamesmu sync.Mutex
|
||||||
|
autotmpnames []string
|
||||||
|
)
|
||||||
|
|
||||||
// autotmpname returns the name for an autotmp variable numbered n.
|
// autotmpname returns the name for an autotmp variable numbered n.
|
||||||
func autotmpname(n int) string {
|
func autotmpname(n int) string {
|
||||||
// Give each tmp a different name so that they can be registerized.
|
autotmpnamesmu.Lock()
|
||||||
// Add a preceding . to avoid clashing with legal names.
|
defer autotmpnamesmu.Unlock()
|
||||||
const prefix = ".autotmp_"
|
|
||||||
// Start with a buffer big enough to hold a large n.
|
// Grow autotmpnames, if needed.
|
||||||
b := []byte(prefix + " ")[:len(prefix)]
|
if n >= len(autotmpnames) {
|
||||||
b = strconv.AppendInt(b, int64(n), 10)
|
autotmpnames = append(autotmpnames, make([]string, n+1-len(autotmpnames))...)
|
||||||
return types.InternString(b)
|
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.
|
// f is method type, with receiver.
|
||||||
|
|
Loading…
Reference in a new issue