-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
A subtask of #63352.
At the moment, inliner does not consider type conversions as duplicable operations.
For example, inlining a function f
func f(a int) {
print(a, a)
}
func main() {
var a int8 = 1
f(int(a))
}generates this code:
func main() {
var a int8 = 1
var s int = int(a)
print(s, s)
}Instead, inliner should consider some conversions (those that do not change semantics or involve additional allocations) to be duplicable:
func main() {
var a int8 = 1
print(int(s), int(s))
}One way to implement this is by changing duplicable function to cover the type conversion cases, which are parsed as *ast.CallExpr with the Fun field being one of *ast.Ident, *ast.ArrayType, *ast.StructType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, or *ast.ChanType.
As per spec, string([]byte) and []byte(string) conversions may involve additional allocations and should not be considered duplicable:
Specific rules apply to (non-constant) conversions between numeric types or to and from a string type. These conversions may change the representation of x and incur a run-time cost. All other conversions only change the type but not the representation of x.