-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
.NET 5.0+ Marshal.PtrToStructure is slower 8x than netcore3.1 #45100
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Note to whoever looks into this - the PR #32520 will impact |
This regression was caused by dotnet/coreclr#26340. That PR improved performance in the P/Invoke and Reverse P/Invoke cases, but we didn't realize until recently that it regressed performance for the Marshal APIs. Generally for best performance in interop scenarios, we recommend using blittable types when possible. In any case, I'll investigate and see if there's a way we can get this performance loss back in .NET 6. |
@sunliusi Unexpectedly in C# it is type safer to use pointers , also it makes faster code since it doesn't involve copying which Marshal methods do. |
It is more efficient to use Pointers directly. here is my code:
netcore3.1 Benchmark test result:
net5.0 Benchmark test result:
|
I think given we have a best-practice and there was a reason for other more common scenarios to be improved we can defer this for now. |
I suspect it is because we are creating a new instance via the runtime/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs Lines 529 to 555 in 0419950
Can you provide a benchmark which affects the generic overload PtrToStructure<T> .
|
The performance issue is real. We have relied on the performant mapping of bytes to structs with MarshalAs/SizeConst for quite some time, with just under 100 hits throughout the code base. Please find the time to resurrect the speed. |
I would second that |
For the best performance avoiding this API is recommended. As noted in #45100 (comment), it is far more efficient to fallback to pointers if needed or ideally switch over to This isn't to say the issue isn't real, it very much is a regression. The push back is based on the benefits that were gained in the other scenarios as noted in #45100 (comment). Understanding why teams cannot move to blittable types or |
It would be superior if we could use VS to convert existing structs into |
It can. @jkoritzinsky has been working on a source generator for producing custom struct marshallers. Work is being done for a COM source generator right now. After that, having support for a source generator solution is possible, especially with user request. Feel free to file a new issue in this repo for that feature. /cc @agocke @dotnet/interop-contrib |
This change adds a simple cache for the marshalling stubs. This cache improves performance of the test mentioned in the issue by 20+x. Fixes dotnet#45100
Here is my test code:
netcore3.1 Benchmark test result:
net5.0 Benchmark test result:
os: windows 10
The text was updated successfully, but these errors were encountered: