]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] go/constant: faster match implementation
authorRobert Griesemer <gri@golang.org>
Tue, 26 Jan 2021 02:04:55 +0000 (18:04 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 26 Jan 2021 05:40:44 +0000 (05:40 +0000)
Shortcut matching code if both operands have the same representation.

Change-Id: I9433455acb5b9d0b88d3c81eb1b3b0ae258193e7
Reviewed-on: https://go-review.googlesource.com/c/go/+/286654
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/go/constant/value.go

index 8ee762060931e4ff07fd9e75213fa46cb8837367..78cb3f896f535cd7fddd64a2f2b34e03f1ec81dd 100644 (file)
@@ -1035,62 +1035,45 @@ func ord(x Value) int {
 // or invalid (say, nil) both results are that value.
 //
 func match(x, y Value) (_, _ Value) {
-       if ord(x) > ord(y) {
-               y, x = match(y, x)
-               return x, y
+       switch ox, oy := ord(x), ord(y); {
+       case ox < oy:
+               x, y = match0(x, y)
+       case ox > oy:
+               y, x = match0(y, x)
        }
-       // ord(x) <= ord(y)
+       return x, y
+}
 
+// match0 must only be called by match.
+// Invariant: ord(x) < ord(y)
+func match0(x, y Value) (_, _ Value) {
        // Prefer to return the original x and y arguments when possible,
        // to avoid unnecessary heap allocations.
 
-       switch x1 := x.(type) {
-       case boolVal, *stringVal, complexVal:
-               return x, y
-
-       case int64Val:
-               switch y.(type) {
+       switch y.(type) {
+       case intVal:
+               switch x1 := x.(type) {
                case int64Val:
-                       return x, y
-               case intVal:
                        return i64toi(x1), y
-               case ratVal:
-                       return i64tor(x1), y
-               case floatVal:
-                       return i64tof(x1), y
-               case complexVal:
-                       return vtoc(x1), y
                }
-
-       case intVal:
-               switch y.(type) {
+       case ratVal:
+               switch x1 := x.(type) {
+               case int64Val:
+                       return i64tor(x1), y
                case intVal:
-                       return x, y
-               case ratVal:
                        return itor(x1), y
-               case floatVal:
-                       return itof(x1), y
-               case complexVal:
-                       return vtoc(x1), y
                }
-
-       case ratVal:
-               switch y.(type) {
+       case floatVal:
+               switch x1 := x.(type) {
+               case int64Val:
+                       return i64tof(x1), y
+               case intVal:
+                       return itof(x1), y
                case ratVal:
-                       return x, y
-               case floatVal:
                        return rtof(x1), y
-               case complexVal:
-                       return vtoc(x1), y
-               }
-
-       case floatVal:
-               switch y.(type) {
-               case floatVal:
-                       return x, y
-               case complexVal:
-                       return vtoc(x1), y
                }
+       case complexVal:
+               return vtoc(x), y
        }
 
        // force unknown and invalid values into "x position" in callers of match