Skip to content

#3103: large blocks are mapped at a power of two and cached by class when freed; a large block grows in place - #3104

Merged
ehartford merged 3 commits into
mainfrom
i3100
Nov 8, 2026
Merged

ehartford merged 3 commits into
mainfrom
i3100

Conversation

@ehartford

@ehartford ehartford commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3103.

A large allocation (payload over 4 KiB) was its own mapping, mapped and unmapped per List buffer and recorded and forgotten in the sorted range table with a shift each: about a fifth of check src/main.w by sampling (rt_range_store, rt_record_large_range, rt_forget_large_range, mmap, munmap), fed by per-scope snapshots and per-call lookups that make and drop a buffer over 4 KiB each time.

A large block is now mapped at a class size (a multiple of the 16 KiB page up to 256 KiB, a power of two up to 2 MiB above) and, when freed, kept on its class's list (32 blocks a class, 96 MiB in all) for the next allocation of its class: still mapped, still recorded, its size word 0 so it is not an owned payload (the ownership test's large branch reads the word), its payload zeroed when reused. WITH_ALLOC_NO_REUSE bypasses the cache as it does the small freelists; a reservation that would cross WITH_MEMORY_LIMIT_BYTES first gives every cached block back, so the cache is never what the limit measures (the first battery's red, behav_1944_comptime_snapshot_memory, was that). A large block that grows within its mapping grows in place.

Measured back to back on check src/main.w: installed compiler (main, no cache) 97 s, this tree's stage2 85 s; the earlier power-of-two version measured 102 s → 86 s and 93 s → 83 s under battery load.

Fixtures: test/debug_alloc/da_3103_large_block_double_free_reported.w (a cached block's second free still reaches the ledger) and test/behavior/behav_3103_large_blocks_reused.w (reused buffers come back zeroed and whole). Gate green on the first version; battery running on this one.

Eric Hartford added 3 commits November 8, 2026 12:56
…when freed; a large block grows in place within its mapping

A large allocation (payload over 4 KiB) was its own mapping, mapped and
unmapped per List buffer and recorded and forgotten in the sorted range
table with a shift each: a fifth of `check src/main.w` by sampling
(rt_range_store, rt_record_large_range, rt_forget_large_range, mmap,
munmap), fed by the per-scope snapshots and per-call lookups that make and
drop a buffer over 4 KiB each time.

A large block up to 2 MiB is now mapped at the power of two that holds it
and, when freed, kept on a per-class list (32 blocks a class, 96 MiB in
all) for the next allocation of its class: still mapped, still recorded,
its size word 0 so it is not an owned payload (the ownership test's large
branch reads the word), its payload zeroed when reused. WITH_ALLOC_NO_REUSE
bypasses the cache as it does the small freelists. A large block that
grows within its mapping grows in place, header updated and new bytes
zeroed, instead of allocate-copy-free.
…elds to the memory limit

Power-of-two mappings doubled the committed bytes of a payload just over a
power of two, and the retained cache counted against WITH_MEMORY_LIMIT_BYTES:
behav_1944_comptime_snapshot_memory (a 32 MiB limit over 64 KiB receivers)
failed in the battery. Mappings up to 256 KiB are now a multiple of the
16 KiB page (one class per multiple; the kernel commits no less), powers of
two above that; and a reservation that would cross the limit first gives
every cached block back, so the cache is never what the limit measures.
@ehartford
ehartford marked this pull request as ready for review November 8, 2026 21:41
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on f9db712: with run tools/battery.w rc=0, wall 963 s, status green (the battery's own gate run included). The first battery's red (behav_1944_comptime_snapshot_memory, a 32 MiB limit) is what the page-multiple classes and the flush-before-limit fix.

@ehartford
ehartford merged commit de7e850 into main Nov 8, 2026
@ehartford
ehartford deleted the i3100 branch November 8, 2026 21:41
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.

Large allocations (>4 KiB) are mmapped one by one and tracked in a shifting sorted array: ~20% of check src/main.w by sampling

1 participant