Conversation
vdpoora
force-pushed
the
pr/reduce-range-allocations
branch
from
August 19, 2026 14:45
30dbe84 to
e1c0709
Compare
vdpoora
marked this pull request as ready for review
August 19, 2026 14:58
Preallocate the result slice and allocate all Int elements in one backing slice, so range() builds in a constant 3 allocations instead of one per element. Compute the element count with overflow-safe math, and cap it at MaxRangeLen (returns ErrRangeLimit) so an oversized range returns an error instead of panicking the host.
vdpoora
force-pushed
the
pr/reduce-range-allocations
branch
from
August 20, 2026 10:42
e1c0709 to
5c44cda
Compare
vdpoora
marked this pull request as draft
September 8, 2026 12:55
vdpoora
marked this pull request as ready for review
September 8, 2026 12:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
range()builds its result array with one[]Intbacking slice and apresized
[]Object, so construction takes a small constant number ofallocations regardless of length, instead of one boxed
Intper element plusrepeated slice growth.
It also computes the element count with overflow-safe unsigned math, and caps
materialization at a new
MaxRangeLen(default 10,000,000; at ~24 bytes per element that bounds onerange at ~240 MB), returning
ErrRangeLimitinstead of amakeslicepanic on anoversized range.
Benchmarks
go test -bench . -benchmem, measured perCompiled.Run, this branch vsmaster:x := range(0, 1000000)for i in range(0, 100000) { s += i }for _ in range(0, 1000000) {}Construction drops from ~N allocations to a small constant at any length. The
allocations left in value-using loops are the loop-body arithmetic, not range
construction.
Notes
range()still returns a materialized array; value semantics are unchanged.range(0, 10, 9223372036854775806)now yields[0]instead of an emptyresult.
MaxRangeLencap is a behavior change at extreme sizes (an oversizedrange()returns an error instead of OOM/panic). Happy to split it into itsown commit if you'd prefer to review it separately.