1
0
mirror of https://github.com/golang/go synced 2024-11-17 16:54:44 -07:00

[dev.typeparams] go/constant: faster match implementation

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>
This commit is contained in:
Robert Griesemer 2021-01-25 18:04:55 -08:00
parent d39685e5e9
commit e48d7d3b21

View File

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