Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a compile-time feature for call hooks #8795

Merged
merged 2 commits into from
Jun 13, 2024

Conversation

alexcrichton
Copy link
Member

This commit moves the Store::call_hook API behind a Cargo feature named call-hook. This helps speed up the path from wasm into the host by avoiding branches at the start and the end of the execution. In a thread on Zulip this is locally leading to significant performance gains in this particular microbenchmark so having an option to disable it at the crate layer seems like a reasonable way to thread this needle for now. This definitely has a downside in that it requires a crate feature at all, but I'm not sure of a better solution as LLVM can't dynamically detect that Store::call_hook is never invoked and therefore the branch can be optimized away.

This commit moves the `Store::call_hook` API behind a Cargo feature
named `call-hook`. This helps speed up the path from wasm into the host
by avoiding branches at the start and the end of the execution. In a
thread on [Zulip] this is locally leading to significant performance
gains in this particular microbenchmark so having an option to disable
it at the crate layer seems like a reasonable way to thread this needle
for now. This definitely has a downside in that it requires a crate
feature at all, but I'm not sure of a better solution as LLVM can't
dynamically detect that `Store::call_hook` is never invoked and
therefore the branch can be optimized away.

[Zulip]: https://bytecodealliance.zulipchat.com/#narrow/stream/217126-wasmtime/topic/Performance.20regression.20since.20rust.201.2E65/near/444505571
@alexcrichton alexcrichton requested a review from a team as a code owner June 13, 2024 19:38
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team June 13, 2024 19:38
@alexcrichton alexcrichton added this pull request to the merge queue Jun 13, 2024
Merged via the queue into bytecodealliance:main with commit 3090ded Jun 13, 2024
36 checks passed
@alexcrichton alexcrichton deleted the gate-call-hook branch June 13, 2024 20:06
@pchickey
Copy link
Collaborator

pchickey commented Jun 13, 2024

I think this is the correct choice - call hooks fill a pretty particular need, and I'd even be fine if this feature was not enabled by default. Have we surveyed if anyone besides Fastly's embedding even makes use of it?

@alexcrichton
Copy link
Member Author

I haven't surveyed myself but carrying a Cargo feature for it isn't too bad. Most of our Cargo features have been relatively low overhead to maintain so far. The main question to me is that this might be an appropriate feature to be off-by-default instead of on-by-default as it can affect benchmarks, but even then it's pretty rare anyone benchmarks host-to-wasm calls or vice versa, most benchmarking is just of the wasm itself.

@tschneidereit
Copy link
Member

I think regardless of who might encounter this in benchmarks, as long it's not essentially always used, it makes sense to disable by default: we should keep the call overhead low wherever we can, after all, and requiring people to disable very specific features for best performance doesn't seem great to me.

@alexcrichton
Copy link
Member Author

I can try to investigate this a bit. I believe the gc feature has a much larger impact than call-hooks in terms of performance. I haven't bottomed that out but disabling gc-by-default I think would be much more significant than disabling call-hook by default.

@alexcrichton
Copy link
Member Author

Measuring locally from the benchmark from Zulip, which is very heavy in wasm->host calls and the host call itself does nothing, enabling the gc feature of Wasmtime yields a slowdown of 3.5x and enabling the call-hook feature slows down only 30%.

Put another way I don't think it's worth over-rotating too much on call-hook, gc is the main feature that's causing a slowdown.

@alexcrichton
Copy link
Member Author

With #8807 the performance is on-par now with the gc feature enabled, so I'll follow that up by disabling the call-hook feature by default.

alexcrichton added a commit to alexcrichton/wasmtime that referenced this pull request Jun 14, 2024
This commit disables the `call-hook` feature for the Wasmtime crate
added in bytecodealliance#8795 by default. The rationale is that this has a slight cost
to all embeddings even if the feature isn't used and it's not expected
to be that widely used of a feature, so off-by-default seems like a more
appropriate default.
alexcrichton added a commit to alexcrichton/wasmtime that referenced this pull request Jun 14, 2024
This commit disables the `call-hook` feature for the Wasmtime crate
added in bytecodealliance#8795 by default. The rationale is that this has a slight cost
to all embeddings even if the feature isn't used and it's not expected
to be that widely used of a feature, so off-by-default seems like a more
appropriate default.
github-merge-queue bot pushed a commit that referenced this pull request Jun 14, 2024
* Disable `call-hook` crate feature by default

This commit disables the `call-hook` feature for the Wasmtime crate
added in #8795 by default. The rationale is that this has a slight cost
to all embeddings even if the feature isn't used and it's not expected
to be that widely used of a feature, so off-by-default seems like a more
appropriate default.

* Enable all features in doc build

* More doc fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants