New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/compile: combine append calls #25828
Comments
Sounds fine if we can figure out a reasonable answer for what debugging info looks like. |
Also might be a good candidate hint for a performance-oriented vet-like tool. cc @dominikh |
Well.. https://go-critic.github.io/overview.html#appendCombine-ref |
There are some differences in observable behavior that can be demonstrated by this example: This makes me think that this optimization is not valid for compiler. Copying code here for convenience: package main
import (
"fmt"
)
func before() {
xs := make([]int, 1, 1)
xs[0] = 6
ys := xs[:0]
ys = append(ys, 1)
ys = append(ys, 2)
fmt.Println(xs, ys) // [1] [1 2]
}
func after() {
xs := make([]int, 1, 1)
xs[0] = 6
ys := xs[:0]
ys = append(ys, 1,2)
fmt.Println(xs, ys) // [6] [1 2]
}
func main() {
before()
after()
} |
Example provided by @TocarIP |
Punting to unplanned, too late for anything major in 1.12. |
Currently, compiler does not rewrite
A
intoB
and generates 2append
sequences forA
(or, more generally, N append sequences for N consecutive appends to the same slice).Synthetic benchmarks show quite significant difference between these two forms.
There are multiple spots inside Go sources that can use this kind of rewrite:
Most of them are not performance-critical.
I've done some impact measurements for real code:
Note that
AppendQuoteRune
needs to be modified in order to see difference because otherwise it does not execute path with appends:The major problem I see is potential hit to things like line info and debugging experience (one append sequence instead of extected N). I also believe that slice expression should be "safe". Arguments may also require this property to avoid panic line info changes where evaluation may lead to it.
I would like to dig into that and provide optimization implementation, as soon as it gets approved.
I've sent CL117615 - net: combine append calls in reverseaddr as it seems beneficial there. In case this optimization is never implemented inside the compiler, we'll get that boost regardless.
The text was updated successfully, but these errors were encountered: