-
Notifications
You must be signed in to change notification settings - Fork 48
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
Use unchecked function callbacks for better performance #186
Merged
peterhuene
merged 7 commits into
bytecodealliance:main
from
kpreisser:generic-define-function-unchecked
Nov 28, 2022
Merged
Use unchecked function callbacks for better performance #186
peterhuene
merged 7 commits into
bytecodealliance:main
from
kpreisser:generic-define-function-unchecked
Nov 28, 2022
Conversation
This file contains 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
This is a follow-up to PR bytecodealliance#163. This also improves the NRT annotations for callback delegates, and fixes an regression that prevented to define callbacks taking or returning an interface.
kpreisser
changed the title
Use unchecked functions with raw values for better performance
Use unchecked functions for callbacks for better performance
Nov 12, 2022
…tter performance.
kpreisser
commented
Nov 14, 2022
…behavior of the Rust implementation (and this doesn't seem to affect performance).
This allows callbacks that don't use a Caller, Function or object parameter to be allocation-free.
…so that allocating a Caller is now only necessary when unboxing Functions.
kpreisser
changed the title
Use unchecked functions for callbacks for better performance
Use unchecked function callbacks for better performance
Nov 21, 2022
peterhuene
approved these changes
Nov 28, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kpreisser thanks for all this work! I paid close attention to the externref/funcref raw value conversion and it seems correct to me.
kpreisser
added a commit
to kpreisser/wasmtime-dotnet
that referenced
this pull request
Dec 3, 2022
…rmance. This applies the changes to function callbacks from bytecodealliance#186 also to the other side, where wasm functions are wrapped with Function.Wrap(). Note that Function.Invoke() still uses the regular Value.
peterhuene
pushed a commit
that referenced
this pull request
Dec 8, 2022
…rmance (#194) * Use unchecked function calls (for wrapped functions) for better performance. This applies the changes to function callbacks from #186 also to the other side, where wasm functions are wrapped with Function.Wrap(). Note that Function.Invoke() still uses the regular Value. * Follow-Up: Add the IsNull check to WrapAction/WrapFunc, and remove an unnecessary check in Invoke(). * Add a comment explaining the differences in return type handling compared to e.g. Function.FromCallback(). Also, add a missing CultureInfo.InvariantCulture, and fix formatting. * Follow-Up: Make calling the function safer by always using the maximum of the parameter count and Results.Count for the stackalloc, regardless of what overload was used. * Follow-Up: Simplify.
Closed
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.
Hi,
this PR implements unchecked function variants (bytecodealliance/wasmtime#3350) for the generic callback overloads added by #163, which improves performance.
This adds a
ValueRaw
struct that maps towasmtime_val_raw_t
and anIValueRawConverter<T>
interface similar to the existing ones, which are then used by the unchecked callback functions.Since the .NET side knows the exact parameter and result types used by the callback, this should be safe. (However I'm not very familiar with wasmtime's resource management, so I would appreciate if you can review especially the handling in the
FuncRefValueRawConverter
andGenericValueRawConverter
.)This also improves the NRT annotations for callback delegates (note that for
<auto-generated>
files, we must explicitely add#nullable enable
even if the project already enables it), and fixes a regression that prevented to define callbacks using an interface as generic type parameter (because that is currently not allowed byValueBox.Converter<T>()
, but it is allowed byValue.TryGetKind()
).Additionally, callbacks that use neither
Caller
norFunction
as parameters are now allocation-free when being called, since theCaller
instance will no longer be created as it isn't needed in these cases.When trying to run the benchmarks from #163 (but using 6 instead of 5 iterations) with .NET 6.0.11 on Windows 10 (Build 19044) x64, I get the following results:
Benchmark 1 (
Action<int>
):Without this PR:
With this PR:
Benchmark 2 (
Action<int, float, long>
):Without this PR:
With this PR:
Benchmark 3 (
Func<int, float, long, ValueTuple<int, int, long>>
):Without this PR:
With this PR :
With the last benchmark, this seems to be roughly a 6x improvement comparing to the current state (without this PR).
What do you think?
Thanks!