Skip to content

rc: a spread given to a rest parameter is held across the call and given back (Array.join leak) - #924

Merged
ASDAlexander77 merged 1 commit into
mainfrom
rc-spread-pack-leak
Nov 18, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
rc-spread-pack-leak

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fixes an -mm=rc leak behind Array.join: a spread of run-time length passed to a rest parameter (f(...xs)) packs a temporary array that was never released.

Cause. MLIRGen builds that array as an empty ts.CreateArray, fills it through a slot of its own, and passes on a ts.Load of the slot. OwnedReturnConsumptionPass holds a new array across a call (retain before, release after) only when it comes straight from CreateArray/NewArray. So this array, born at count 0 and holding a count of each element, was never released. Array.join ends in "".concat(...parts), so every join leaked.

Fix. MLIRGen marks the literal's final read __spread_array_literal (SPREAD_ARRAY_LITERAL_ATTR_NAME). Under rc the pass treats it as a new array, so the call holds it and releases it afterwards, as for f(a, b). own keeps its existing handling.

Paired with ASDAlexander77/TypeScriptCompilerDefaultLib#107, which fixes the toFixed / Date string leak. That change is library-only: it marks the six core.os.d.ts string functions @owned.

Measured (-mm=rc, peak working set, 100K vs 1M iterations):

Probe Before After
"".concat(...parts) 107 MB at 1M 5.7 MB, flat
[a, b].join(",") 36.6 MB at 1M 5.7 MB, flat

Tests

  • New test-rc-spread-rest-pack reads the IR and checks the packed spread is retained before the call and released after it.
  • New spread-rest-pack runtime tests (compile and JIT) in gc, rc, none and own.
  • ctest -C Release -j 8: 8315/8315 pass.
  • DefaultLib tests.ps1 release (compile + JIT), rebuilt with this compiler and the DefaultLib branch: passes in gc, rc, none and own.

Not fixed here: a temporary array literal that's only read, like [a, b, "x"].length, still leaks under rc, with or without a spread. That's a separate, general bug.

🤖 Generated with Claude Code

…ven back

`f(...xs)` packs a new array with a spread of run-time length: an empty CreateArray filled through
a slot of its own, read out with a `ts.Load`. OwnedReturnConsumptionPass holds a new array given
straight to a call (retain before, release after) only when it comes from CreateArray/NewArray, so
this one - born uncounted, holding a count of every element - was never given back. The default
library's Array.join ends in `"".concat(...parts)`: 1M joins of two strings grew to 36.6 MB at 300K;
now flat at 5.7 MB.

MLIRGen marks the literal's final read __spread_array_literal (SPREAD_ARRAY_LITERAL_ATTR_NAME), and
the pass counts it as a new array under rc. Own keeps its own handling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 05d3e63 into main Nov 18, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the rc-spread-pack-leak branch November 18, 2026 21:02
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