1
0
mirror of https://github.com/golang/go synced 2024-11-11 20:01:37 -07:00

[dev.regabi] go/constant: avoid heap allocations in match

When type switching from interface{} to T, and then returning the T as
interface{} again, it's better to return the original interface{}
value. This avoids needing to heap allocate the T for
non-pointer-shaped types (i.e., int64Val, complexVal, stringVal).

Change-Id: I25c83b3f9ec9bd2ffeec5a65279b68f4fcef8a19
Reviewed-on: https://go-review.googlesource.com/c/go/+/272647
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Matthew Dempsky 2020-11-23 04:28:25 -08:00
parent 1abb12fc97
commit 96f3fb7244

View File

@ -1023,52 +1023,55 @@ func match(x, y Value) (_, _ Value) {
}
// ord(x) <= ord(y)
switch x := x.(type) {
// 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 := y.(type) {
switch y.(type) {
case int64Val:
return x, y
case intVal:
return i64toi(x), y
return i64toi(x1), y
case ratVal:
return i64tor(x), y
return i64tor(x1), y
case floatVal:
return i64tof(x), y
return i64tof(x1), y
case complexVal:
return vtoc(x), y
return vtoc(x1), y
}
case intVal:
switch y := y.(type) {
switch y.(type) {
case intVal:
return x, y
case ratVal:
return itor(x), y
return itor(x1), y
case floatVal:
return itof(x), y
return itof(x1), y
case complexVal:
return vtoc(x), y
return vtoc(x1), y
}
case ratVal:
switch y := y.(type) {
switch y.(type) {
case ratVal:
return x, y
case floatVal:
return rtof(x), y
return rtof(x1), y
case complexVal:
return vtoc(x), y
return vtoc(x1), y
}
case floatVal:
switch y := y.(type) {
switch y.(type) {
case floatVal:
return x, y
case complexVal:
return vtoc(x), y
return vtoc(x1), y
}
}