-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: don't allocate when putting constant strings in an interface #18704
Comments
Oh, and for those that are curious:
|
The regular function is 235 bytes of code. Prealloc is 188 bytes of code. |
Might even be worth doing this for all static data, including ints, floats, etc. |
Threw together a prototype for strings only. Not ready to be mailed; lots of sinit.go needs to be updated for better handling of interface values for a complete CL, and it's a fair amount of careful work. But it was enough to get some numbers to convince myself that this will help. For the go-kit logging benchmarks:
fmt benchmarks also show some improvements. CPU benchmarks omitted because they are too noisy--the fmt CPU benchmarks are super sensitive to alignment, etc.
|
Found a simpler and broader (and thus higher risk) implementation. CL 35554 |
CL https://golang.org/cl/35554 mentions this issue. |
fmt.Println("abc")
allocates when it converts"abc"
into an interface. It should not.When the compiler sees a constant string being converted into an interface (usually at a call site?), it could create a static interface value to use, rather than allocating it at runtime.
That is, do the following conversion automatically:
This might not be as bad for binary size as it initially appears; it might even be an improvement. The cost of the runtime conversion call is probably about as large as the two additional words of static data plus symbol overhead. (And that symbol overhead could be removed if necessary, as we did for strings.)
See also #17725 and the golang-dev thread about logging that insired this.
cc @randall77 @mdempsky @bradfitz
The text was updated successfully, but these errors were encountered: