Stream: git-wasmtime

Topic: wasmtime / PR #15034 Do not force the `VMDeferredThread` ...


view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 20:59):

fitzgen opened PR #15034 from fitzgen:lazy-lenders to bytecodealliance:main:

This commit introduces a lazy_lenders buffer on VMStoreContext. When we need to push a lender for a resource borrow, but haven't forced the lazy thread context yet, we push to lazy_landers. This avoids forcing the lazy thread context, like we previously did. Each VMDeferredThread keeps a watermark of the lazy_lenders length upon construction so that, when we do something that does force the lazy thread context, we can partition lazy_lenders by each thread context that is responsible for those borrows.

This does not change what is inlined in Wasm code or what is done in libcalls. In the inline exit-sync-call fast path, we bail out to the host when there are lazy lends to be undone. The host libcall still takes care to avoid forcing the VMDeferredThread right before we exit the call in this case.

Follow up commits will move a bunch of this work inline into the adapters, but I wanted to keep this introduction of lazy_lenders as small as I could.

Even so, this is a significant speedup for cm-online-stats, our adapter-call-heavy Sightglass benchmark:

execution :: instructions-retired :: cm-online-stats

    lazy-lenders/libengine.dylib is 8.4852x to 8.4854x faster than main/libengine.dylib!
    (confidence = 99%, p < 0.0001)

    ┌────────────┬────────────┬───────────────┬───────────────┬────────┬──────────────────────────────┐
    │ Min        │ Max        │ Mean          │ Median        │ Noise  │ Engine                       │
    ├────────────┼────────────┼───────────────┼───────────────┼────────┼──────────────────────────────┤
    │ 819957895  │ 827344185  │ 820584935.77  │ 819983269.00  │ 0.014% │ lazy-lenders/libengine.dylib │
    ├────────────┼────────────┼───────────────┼───────────────┼────────┼──────────────────────────────┤
    │ 6957508999 │ 6980867062 │ 6962057536.66 │ 6957759525.50 │ 0.192% │ main/libengine.dylib         │
    └────────────┴────────────┴───────────────┴───────────────┴────────┴──────────────────────────────┘

execution :: cycles :: cm-online-stats

    lazy-lenders/libengine.dylib is 7.41x to 7.48x faster than main/libengine.dylib!
    (confidence = 99%, p < 0.0001)

    ┌────────────┬────────────┬───────────────┬───────────────┬────────┬──────────────────────────────┐
    │ Min        │ Max        │ Mean          │ Median        │ Noise  │ Engine                       │
    ├────────────┼────────────┼───────────────┼───────────────┼────────┼──────────────────────────────┤
    │ 156815386  │ 183006652  │ 162831928.91  │ 161767696.00  │ 1.037% │ lazy-lenders/libengine.dylib │
    ├────────────┼────────────┼───────────────┼───────────────┼────────┼──────────────────────────────┤
    │ 1170592107 │ 1302149964 │ 1210932378.38 │ 1206847175.00 │ 2.839% │ main/libengine.dylib         │
    └────────────┴────────────┴───────────────┴───────────────┴────────┴──────────────────────────────┘

compilation :: instructions-retired :: cm-online-stats

    main/libengine.dylib is 1.00x to 1.03x faster than lazy-lenders/libengine.dylib!
    (confidence = 99%, p = 0.0017)

    ┌──────────┬──────────┬─────────────┬─────────────┬────────┬──────────────────────────────┐
    │ Min      │ Max      │ Mean        │ Median      │ Noise  │ Engine                       │
    ├──────────┼──────────┼─────────────┼─────────────┼────────┼──────────────────────────────┤
    │ 67817425 │ 85606560 │ 77573786.81 │ 77931355.50 │ 5.618% │ lazy-lenders/libengine.dylib │
    ├──────────┼──────────┼─────────────┼─────────────┼────────┼──────────────────────────────┤
    │ 66424126 │ 85683019 │ 76488563.76 │ 76928252.50 │ 5.252% │ main/libengine.dylib         │
    └──────────┴──────────┴─────────────┴─────────────┴────────┴──────────────────────────────┘

compilation :: cycles :: cm-online-stats

    No difference in performance.

    ┌──────────┬───────────┬─────────────┬─────────────┬─────────┬──────────────────────────────┐
    │ Min      │ Max       │ Mean        │ Median      │ Noise   │ Engine                       │
    ├──────────┼───────────┼─────────────┼─────────────┼─────────┼──────────────────────────────┤
    │ 23475857 │ 93001875  │ 51231120.40 │ 48774961.50 │ 32.407% │ lazy-lenders/libengine.dylib │
    ├──────────┼───────────┼─────────────┼─────────────┼─────────┼──────────────────────────────┤
    │ 22429448 │ 123149979 │ 53576849.80 │ 48918162.00 │ 43.167% │ main/libengine.dylib         │
    └──────────┴───────────┴─────────────┴─────────────┴─────────┴──────────────────────────────┘

<!--
Please make sure you include the following information:

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please review the Bytecode Alliance's AI tool usage policy at
https://github.com/bytecodealliance/governance/blob/main/AI_TOOL_POLICY.md

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->

view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 20:59):

fitzgen requested cfallin for a review on PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 20:59):

fitzgen removed the review request for wasmtime-compiler-reviewers from PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 20:59):

fitzgen requested wasmtime-compiler-reviewers for a review on PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 20:59):

fitzgen requested wasmtime-core-reviewers for a review on PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 21:42):

fitzgen updated PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 27 2026 at 23:44):

github-actions[bot] added the label wasmtime:api on PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 04:01):

:memo: cfallin submitted PR review:

Thanks! This rates as "plausible" to me only because I'm not deeply familiar with the borrow machinery here; @alexcrichton should probably look it over too. Just a few comments I noted below as I read through.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 04:01):

:speech_balloon: cfallin created PR review comment:

"Watermark" to me implies some sort of lazy "max seen so far" behavior; this is really just "start of our segment in lazy_lenders", right? So lazy_lenders_index or lazy_lenders_start might be more clear?

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 04:01):

:speech_balloon: cfallin created PR review comment:

We could avoid the smallvec clone here with a hypothetical components_data.lazy_lenders_and_task_state_mut() returning a split borrow, perhaps (similar to the trick we play in a bunch of instances on StoreOpaque)?

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 04:02):

cfallin requested alexcrichton for a review on PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 15:13):

fitzgen updated PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 15:34):

fitzgen updated PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:memo: alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

A few thoughts on this:

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

Subjectively I'd say we could probably get away with a much smaller value here, even something like 4 or maybe 8. This seems relatively difficult to fill in conventional components where there's not all that much cross-component-talk and fast paths generally need just one or two slots.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

We talked about this briefly elsewhere, but personally I'd say just go ahead and remove this and inline the constant LAZY_LENDERS_CAPACITY everywhere since it seems pretty unlikely we'd want to double this at runtime.

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

Instead of a packed u64 would it make sense for this to be just two u32 fields, perhaps with the raw index type itself?

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

Would it make sense to back this up with a debug_assert!?

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

Instead of a LazyLendersFull error, could this perhaps return Option<..> where None means "no you can't push the capacity is full"

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

How come this is all #[cfg]'d to component-model-async? I'd naively expect the optimization here to equally apply with/without component-model-async

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

Would it be possible to just delete this code? This seems like it'll all quickly become dead code as this would otherwise happen in cranelift-compiled code otherwise. I think it's reasonable to add the lazy infrastructure here, but I also think it'd be best to avoid adding pieces that won't survive to the final implementation (where possible)

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

This method is mostly a duplicate of what's below, in particular the let guest block and assembling all the borrows and such. Could these methods be combined to share more of the implementation?

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

Any particular reason to split out reserve/push here? OOM-wise I figure it doesn't really matter if we drop a borrow or two while we're trapping due to OOM

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

We talked a bit about this elsewhere, but I wanted to write this down here too. I think it'd be best to ideally remove the need for this method altogether where the lazy lend is a record of "here's the borrows we haven't yet accounted for in the official tables" and then undoing that is a simple "move the stack pointer" style operation which needs no sort of destructor or such. That would make actually recording a lazy lend that much faster (just put the resource in a table) and would keep everything consistent I believe as well.

I'm hopeful that something like this would help simplify both this PR as-is and subsequent PRs to add more infrastructure, but I could also be off-the-mark here

view this post on Zulip Wasmtime GitHub notifications bot (Oct 29 2026 at 20:56):

:speech_balloon: alexcrichton created PR review comment:

To keep raw values, maybe flip this and have the host at u32::MAX and other types as-is?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:11):

fitzgen updated PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:13):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:13):

:speech_balloon: fitzgen created PR review comment:

The exit fast path no longer reads the lazy_lenders_start and instead just resets it. We can't get rid of it though or else we wouldn't be able to partition the lazy lends by frame properly. But the main thrust of your concern has been addressed I believe, since we have just a single brif(icmp(...)) now.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:13):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:14):

:speech_balloon: fitzgen created PR review comment:

Changed to 8.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:14):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:14):

:speech_balloon: fitzgen created PR review comment:

Done.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:14):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:14):

:speech_balloon: fitzgen created PR review comment:

Done.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:15):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:15):

:speech_balloon: fitzgen created PR review comment:

Added a debug assert in LiftContext::new which is where the deferred thread is forced.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:15):

:speech_balloon: fitzgen created PR review comment:

Done.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:15):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:17):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:17):

:speech_balloon: fitzgen created PR review comment:

We don't do deferred threads when there isn't cfg(feature = "component-model-async") because there are no threads to force in that build, so there is also nothing to do here in that build.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:17):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:17):

:speech_balloon: fitzgen created PR review comment:

Done

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:17):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:17):

:speech_balloon: fitzgen created PR review comment:

Done

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:18):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:18):

:speech_balloon: fitzgen created PR review comment:

Yeah it was to try to separate side effects until after memory reservations succeeded, but it doesn't matter here. Merged.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:19):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:19):

:speech_balloon: fitzgen created PR review comment:

Done

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:19):

:speech_balloon: fitzgen created PR review comment:

Done, and it was indeed a nice clean up that got rid of a bunch of machinery that would ultimately be removed at the end of my patch stack anyways, so that seems like a nice win.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:19):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:19):

fitzgen requested alexcrichton for a review on PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 16:20):

fitzgen commented on PR #15034:

@alexcrichton this should be ready for another round of review now, I believe I've addressed everything

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 18:21):

:speech_balloon: alexcrichton created PR review comment:

Why a new impl block?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 18:21):

:thumbs_up: alexcrichton submitted PR review:

Overall lgtm, but the main thing that doesn't sit well with me still is all the #[cfg] necessary. When removing most of them I see locally that indeed promote_lazy_lends isn't used, but that also doesn't sit well with me. There's nothing component-model-async-specific about any of this code and it's, in theory at least, all equally applicable both with and without async.

My main worry is that we'll get ourselves into a situation where in certain compilation modes, which aren't well tested, we end up mismatching lazy things vs not. For example we would accidentally push lazy lends but then not actually ever flush the list or try promotion at some point. I don't believe that's currently happening, but all the configuration options we have put us precariously close to being in this situation.

Would you be ok with a tact like:

My hope with that is to avoid undue #[cfg] traffic, keep track in-code that something isn't implemented yet, and have at least an issue on file for this. My attempt left a few assorted #[cfg(feature = "component-model")] annotations which I'd ideally like to see moved into component-specific locations as opposed to gating, but that's relatively minor. One example was impl VMResourceIndex which could probably move to vm/component.rs

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 18:21):

:speech_balloon: alexcrichton created PR review comment:

Was this removal intentional?

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 20:52):

fitzgen updated PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:12):

fitzgen updated PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:14):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:14):

:speech_balloon: fitzgen created PR review comment:

Whoops, that got caught in the re-merging of reserve and push. Fixed.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:14):

:memo: fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:14):

:speech_balloon: fitzgen created PR review comment:

Folded into the existing one.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:15):

fitzgen has enabled auto merge for PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:29):

fitzgen added PR #15034 Do not force the VMDeferredThread for borrows to the merge queue.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:53):

:check: fitzgen merged PR #15034.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 04 2026 at 21:53):

fitzgen removed PR #15034 Do not force the VMDeferredThread for borrows from the merge queue.

view this post on Zulip Wasmtime GitHub notifications bot (Nov 05 2026 at 13:18):

fitzgen commented on PR #15034:

:smirk:

(Small compilation regression on cm-online-stats due to the additional lazy lenders length reset in CLIF meaning there is a little more CLIF to compile than before.)

https://github.com/bytecodealliance/are-we-fast-yet/actions/runs/41932981279


Last updated: Nov 19 2026 at 20:05 UTC