Skip to content

Reduce allocations in builtin range - #483

Open
vdpoora wants to merge 1 commit into
d5:masterfrom
vdpoora:pr/reduce-range-allocations
Open

vdpoora wants to merge 1 commit into
d5:masterfrom
vdpoora:pr/reduce-range-allocations

Conversation

@vdpoora

@vdpoora vdpoora commented Aug 19, 2026

Copy link
Copy Markdown

range() builds its result array with one []Int backing slice and a
presized []Object, so construction takes a small constant number of
allocations regardless of length, instead of one boxed Int per element plus
repeated 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 one
range at ~240 MB), returning ErrRangeLimit instead of a makeslice panic on an
oversized range.

Benchmarks

go test -bench . -benchmem, measured per Compiled.Run, this branch vs master:

scenario master this PR
x := range(0, 1000000) 1000041 allocs / 96 MB 5 / 24 MB
for i in range(0, 100000) { s += i } 200031 / 10.6 MB 100005 / 3.3 MB
for _ in range(0, 1000000) {} 1000042 / 96 MB 6 / 24 MB

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.
  • The overflow-safe count also fixes wrong results for wide ranges, e.g.
    range(0, 10, 9223372036854775806) now yields [0] instead of an empty
    result.
  • The MaxRangeLen cap is a behavior change at extreme sizes (an oversized
    range() returns an error instead of OOM/panic). Happy to split it into its
    own commit if you'd prefer to review it separately.

@vdpoora
vdpoora force-pushed the pr/reduce-range-allocations branch from 30dbe84 to e1c0709 Compare August 19, 2026 14:45
@vdpoora
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
vdpoora force-pushed the pr/reduce-range-allocations branch from e1c0709 to 5c44cda Compare August 20, 2026 10:42
@vdpoora
vdpoora marked this pull request as draft September 8, 2026 12:55
@vdpoora
vdpoora marked this pull request as ready for review September 8, 2026 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant