-
Notifications
You must be signed in to change notification settings - Fork 197
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
C#: use stackalloc
where appropriate for caller-allocated buffers
#1012
Comments
I looked into this, my concern with using @pavelsavara Maybe wit-bindgen/crates/csharp/src/function.rs Lines 856 to 869 in 953b183
|
But it would only work if the current stack frame is alive during the call to the external function (WASI import). Can we calculate the size of the buffer during code gen ? And if the payload size is dynamic, we can also generate the condition and both branches into C# code. The 3rd option is to pin the original array and pass it's address without copying it.
@AaronRobinsonMSFT and his team have lot of experience doing this. They also understand backward compatibility consequences, although this looks like implementation detail right now. Also note that |
Maybe until we get to WASI p3 or 1.0, this is premature optimization. |
@pavelsavara Thanks for including me on this.
That is correct. We address this by having the marshaller accept a buffer (typically Reference our design docs for other considerations. /cc @jkoritzinsky
Yep, see the interop team's pattern in the response above. You will need to experiment with the size. It is too easy to exceed the stack and cause all sorts of issues and performance cliffs when performing interop.
At the interop boundary this should be a general requirement, but I'm not sure if it is practical in the WASI world.
+1
I'm not sure how this would solve the interop issue considering if it is needed, how would unmanaged code leverage the GCHandle contents?
Agree. I would consider handling this in a stack allocated |
This particular payload size is dynamic, so the we should use a pattern like Utf8StringMarshaller.
Awesome, We using a variety of all these patterns. See for the latest #1138
You're right this won't work for this particular scenario since the memory needs to be layed out according to the ABI, it does work with the primative/canonical types.
I'll open an issue to clean this up |
From that PR:
This is slightly misleading. Yes it isn't reallocating the object, but it is allocating a GCHandle, which is from a special resource pool that can create a similar, but different, kind of GC pressure.
I'd be curious to see why this is needed. Why isn't the |
👍 makes sense, I'll adjust the comment to be more specific
For instance, if you have a wit function with some parameters that are lists that has variants |
I may have misinterpreted the last message. Where you saying why use |
Per dotnet/runtimelab#2614 (comment), using
stackalloc
would likely be more efficient and avoid the need for GC pinning.The text was updated successfully, but these errors were encountered: