Skip to content

component: Defer host task creation - #14350

Open
adamrk wants to merge 3 commits into
bytecodealliance:mainfrom
adamrk:abk/deferred-host-task
Open

adamrk wants to merge 3 commits into
bytecodealliance:mainfrom
adamrk:abk/deferred-host-task

Conversation

@adamrk

@adamrk adamrk commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Defer creation of a HostTask and insertion of it in the Store until it is required. This is an optimization on wasm->host calls where the HostTask may not actually be needed. The HostTask is materialized if the host future actually suspends (doesn't return Ready on the first poll), or if it tries to lower a borrowed resource (which requires inserting its CallContext into the Store to get a persistent identifier for it).

The work is separated into 3 commits:

  • Add benchmarks for nop wasm->host calls that pass a borrow and where the host suspends before returning.
  • Make the u32 identifier for CallContexts an proper enum
  • The actual change of deferring HostTask creation.

These are the changes I see in the wasm->host benchmarks:
Baseline benchmarks:

component-async/wasm-to-host                                                                                          
                        time:   [354.02 ns 366.83 ns 380.05 ns]               
component-async/wasm-to-host-borrow              
                        time:   [462.76 ns 479.96 ns 497.66 ns]                                                       
component-async/wasm-to-host-pending-once                                                                             
                        time:   [2.5383 µs 2.6139 µs 2.6873 µs]

With changes:

component-async/wasm-to-host                                                                                          
                        time:   [283.83 ns 296.05 ns 308.65 ns]
                        change: [−21.162% −17.822% −14.374%] (p = 0.00 < 0.05)                                        
                        Performance has improved.        
component-async/wasm-to-host-borrow                                                                                   
                        time:   [377.80 ns 392.33 ns 406.01 ns]               
                        change: [−31.982% −29.587% −26.924%] (p = 0.00 < 0.05)
                        Performance has improved.          
component-async/wasm-to-host-pending-once                                                                             
                        time:   [2.4307 µs 2.5107 µs 2.5932 µs]                                                   
                        change: [−3.0505% +0.1437% +3.5901%] (p = 0.94 > 0.05)
                        No change in performance detected.

@github-actions github-actions Bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Sep 16, 2026
@adamrk
adamrk force-pushed the abk/deferred-host-task branch 2 times, most recently from a19ed7d to 1a73b4f Compare September 17, 2026 13:07
@adamrk
adamrk force-pushed the abk/deferred-host-task branch from 1a73b4f to dc76ae5 Compare September 17, 2026 15:14
@adamrk
adamrk marked this pull request as ready for review September 17, 2026 16:00
@adamrk
adamrk requested review from a team as code owners September 17, 2026 16:00
@adamrk
adamrk requested review from alexcrichton and dicej and removed request for a team September 17, 2026 16:00

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some great wins, thanks!

}
}
None => {
self.exit_call_not_concurrent();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a debug-assert on this branch that materialized_task is None?

debug_assert!(context.is_empty());
};
if let CurrentThread::DeferredHost(_) = thread {
state.deferred_host_call_context = Some(CallContext::default());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be moved into host_task_create? To the extent that set_thread is a somewhat-hot function and I think that's the only caller which would otherwise trigger this, it might help keep this function a bit leaner to move it over there. That might mess up and/or require moving around some debug assertions though

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, similarly, for handling deferred_host_call_context getting cleared above, could that move into the first_poll function or similar?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants