fix: balance linear-memory limiter reservations - #61
Open
matthargett wants to merge 1 commit into
Open
matthargett wants to merge 1 commit into
matthargett wants to merge 1 commit into
Conversation
Refund approved growth when backing allocation fails or a shared-memory proposal goes stale. Retain memory charges until the backing allocation is freed, including across shared handles and worker-store teardown. Stage module memories before publishing them so partial allocation failure does not strand an unreachable charge.
Owner
|
Thanks for the PR! I'm a little unsure about this, so I might have to take some time to think about it. I've been waiting for the Custom Allocator API to be stabilized in Rust (which it finally will be in the next version 🎊), and that might be a better fit for this use case, with a per-store configurable allocator for Wasm guest data. I think ResourceLimiter should probably stay closer to policy ("is this resource allowed to grow?"), while the allocator handles actual memory accounting. |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A host with a main WASM guest and a potentially short-lived WASM-backed AudioWorklet processors needs one quota across worker restarts to gracefully handle abuse (perhaps unintentional) cases. The W3C Web Audio model puts processors on a rendering thread; using WASM there is my embedding choice for my little game runtime. Limiter approval currently precedes fallible allocation, with no refund or teardown signal, which is a problem when running a WebAudio graph that's a little too big to run on Apple Watch.
This PR adds default no-op failure/release callbacks, keeps charges with the backing memory through shared-handle lifetime, and stages module memories before publishing them so that partial allocation failures refund earlier reservations. The limiter handle uses atomic reference counting so shared memory can outlive its creating store. Host-created shared memory starts uncharged; its first limiter-approved guest growth binds the accounting owner. I measured branch prediction and L1 cache misses in the opcode dispatch path on iPhone XS/11/12 to see if there's a ripple effect that affects performance, and its unchanged.
Tests cover allocation failure, multi-memory partial failure, imported memory, custom pages, concurrent stores, stale shared growth, and last-handle release. I had to fortify this when I experimented with wasmtime, WasmEdge, and WAMR as well, but lmk if there's other fortifications you want to see :D