Component hostcall optimizations#13876
Open
tschneidereit wants to merge 8 commits into
Open
Conversation
Splitting current_thread into an #[inline] fast path plus #[cold] outlined deferred-promotion slow path reduces call overhead by about 20% (measured in a Linux VM on an M5 Max MBP): sync calls go from about 83ns to about 65ns, immediately ready async calls from 142ns to 117ns.
Precompute `TypeFunc::contains_borrow` at compile time and use it in the hostcall entrypoint to skip `CallContext` scope push/pop and `validate_scope_exit` when lending is statically impossible. Relative to the previous commit, this reduces call overhead by about 29% for sync calls, and about 9% for async calls (both measured in a Linux VM on an M5 Max MBP): sync calls go from about 65ns to about 46ns, immediately ready async calls from 117ns to 106ns. I'm not entirely sure why the win is so much less for async calls, but there are more wins in the next commits.
Such calls now run as the caller's guest thread without creating a pending future at all: `poll_and_block` polls the host future once before materializing anything and returns ready results directly. `host_task_reenter_caller` becomes a no-op when no task was created. Async-lowered calls keep the eager task for subtask status/cancellation), and borrow-ful calls keep it as scope identity. Relative to the previous commit, this reduces call overhead by almost another 60% (46ns -> 19ns) for sync calls, with async calls, expectedly, stay unchanged.
Specifically, if no borrows need to be tracked, no scope needs to be resolved. Plus, inline a bunch of single-instantiation generics. Relative to the previous commit, this reduces call overhead by another 31% for sync calls (19ns to 13ns), with async calls unchanged (both measured in a Linux VM on an M5 Max MBP).
A ready async-lowered call returns Status::Returned with no waitable, so the guest cannot observe the task's absence. first_poll now polls the raw future before creating anything, only pending futures get in `JoinHandle::run` and materialize a task. This only works for borrow-free calls, because for borrows the task is still needed for scope tracking. Relative to the previous commit, sync calls stay unchanged, but async calls are improved by about 45%: 109ns to 60ns. (measured in a Linux VM on an M5 Max MBP)
Polls the result future once in-place and skips creating a `HostTask`, etc if it's ready immediately. Reduces costs of immediately-ready async calls by another 12% (60.5ns to 53ns)
tschneidereit
force-pushed
the
hostcall-opts
branch
from
July 14, 2026 13:41
77c8bce to
8da58dc
Compare
Member
|
Thanks! Could this be split up to land each commit independently? Some of these I'm more sure about than others and might take more time to review |
Member
Author
|
Happy to do that, yes! I might in that case also restructure some of them a little bit to bring in some borrows-related fixes which change some of the new code again. |
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.
This set of commits applies a bunch of independent optimizations to calling host imports from components. In combination, they reduce overhead of sync calls by about 84%, from 83ns to 13ns, and that of immediately-ready async host imports by about 62%, from 142ns to 53ns. All numbers measured in a Linux VM on an M5 Max MBP, but I don't think any of this is particularly architecture-specific.
A few of notes on the results:
All commits can be reviewed independently and come with improvement numbers relative to the previous commit.