alexcrichton requested fitzgen for a review on PR #11987.
alexcrichton opened PR #11987 from alexcrichton:less-clone to bytecodealliance:main:
This commit adds a new, safe, function to the
Instancetype for
internal use in Wasmtime which enables simultaneously borrowing the
Component-within-the-Instanceas well as the original store at the
same time. This is not possible to do in safe Rust when the store is
mutable and must be implemented withunsafecode.The motivation for this commit is performance. In https://github.com/bytecodealliance/wasmtime/issues/11974 it was
discovered that the addition of a singleArc::clonewas enough to
reduce throughput in the embedding by 20%. WhileArc::cloneis
generally cheap I can see how a "contended"Arc::clonecould get quite
expensive. This particular benchmark was running multiple instances of
the same component across multiple threads which were all doing many
host calls. Each host call, after the refactoring of https://github.com/bytecodealliance/wasmtime/pull/10959, contained
anArc::cloneto the component itself. This in turn led to many
threads constantly incrementing/decrementing theArc::clonecount of
the sameArcinstance.At a high level this clone is not necessary. The component lives within
the store and cannot be mutated/deleted during execution. Safe Rust,
however, forbids access to the component and mutably using the store at
the same time. Thus, this helper function enters the picture. The goal
here is to remove theArc::clonecalls without requiring major surgery
or such to refactor the implementations of various functions throughout
Wasmtime. Theunsafeblock used to implement this function documents
more rationale as to why this should be safe.Closes #11974
alexcrichton requested wasmtime-core-reviewers for a review on PR #11987.
alexcrichton commented on PR #11987:
Note that this is built on https://github.com/bytecodealliance/wasmtime/pull/11986, so only the second commit is relevant here.
fitzgen submitted PR review:
Do we have tests that run under MIRI that cover this?
fitzgen created PR review comment:
// within a component instance, within a store, to be deallocated or mutated while // a store is in use. Consequently it should be safe to simultaneously
fitzgen submitted PR review.
alexcrichton updated PR #11987.
alexcrichton commented on PR #11987:
We do indeed! There are a number of tests in
tests/all/pulley.rsrun under miri which do host calls which should exercise these paths.
alexcrichton has enabled auto merge for PR #11987.
alexcrichton merged PR #11987.
Last updated: Dec 06 2025 at 06:05 UTC