Skip to content

@owned on lib.d.ts / core.d.ts: under rc a library call's result is its caller's - #106

Merged
ASDAlexander77 merged 1 commit into
mainfrom
rc-owned-declarations
Nov 18, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
rc-owned-declarations

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Marks the declarations lib.ts defines with @owned (lib.d.ts: every top-level declare function, declare class and declare namespace; core/core.d.ts: the Error classes), so under -mm=rc the compiler counts their results as the caller's.

Without it, every library declaration was unclassified in OwnedReturnConsumptionPass. A program that used the library never had a closed world, so every call through a function value, callback or any leaked its heap result, and so did every direct library call.

  • core/core.os.d.ts stays unmarked. convertNumber, timestamp_to_string and the time_* functions return a pointer into a char[] buffer, not a reference of their own.
  • Needs the compiler PR that adds @owned (ASDAlexander77/TypeScriptCompiler, branch rc-owned-declarations). An older compiler ignores the decorator, so merge order doesn't matter.

Measured with the compiler PR (-mm=rc, peak working set):

Probe Before After
1M calls through (n) => any / (n) => P 97 MB 5.6 MB (flat)
.trim() in a loop, 1M 20.8 MB 5.7 MB (flat)
mixed library probe (map/reduce callbacks, Date, Error subclass, split, toFixed), 300K 279 MB 69.7 MB

The mixed probe prints the same output under gc and rc. Its remaining growth is inside the library (toFixed through convertNumber's buffer), a separate leak.

Tests: tests.ps1 release (compile + JIT) passes in gc, rc, none and own against the compiler branch.

🤖 Generated with Claude Code

… its caller's

lib.ts defines what these files declare, under the program's own memory model, so each function
returns a reference its caller owns. Without a word saying so the compiler could classify none of
them, every call kept its +1 for good, and one heap-returning declaration was enough to stop any
call through a function value or callback from giving its result back: under rc, 1M calls of a
`(n) => any` returning a fresh string grew to 97 MB; with `@owned` they stay at 5.6 MB.

core/core.os.d.ts stays unmarked: convertNumber, timestamp_to_string and the time_* functions
return a pointer into a char[] buffer, not a reference of their own. Needs the compiler's `@owned`
(an older compiler ignores the decorator).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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