]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: don't generate algs for [0]T and [1]T
authorJosh Bleecher Snyder <josharian@gmail.com>
Mon, 4 May 2015 22:02:09 +0000 (15:02 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Sun, 21 Feb 2016 18:13:55 +0000 (18:13 +0000)
All [0]T values are equal.
[1]T values are equal iff their sole components are.

This types show up most frequently as a by-product of variadic
function calls, such as fmt.Printf("abc") or fmt.Printf("%v", x).

Cuts 12k off cmd/go and 22k off golang.org/x/tools/cmd/godoc, approx 0.1% each.

For #6853 and #9930

Change-Id: Ic9b7aeb8cc945804246340f6f5e67bbf6008773e
Reviewed-on: https://go-review.googlesource.com/19766
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/subr.go
src/cmd/compile/internal/gc/walk.go

index 0d25ddf2af9cd8904a67dafebbe6f1899a7161c5..a17d7df60d64b989c356d2a8f048d56ade75cc50 100644 (file)
@@ -465,6 +465,15 @@ func algtype1(t *Type, bad **Type) int {
                        return a
                }
 
+               switch t.Bound {
+               case 0:
+                       // We checked above that the element type is comparable.
+                       return AMEM
+               case 1:
+                       // Single-element array is same as its lone element.
+                       return a
+               }
+
                return -1 // needs special compare
 
        case TSTRUCT:
index e0083175620a1ff05ee2d85e1166e94b825edb7e..f324d5e00f1cda05a763854e5d7fe2215883bf1b 100644 (file)
@@ -3193,6 +3193,21 @@ func walkcompare(np **Node, init **NodeList) {
                return
        }
 
+       if t.Etype == TARRAY {
+               // Zero- or single-element array, of any type.
+               switch t.Bound {
+               case 0:
+                       finishcompare(np, n, Nodbool(n.Op == OEQ), init)
+                       return
+               case 1:
+                       l0 := Nod(OINDEX, l, Nodintconst(0))
+                       r0 := Nod(OINDEX, r, Nodintconst(0))
+                       a := Nod(n.Op, l0, r0)
+                       finishcompare(np, n, a, init)
+                       return
+               }
+       }
+
        if t.Etype == TSTRUCT && countfield(t) <= 4 {
                // Struct of four or fewer fields.
                // Inline comparisons.