Skip to content

clazel-js-heap: index shape lookups (faster runtime and page setup) - #305

Merged
atiernan merged 4 commits into
mainfrom
claude/js-setup-speed
Nov 25, 2026
Merged

atiernan merged 4 commits into
mainfrom
claude/js-setup-speed

Conversation

@atiernan

@atiernan atiernan commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Makes building a JS runtime and a page with the generated WebIDL bindings much cheaper. All changes are in clazel-js-heap; nothing in clazel-js-vm's Verus code changed.

Hot spots (native sample of debug builds)

  • Shapes::lookup walked the parent chain (up to 128 shapes) on every get, define and add. It took 25% of plain Runtime setup and 66% of page setup on claude/dom-api, mostly through Heap::find and Heap::define.
  • String construction: JsString::from_rust_str went through UTF-16 and an Option collect, about half of runtime setup.
  • Interning: a miss hashed twice and cloned twice, and every HashMap growth rehashed every string.

Changes (three commits)

  1. Contract first (shape/contract.rs, X1 to X5). The parent walk is the spec (spec_lookup), and table_well_formed defines what a well-formed table is.
  2. Shape index (shape/index.rs):
    • Every shared shape names a table holding its properties in slot order and sorted by key. All shapes on one line of transitions share it: a child that extends its parent's table at the end appends in place, and a branch copies the prefix.
    • A lookup is a hand-written binary search.
    • A sweep shortens each table to its longest live user and frees unused tables.
    • add_property no longer looks the key up before checking transitions.
    • Transitions are a short list until there are more than 8.
  3. Strings and interning:
    • ASCII goes straight to Latin-1.
    • Interner stores each string once, with its hash, in its own open-addressing table of key ids. It still uses std's keyed SipHash, so page-chosen names can't collide on purpose. That means one hash per intern, and none on growth or sweep.

Measurements

before (main) after
Miri, clazel-js tests/api.rs script_host_data_comes_back 39.4 s 16.6 s
Native Runtime::new + realm, debug 2.07 ms 0.86 ms
Native Runtime::new + realm, release 263 µs 124 µs
claude/dom-api with this cherry-picked locally: Miri a_page_with_the_bindings_runs_a_script 278 s 115 s
claude/dom-api, native page setup, debug 15.3 ms 6.5 ms
claude/dom-api, native page setup, release 2.0 ms 0.83 ms

The Miri times are libtest's --report-time on Miri's virtual clock. The Miri timings in the table come from just before the last small change (the hand-written binary search, which took about another 4% off native debug time). The dom-api numbers are a local cherry-pick; nothing was pushed to that branch.

Verification: what Kani covers and what it doesn't

New harnesses in src/proofs.rs:

  • index_table_push_lookup: one index table after up to 3 pushes of any key below 3, with any attributes.
    • It is well formed (X2).
    • A push is refused exactly when the key is already there.
    • Its entries are the accepted pushes, in order.
    • A lookup of any u32 key, at any prefix length n, equals spec_lookup of the first n entries, and any slot it finds is below n (X3).
  • index_search_any: the binary search over every sorted list of up to 4 keys, of any value, against any key.

Not proven by Kani:

  • Table::truncate, the shortening in X5. A harness for it (after 2 or 3 pushes, truncating to any length or to 1) took CBMC to 10–18 GB within a minute. It killed CI's Verify clazel-js-heap job on the 16 GB runner, so it was dropped.
  • Index as a whole: its table list, extend's choice between appending and copying, and retain's bookkeeping.
  • Shapes: transitions and dictionaries are hash maps.

Harnesses over trees of shapes through Index were tried and dropped. With symbolic parents (3 or 4 shapes) they did not finish in 25+ minutes; with a fixed tree form they ran for over 3 hours and over 12 GB. That glue is covered by shape::tests::the_index_agrees_with_the_parent_walk_on_random_trees. It builds random trees, with branches, transitions past the list cutoff and sweeps closed under parents, and checks every live shape's lookup against the old parent walk and spec_lookup, and every table as well formed. All existing harnesses are unchanged.

Checks

  • cargo clippy --workspace --all-targets: clean.
  • cargo fmt --check: clean.
  • cargo test --workspace: passed.
  • cargo xtask check: STATIC CHECKS PASSED.
  • cargo xtask verify --changed: not completed locally (the first run was killed during Miri; the second was stopped to chase the CI failure).
  • cargo kani -p clazel-js-heap: all 14 harnesses pass in 492 s. The new ones: index_table_push_lookup 14 s and 1.4 GB peak; index_search_any 3.5 s and 0.3 GB. The run's 17 GB peak (macOS RSS) comes from the existing dense_one_op, which is unchanged from main (17.5 GB alone); gc_mark_closed_over_roots peaks at 10.7 GB.
  • Miri, clazel-js-heap --lib: the random-tree test is cut from 184 s to 27 s.

CI's full verification has not run yet; the proof jobs need a full-verification approval.

Not in this PR

Removing the #[cfg_attr(miri, ignore = "... about 290 s ...")] attributes on #300 and #297 is a follow-up, once both have merged and the per-page Miri time has been re-measured there.

🤖 Generated with Claude Code

atiernan and others added 3 commits November 24, 2026 17:18
The parent walk in Shapes::lookup is the spec; the index of tables
shared along lines of transitions must agree with it. Contract first
(CLAUDE.md rule 3); the implementation and harnesses follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- JsString::from_rust_str takes ASCII straight as Latin-1, without the
  UTF-16 detour; from_units checks for Latin-1 before copying.
- Interner stores each string once, with its hash, and searches its own
  open-addressing table of key ids (std's keyed SipHash, at most half
  full): one hash per intern, none on growth or sweep. Before, a miss
  hashed twice, cloned the string twice, and every growth of the
  HashMap rehashed every string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Shapes::lookup walked the parent chain (up to 128 shapes) on every
property get, define and add; building a runtime, and a page with the
WebIDL bindings, spent most of its time there. Now every shared shape
names a table (shape::index) of its properties in slot order and sorted
by key, shared by the shapes on one line of transitions: a child that
extends its parent's table at its end appends in place, a branch copies
the parent's prefix. A lookup is a binary search, meeting the contract
of the previous commit (X1 to X5); the parent walk stays as the test
oracle.

Also: add_property no longer looks the key up before the transition
(a hit needs none, and the index refuses a duplicate); transitions are
a short list until there are more than eight; sweeps shorten tables
to their longest live user and free unused ones.

Kani: the index's table (push, lookup at any prefix, truncate) and its
binary search; Index and Shapes as a whole are covered by a random-tree
unit test against the parent walk, with sweeps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atiernan
atiernan temporarily deployed to full-verification November 24, 2026 21:52 — with GitHub Actions Inactive
…test under Miri

index_table_truncate took CBMC to 10 to 18 GB within a minute (after two
or three pushes, truncating to any length or to one), past CI's 16 GB
runner; the Verify clazel-js-heap job on #305 died of it. Table::truncate
is left to the unit tests and stated as not proven.

the_index_agrees_with_the_parent_walk_on_random_trees took 184 s under
Miri; it now runs 2 rounds of 40 steps there, sweeping every 20 (27 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atiernan
atiernan temporarily deployed to full-verification November 25, 2026 01:35 — with GitHub Actions Inactive
atiernan added a commit that referenced this pull request Nov 25, 2026
A page defines about 550 attributes (940 getter and setter functions) and
uses few. Miri time of a page with every interface and a one-line script:
292 s before, 192 s now (69 s with #305's heap).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@atiernan
atiernan added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 5bc131b Nov 25, 2026
21 checks passed
atiernan added a commit that referenced this pull request Nov 25, 2026
…nts-input (#300)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was previously deployed

1 inactive deployment
full-verification — c765fbb9 Deployed Nov 25, 2026 by atiernan via Approve the proofs #823
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