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: eliminate memory allocation for non-int n in slice extension idiom #29785
Comments
I'm curious - what happens with other integer types like |
cc @martisch |
This was a known limitation when writing the compiler optimization and there is a todo in the walk.go code to support more types. Adding all types which values fit in an int shouldnt be hard. For types exceeding the value range of int we need additional runtime checks. E.g. if uint64 seems worth supporting even if only for consistency we can add an extra check to throw the same panic as eg makeslice64 does. I have a look at it once the tree opens for go1.13. |
Change https://golang.org/cl/182559 mentions this issue: |
golang.org/cl/109517 optimized the compiler to avoid the allocation for make in append(x, make([]T, y)...). This was only implemented for the case that y has type int. This change extends the optimization to trigger for all integer types where the value is known at compile time to fit into an int. name old time/op new time/op delta ExtendInt-12 106ns ± 4% 106ns ± 0% ~ (p=0.351 n=10+6) ExtendUint64-12 1.03µs ± 5% 0.10µs ± 4% -90.01% (p=0.000 n=9+10) name old alloc/op new alloc/op delta ExtendInt-12 0.00B 0.00B ~ (all equal) ExtendUint64-12 13.6kB ± 0% 0.0kB -100.00% (p=0.000 n=10+10) name old allocs/op new allocs/op delta ExtendInt-12 0.00 0.00 ~ (all equal) ExtendUint64-12 1.00 ± 0% 0.00 -100.00% (p=0.000 n=10+10) Updates #29785 Change-Id: Ief7760097c285abd591712da98c5b02bc3961fcd Reviewed-on: https://go-review.googlesource.com/c/go/+/182559 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
https://golang.org/cl/182559 fixes the provided benchmarks. |
The slice extension idiom from #21266 doesn't work in go tip for non-int
n
inappend(a, make([]T, n)...)
:Benchmark results:
As you can see, if
n
has a type other thanint
, go doesn't remove the allocation.The text was updated successfully, but these errors were encountered: