Skip to content

perf(rust): the object heap in fixed chunks, and arenas that move rather than realloc - #86

Merged
kommander merged 2 commits into
devfrom
perf/stack-03-heap-chunks
Nov 26, 2026
Merged

kommander merged 2 commits into
devfrom
perf/stack-03-heap-chunks

Conversation

@hearnadam

Copy link
Copy Markdown
Contributor

macOS does not reuse a large block that realloc grew once it is freed (20 lines
of plain Rust show it), so a process running one large program after another
kept every heap it ever grew: a 200k-object program peaked at 498 MB after 12
runs in one process, where QuickJS holds 78 MB. Objects now live in chunks of
4,096 slots, each allocated whole; the first starts small for the many runs
that make few. Cell and string arenas past 4,096 slots move to a new
allocation of twice the size instead of growing in place, and smaller ones grow
by a quarter.

12 runs in one process, peak RSS: 200k objects 498 to 77 MB, JSON round-trip
84 to 14 MB, 100k closures 114 to 30 MB.
Cost: heap[id] is two indexed loads, 1 to 5% more instructions on the bench
programs. Array elements and Map entries are still vectors realloc grows.


Part 3 of 11 in a stack; based on perf/stack-02-image. Each part was built, linted and tested on its own (cargo test --release --workspace, the same under PSYCHOSCRIPT_GC_STRESS=1, and the conformance cases under PSYCHOSCRIPT_ROUNDTRIP=1). Numbers are instructions retired, from rust/bench/examples/corpus and mem_probe (part 1).

@hearnadam
hearnadam added this pull request to stack #95 November 26, 2026 17:17
@hearnadam
hearnadam force-pushed the perf/stack-03-heap-chunks branch from 8144288 to 322cd2d Compare November 26, 2026 17:20
Base automatically changed from perf/stack-02-image to dev November 26, 2026 22:45
…her than realloc

macOS does not reuse a large block that realloc grew once it is freed (20 lines
of plain Rust show it), so a process running one large program after another
kept every heap it ever grew: a 200k-object program peaked at 498 MB after 12
runs in one process, where QuickJS holds 78 MB. Objects now live in chunks of
4,096 slots, each allocated whole; the first starts small for the many runs
that make few. Cell and string arenas past 4,096 slots move to a new
allocation of twice the size instead of growing in place, and smaller ones grow
by a quarter.

12 runs in one process, peak RSS: 200k objects 498 to 77 MB, JSON round-trip
84 to 14 MB, 100k closures 114 to 30 MB.
Cost: heap[id] is two indexed loads, 1 to 5% more instructions on the bench
programs. Array elements and Map entries are still vectors realloc grows.
@kommander
kommander force-pushed the perf/stack-03-heap-chunks branch from 322cd2d to 703d4d6 Compare November 26, 2026 22:45
…it value_depth's margin again

Inlined into every recursive conversion in debug builds, the chunked index made each level's frame larger: at
depth 200 a host call's argument needed 541,567 B of a debug build's stack, past the 512 KiB (half the mobile
live-run thread) that every_conversion_at_depth_200_fits_half_of_it allows. Not forced inline in debug builds, it
needs 508,807 B, as before the chunks. Release builds are unchanged.
@kommander
kommander merged commit 0b74548 into dev Nov 26, 2026
1 check passed
@kommander
kommander deleted the perf/stack-03-heap-chunks branch November 26, 2026 22:49
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.

2 participants