Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
runtime: unnecessary heap allocations when `range`ing over an empty array #39668
What version of Go are you using (
This is more or less intentional. We want that allocation to happen outside the loop, so it only happens once even if the loop has many iterations. Of course, that's pessimistic if the loop has 0 iterations.
We could condition the allocation by checking the number if iterations first. Offhand I suspect this would be fairly hard to do, just because it requires some form of loop peeling. But it would be nice to handle this case.