Skip to content

InternPool and AstPool are counted handles: a clone is another holder, the last drop frees (#2449) - #2453

Merged
ehartford merged 1 commit into
mainfrom
pool-owners
Oct 18, 2026
Merged

ehartford merged 1 commit into
mainfrom
pool-owners

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #2449.

The defect. InternPool and AstPool were impl Copy handles over raw with_alloc blocks with no Drop and no owner. Every InternPool.init() leaked a 1 MB arena page and its tables by construction, every AstPool.new() its node tables, and the language could not see it. The LSP made a pool per reparse: peak RSS 45 MB after 40 edits, 126 MB after 200 (about half a megabyte per edit). The test runner made one per discovered file, Codegen three placeholders per unit.

The fix. The state block carries a holder count. clone() is another holder, dropping a handle releases, the last release drops the tables, the maps and the arena pages and frees the block. Copy is gone, so the language sees the handle: a function that only reads or interns takes &InternPool / &AstPool (428 signatures, a with run sweep), a type that keeps a handle stores a clone (Parser, Sema, MirBuilder, the evaluators, Codegen, Zcu, the LSP document), a pass that hands the pool on takes it owned. The parser holds the caller's intern pool as a further holder, so the x = parser.intern hand-backs are gone; the backend seams give codegen clones instead of lending and restoring; take_pool/take_intern and the foundation duplicate src/compiler/foundation/InternPool.w (two internals tests, now on the real pool) are removed. AstPool's setters drop mut (they write through the state pointer; mut only stopped borrowed callers). The arena leaves 16 bytes at the head of every page: a str whose payload started a page read as an owned allocation to rt_payload_start_is_owned, so its copies retained and released the page itself.

Ceremony report (the census's clone-call, granted 537 → 558 in build/ceremony-allowances.tsv). pool.clone() at a type that keeps a handle is the explicit second-holder spelling, as Rc.clone is: With has no implicit alias, and today every compiler phase holds the pool. It reads as a meaning (this value outlives the caller's handle), not as something the compiler already knows. What would remove the 21: one pool per compilation with every phase borrowing it, which makes Sema and Codegen view-holding types and restructures the phase hand-offs. The clone count is the honest measure of how far that restructure is.

Pins.

  • test/debug_alloc/da_2449_pools_free_on_last_drop.w: holders, clones, interning through a clone, a page-sized string; leak count=0. The same program against main's pool: leak count=283 allocations=286, the 1 MB page among them.
  • lsp-memory-flat in cli-selfhost-lsp-tests: the new with/memory request answers the runtime's committed byte count; after 50 edits it may have grown by less than a megabyte (measured 65 KB over 40 edits, 262 KB over 200; main's server has no such request and fails the check).

Verification (pinned seed, on 263d45e): :gate green (231 s); :cli-selfhost-lsp-tests, :drop-audit, :debug-alloc-tests, :deep-debug-tool-tests, :internals-tests green on the gate's release compiler; self-host A/B against main 809561f's reseeded compiler, check src/main.w, three interleaved rounds: 0.976, 0.962, 0.973 (median 0.973: the branch is faster). Battery: running, posted when green.

Found on the way and filed: #2452 (the build graph cache survives a deleted or added source file).

…, the last drop frees (#2449)

Both pools were `impl Copy` handles over raw with_alloc blocks with no Drop
and no owner: every InternPool.init() leaked a 1 MB arena page and the
symbol tables by construction, every AstPool.new() its node tables. The
LSP made a pool per reparse (peak RSS grew about half a megabyte per edit:
45 MB after 40 edits, 126 MB after 200), the test runner one per
discovered file, Codegen three placeholders per unit.

Now the state block carries a holder count. `clone()` is another holder,
dropping a handle releases, and the release that finds no other holder
drops the tables, the maps and the arena's pages and frees the block.
Copy is gone, so the language sees the handle: a function that only reads
or interns takes `&InternPool` / `&AstPool` (428 signatures), a type that
keeps a handle stores a clone (Parser, Sema, MirBuilder, the evaluators,
Codegen, Zcu, the LSP document), and a pass that hands the pool on takes
it owned. The parser holds the caller's intern pool as a further holder,
so the `x = parser.intern` hand-backs are gone; the backend seams give
codegen clones instead of lending and restoring; take_pool/take_intern
and the foundation duplicate (src/compiler/foundation/InternPool.w, used by
two internals tests, now on the real pool) are removed. AstPool's setters
drop `mut`: they write through the state pointer, and `mut` only stopped
borrowed callers. The arena leaves 16 bytes at the head of every page: a
str whose payload started a page read as an owned allocation to
rt_payload_start_is_owned, and its copies retained and released the page.

Pins: test/debug_alloc/da_2449_pools_free_on_last_drop.w (holders, clones,
a page-sized string, leak count=0; main's pool leaks 283 of 286 blocks on
the same program) and the LSP self-host check `lsp-memory-flat`: the new
`with/memory` request answers the runtime's committed byte count, and 50
edits may grow it by less than a megabyte (measured 65 KB over 40 edits,
262 KB over 200).
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on 263d45e under the pinned seed: 333 s wall (out/battery/status.txt: gate, uat, battery-checks all green).

@ehartford
ehartford marked this pull request as ready for review October 18, 2026 16:56
@ehartford
ehartford merged commit c98c841 into main Oct 18, 2026
@ehartford
ehartford deleted the pool-owners branch October 18, 2026 16:56
@ehartford

Copy link
Copy Markdown
Contributor Author

Main's combined tree (c98c841 = #2451 + this PR; the branch was cut before #2451 landed) ran its own battery in the reseed worktree: GREEN, 520 s wall. Installed.

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.

InternPool and AstPool are Copy handles over raw allocations that nothing frees: every init leaks (the LSP leaks 1 MB per reparse)

1 participant