Skip to content

fix(husk): charge shared arrays and maps once against the plugin heap - #399

Merged
fcoury merged 2 commits into
mainfrom
fix/husk-shared-heap-accounting
Oct 15, 2026
Merged

fcoury merged 2 commits into
mainfrom
fix/husk-shared-heap-accounting

Conversation

@fcoury

@fcoury fcoury commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #395.

Why

The Husk runtime charged every variable, parameter and captured value the full size of what it held. Arrays and maps are shared (Arc), so passing one to a function copies nothing. But each new binding was still charged the whole list again. A plugin holding a 25 MB list could not pass it to a helper: four bindings meant 100 MB against a 64 MiB heap. The errors looked like real memory exhaustion, and lsp_symbols had to work around them (#388, #393).

What changed

  • Shared storage is counted once. The heap keeps a table of the arrays and maps that cells hold, keyed by storage address, with a count of the cells and parent lists that refer to each. A cell storing a list that's already counted is charged only for the value itself. The list's contents are released when the last holder goes away, by frame release, reassignment or garbage collection.
  • Writes are still charged. Changing a list (push, index or field assignment) works on a copy, because the cell and the table both hold the original. That copy is new storage and is charged in full, so the limit still bounds real memory. The table holds a clone of each counted Arc, so a counted address can't be reused while it's in the table.
  • Failed writes roll back. Reassignment takes the old value out of the cell instead of cloning it. When the new value goes over the limit, its holds are released, and the old value's holds and the value itself are restored, so nothing leaks into the table.
  • heap_value_size now counts a struct's type name, as it already did for variants, so it matches the new charge exactly. The comments on it and heap_cell_size now say that heap_cell_size is the exact charge for a value no cell shares yet, such as a host result, which is the number the fix(plugins): trim or drop request results too large for a plugin's heap #398 budget check uses. In lsp_symbols.hk I removed the comments blaming per-binding charges. The 4,096-record slicing stays, because pickers show at most that many rows.

The deterministic performance gate (husk_cursor_bench) is unchanged within noise: p50 789 µs before, 797 µs after, on the same machine.

How to Test

  1. Save this as shared.hk. It builds one ~25 MB list of 800 strings of 32 KiB each and holds it in four bindings:

    fn main() {
        let chunk = "x";
        for i in 0..15 {
            chunk = chunk + chunk;
        }
        let seeds = [];
        for i in 0..800 {
            seeds.push(i);
        }
        let items = seeds.map(|i| chunk);
        let copy = items;
        report(copy, items);
    }
    
    fn report(first: [String], second: [String]) {
        let kept = first;
    }
  2. Run cargo run -p husk-cli -- run shared.hk. It exits with status 0 and no output. On main it fails at report(copy, items) with Husk heap byte limit exceeded (requested 26284976 bytes with 52674082 of 67108864 bytes live).

  3. Regression check, the limit still holds: save this as runaway.hk. It pushes a 1 MiB string 100 times, so every push makes a new, larger list:

    fn main() {
        let chunk = "x";
        for i in 0..20 {
            chunk = chunk + chunk;
        }
        let items = [];
        for i in 0..100 {
            items.push(chunk);
        }
    }

    cargo run -p husk-cli -- run runaway.hk still fails with Husk heap byte limit exceeded (value needs 66065920 bytes …).

  4. Optional: open a large TypeScript project and press Space w. The workspace symbol picker behaves as after fix(plugins): trim or drop request results too large for a plugin's heap #398.

New tests in husk-runtime:

  • shared_array_is_charged_once_however_many_cells_hold_it: one array held by six cells across two frames fits a heap sized for one copy. It also checks that all its memory is released afterwards.
  • copying_a_shared_array_to_change_it_is_charged: a push on a shared array is charged as a full copy. After the failed write, nothing has leaked.
  • growing_an_array_past_the_heap_limit_still_fails
  • unshared_value_is_charged_exactly_its_heap_value_size: for a value no cell shares yet, such as a host result, the charge equals heap_cell_size, the number the fix(plugins): trim or drop request results too large for a plugin's heap #398 budget check relies on.

The first two fail on main.

Every cell holding an Arc-shared array was charged its full size, so a
plugin holding a 30 MB list could not pass it to a helper. Count shared
storage once, with a holder count, and charge cells only for the value
itself. Writes still copy and are still charged.

Fixes #395
@fcoury

fcoury commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T18:18:08.093454Z 705e7dc Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 705e7dc099

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/husk-runtime/src/lib.rs Resolved
Comment thread crates/husk-runtime/src/lib.rs Resolved
…nment

heap_value_size left out a struct's type name, which the shared-storage
charge includes, so a host result sized to the free heap could pass the
preflight check and fail allocation. Reassignment also cloned the old
value, a deep copy for strings and JSON.
@fcoury
fcoury merged commit ce2ef65 into main Oct 15, 2026
21 checks passed
@fcoury
fcoury deleted the fix/husk-shared-heap-accounting branch October 15, 2026 18:33
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.

Husk heap accounting charges a shared array's full size to every variable that holds it

1 participant