runtime: unnecessary heap allocations when range
ing over an empty array
#39668
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Performance
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes (1.15beta1)
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
In an example like the following,
thing
is created on the heap even if the passedarr
has length 0 and the loop body is never run:This is contrived/minimalistic example (inlining is just to make the asm easier to read), but shows the point. This originally came up in a case where
arr
was generated in an event processing critical path, and this iteration over a (usually empty array) was creating millions of objects that escaped to heap (due to storage in another object, not assignment to a global), eating ~10% of run time.What did you expect to see?
I would expect that the
runtime.newobject
call would only happen after the array is checked for length > 0.What did you see instead?
The text was updated successfully, but these errors were encountered: