mirror of
https://github.com/golang/go
synced 2024-11-17 14:14:56 -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:
parent
d39685e5e9
commit
e48d7d3b21
@ -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) {
|
||||
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
|
||||
}
|
||||
|
||||
switch y.(type) {
|
||||
case intVal:
|
||||
switch y.(type) {
|
||||
case intVal:
|
||||
return x, y
|
||||
case ratVal:
|
||||
return itor(x1), y
|
||||
case floatVal:
|
||||
return itof(x1), y
|
||||
case complexVal:
|
||||
return vtoc(x1), y
|
||||
switch x1 := x.(type) {
|
||||
case int64Val:
|
||||
return i64toi(x1), y
|
||||
}
|
||||
|
||||
case ratVal:
|
||||
switch y.(type) {
|
||||
case ratVal:
|
||||
return x, y
|
||||
case floatVal:
|
||||
return rtof(x1), y
|
||||
case complexVal:
|
||||
return vtoc(x1), y
|
||||
switch x1 := x.(type) {
|
||||
case int64Val:
|
||||
return i64tor(x1), y
|
||||
case intVal:
|
||||
return itor(x1), y
|
||||
}
|
||||
|
||||
case floatVal:
|
||||
switch y.(type) {
|
||||
case floatVal:
|
||||
return x, y
|
||||
case complexVal:
|
||||
return vtoc(x1), y
|
||||
switch x1 := x.(type) {
|
||||
case int64Val:
|
||||
return i64tof(x1), y
|
||||
case intVal:
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user