Commit e64764c8 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/internal/gc: cache commonly used Ptrto types

Reduces allocations in the compiler by ~1.5%.

No functional changes. Passes toolstash -cmp.

Change-Id: I2416f7fb0aaf9b7d6783c79e840039ad8fa7b5a3
Reviewed-on: https://go-review.googlesource.com/9419Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 2385f692
...@@ -1546,10 +1546,25 @@ func typehash(t *Type) uint32 { ...@@ -1546,10 +1546,25 @@ func typehash(t *Type) uint32 {
return binary.LittleEndian.Uint32(h[:4]) return binary.LittleEndian.Uint32(h[:4])
} }
func Ptrto(t *Type) *Type { var initPtrtoDone bool
if Tptr == 0 {
Fatal("ptrto: no tptr") var (
} ptrToUint8 *Type
ptrToAny *Type
ptrToString *Type
ptrToBool *Type
ptrToInt32 *Type
)
func initPtrto() {
ptrToUint8 = ptrto1(Types[TUINT8])
ptrToAny = ptrto1(Types[TANY])
ptrToString = ptrto1(Types[TSTRING])
ptrToBool = ptrto1(Types[TBOOL])
ptrToInt32 = ptrto1(Types[TINT32])
}
func ptrto1(t *Type) *Type {
t1 := typ(Tptr) t1 := typ(Tptr)
t1.Type = t t1.Type = t
t1.Width = int64(Widthptr) t1.Width = int64(Widthptr)
...@@ -1557,6 +1572,32 @@ func Ptrto(t *Type) *Type { ...@@ -1557,6 +1572,32 @@ func Ptrto(t *Type) *Type {
return t1 return t1
} }
// Ptrto returns the Type *t.
// The returned struct must not be modified.
func Ptrto(t *Type) *Type {
if Tptr == 0 {
Fatal("ptrto: no tptr")
}
// Reduce allocations by pre-creating common cases.
if !initPtrtoDone {
initPtrto()
initPtrtoDone = true
}
switch t {
case Types[TUINT8]:
return ptrToUint8
case Types[TINT32]:
return ptrToInt32
case Types[TANY]:
return ptrToAny
case Types[TSTRING]:
return ptrToString
case Types[TBOOL]:
return ptrToBool
}
return ptrto1(t)
}
func frame(context int) { func frame(context int) {
var l *NodeList var l *NodeList
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment