You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf(wasm): cache memory views, slot codec, and dense dispatch for mem_call - #16007
Eliminates per-call TypedArray/BigInt/argument-array allocations and string-key object lookups on the mem_call and mem_call_i32 bridge hot path in wasm_runtime.js.
Motivation
Every mem_call previously constructed two fresh BigUint64Array views over wasmMemory.buffer, decoded and encoded slots through BigInt (__bitsToJsValue/__jsValueToBits), allocated a temporary args array, spread ...args, and performed repeated property lookups on __memDispatch / __perryUiDispatch by string name.
Cache _memBuf, _memU32, _memF64, _memDV, and _memLen in __refreshMem(), automatically refreshing before reads and after calls if a callback triggered memory.grow (detaching or resizing wasmMemory.buffer), with bounds and 8-byte alignment validation.
Read and write 64-bit NaN-boxed slots directly via two 32-bit words (_memU32) / _memF64 on little-endian hosts (with DataView fallback on big-endian hosts), canonicalizing all JS NaN values to (0x7FF80000, 0) to prevent NaN-tag spoofing.
Mark mem_call and mem_call_i32 with __rawBigint = true and unroll arities 0..4 without intermediate args arrays.
Build dense integer-indexed dispatch tables (__memFnById / __i32FnById) via __buildMemDispatchCache() after _start() populates static strings, with transparent fallback for _start()-time calls, dynamic strings, __classDispatch, direct WebAssembly.instantiate, and Workers.
No actionable comments were generated in the recent review.
Walkthrough
The WASM runtime now uses cached linear-memory views and slot encoding for mem_call and mem_call_i32. It adds static-ID dispatch caches, validates call inputs, refreshes views after memory changes, and includes tests for dispatch, encoding, memory growth, exceptions, and invalid inputs.
Changes: WASM mem_call fast path
Layer / File(s)
Summary
Slot codec and memory views crates/perry-codegen-wasm/src/wasm_runtime.js, crates/perry-codegen-wasm/src/emit/mem_call_fastpath_tests.rs
The runtime adds cached linear-memory views and slot encoding and decoding. Tests cover round trips across value types and endian settings.
Dispatch cache and boot lifecycle crates/perry-codegen-wasm/src/wasm_runtime.js, crates/perry-codegen-wasm/src/emit/mem_call_fastpath_tests.rs
WASM boot resets dispatch-cache state and builds static dispatch tables after _start or main. Tests cover cached and dynamic resolution, direct startup, minified startup, and worker bootstrap.
mem_call and mem_call_i32 validate call inputs and use slot encoding to read arguments. They dispatch calls, preserve exception handling, and handle memory changes before result writeback. Tests cover arities, memory growth, exceptions, and invalid inputs. The changelog describes the update.
sequenceDiagram
participant Runtime as wasm_runtime.js
participant Memory as linear memory
participant Cache as dispatch cache
participant Target as target function
Runtime->>Memory: Read argument slots
Runtime->>Cache: Resolve static function ID
Cache-->>Runtime: Return cached function
Runtime->>Target: Invoke with decoded arguments
Target-->>Runtime: Return result
Runtime->>Memory: Refresh views and write result
PerryTS/perry#16002: This PR also changes NaN canonicalization in the WASM runtime encoders, which overlaps with the slot codec’s NaN handling.
Suggested reviewers:raliclo, thehypnoo
Merge Risk: Minimal · up to f27db
This change speeds up the WASM mem_call path with cached memory views and dispatch tables. Inspection found no current regression in call behavior, and the author reports tests passing. No merge-blocking risk was identified.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title is a single, specific sentence that names the main change: a performance change to the wasm mem_call path. It names the three parts of the change (cached memory views, slot codec, dense disp…
Description check
The description follows the repository template. It has a Summary, Motivation, Changes, and Verification, which maps to the template's Test plan. It omits the Related issue section, the Type of change…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title is a single, specific sentence that names the main change: a performance change to the wasm mem_call path. It names the three parts of the change (cached memory views, slot codec, dense dispatch), and the description and objectives describe the same work. The title follows the perf(wasm) convention. It does not describe a different change.
Full details: Description check
Explanation
The description follows the repository template. It has a Summary, Motivation, Changes, and Verification, which maps to the template's Test plan. It omits the Related issue section, the Type of change checkboxes, the Platforms checkboxes, and the Tested on line. The Test plan has no fenced command block for the lint gate, and the checklist items are not ticked. The author gave test results and a verification list, so the description is mostly complete.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
Note on the overlap with #16002: this PR canonicalizes numeric NaNs in the fast-path linear-memory slot writer (__writeSlot), while #16002 covers the host-value and worker encoders used outside that path (fromJsValue, __jsValueToBits, and WORKER_BOOTSTRAP). Both paths are complementary, emit the same canonical quiet NaN bits (0x7FF8_0000_0000_0000), and merge cleanly in wasm_runtime.js.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
area:compilerparser, HIR, transform, codegen, wpfactsplatform:wasmSpecific to wasmtype:perfRuntime, compile-time, build-size, or memory performance
1 participant
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Eliminates per-call TypedArray/BigInt/argument-array allocations and string-key object lookups on the
mem_callandmem_call_i32bridge hot path inwasm_runtime.js.Motivation
Every
mem_callpreviously constructed two freshBigUint64Arrayviews overwasmMemory.buffer, decoded and encoded slots throughBigInt(__bitsToJsValue/__jsValueToBits), allocated a temporaryargsarray, spread...args, and performed repeated property lookups on__memDispatch/__perryUiDispatchby string name.Changes
crates/perry-codegen-wasm/src/wasm_runtime.js):_memBuf,_memU32,_memF64,_memDV, and_memLenin__refreshMem(), automatically refreshing before reads and after calls if a callback triggeredmemory.grow(detaching or resizingwasmMemory.buffer), with bounds and 8-byte alignment validation.__readSlot/__writeSlot):_memU32) /_memF64on little-endian hosts (withDataViewfallback on big-endian hosts), canonicalizing all JSNaNvalues to(0x7FF80000, 0)to prevent NaN-tag spoofing._start()dispatch cache:mem_callandmem_call_i32with__rawBigint = trueand unroll arities 0..4 without intermediateargsarrays.__memFnById/__i32FnById) via__buildMemDispatchCache()after_start()populates static strings, with transparent fallback for_start()-time calls, dynamic strings,__classDispatch, directWebAssembly.instantiate, and Workers.crates/perry-codegen-wasm/src/emit/mem_call_fastpath_tests.rs):i32truncation, exception handling, all NaN-box types + little/big-endian paths + NaN canonicalization,memory.growview refresh during callbacks, bounds/alignment rejections, dense vs dynamic dispatch fallback, and__classDispatch.Verification
cargo test -p perry-codegen-wasm: all 7 newmem_call_fastpathtests pass (365 passed onmainbase).RUSTFLAGS="-D warnings" cargo check -p perry-codegen-wasm --all-targets: 0 errors.rustfmt --edition 2021 --check: clean.Summary by CodeRabbit
Performance
Bug Fixes