Skip to content

Runtime: empty small-object slabs go back to the system (#685 tier 1) - #3395

Merged
ehartford merged 3 commits into
mainfrom
slab-release
Nov 17, 2026
Merged

ehartford merged 3 commits into
mainfrom
slab-release

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

A freed small block joined its class's freelist and its slab stayed mapped for the life of the process: a program's small-object memory was its high-water mark (#685).

What changes. A slab whose block map has no bit set holds no live block, so every block carved from it is on a freelist. A sweep finds those slabs, unlinks their blocks from the freelists and unmaps them (rt_release_empty_slabs, rt/rt_core.w). The hot path pays one counter: the sweep runs when the free blocks have doubled since the last one (plus a floor of 32,768), so its walk is paid by the frees that grew the lists, and a program whose free blocks hold steady never sweeps. The slab being carved is never released. The page index keeps a released slab's keys with no start and drops them at its next rebuild, which is now sized by live slabs. wasm keeps its slabs (linear memory never shrinks and its unmap recycler is bounded): each platform states rt_unmap_returns_pages. WITH_ALLOC_NO_REUSE never sweeps.

Measured (darwin-aarch64).

before after
behav_685_empty_slabs_released.w (200,000 48-byte blocks, freed, twice) assertion fails: 0 bytes returned ok: over 8 MB returned each round
synthetic: committed at exit after 40 such rounds 19.7 MB 6.6 MB
with check src/main.w wall 51.5, 51.8 s 51.8, 51.7 s
same: peak RSS 869 MB 873 MB
30 M alloc/free pairs 518–616 ms 523–529 ms
the 40 rounds themselves 164 ms 206 ms

The last row is the cost: a program that allocates and frees the same 13 MB over and over pays about 1 ms a round to map it again. The compiler's peak does not move, as expected: this returns memory after a peak, it does not lower one. That is tier 2 of the issue (phase-scoped arenas), which stays open.

A first version kept a live count in each slab and cost a self-check 2%; this one reads the block maps the allocator already keeps.

Battery GREEN on c34c123; :release-checks green.

Part of #685 (tier 1).

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