]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/gc: cache commonly used Ptrto types
authorJosh Bleecher Snyder <josharian@gmail.com>
Wed, 29 Apr 2015 00:08:28 +0000 (17:08 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Wed, 29 Apr 2015 23:18:54 +0000 (23:18 +0000)
Reduces allocations in the compiler by ~1.5%.

No functional changes. Passes toolstash -cmp.

Change-Id: I2416f7fb0aaf9b7d6783c79e840039ad8fa7b5a3
Reviewed-on: https://go-review.googlesource.com/9419
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/internal/gc/subr.go

index 381079e40c5c26c28fb60c2e36b5d16696bfee00..8cc140a004657a4323bd8da26f10689c2a55954f 100644 (file)
@@ -1546,10 +1546,25 @@ func typehash(t *Type) uint32 {
        return binary.LittleEndian.Uint32(h[:4])
 }
 
-func Ptrto(t *Type) *Type {
-       if Tptr == 0 {
-               Fatal("ptrto: no tptr")
-       }
+var initPtrtoDone bool
+
+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.Type = t
        t1.Width = int64(Widthptr)
@@ -1557,6 +1572,32 @@ func Ptrto(t *Type) *Type {
        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) {
        var l *NodeList