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

API: Consider Replacing Marshal.Read & Array Marshalling #1

Open
Sewer56 opened this issue Jul 25, 2022 · 1 comment
Open

API: Consider Replacing Marshal.Read & Array Marshalling #1

Sewer56 opened this issue Jul 25, 2022 · 1 comment

Comments

@Sewer56
Copy link

Sewer56 commented Jul 25, 2022

Consider refactoring usage of Marshal.Read* and Marshal.Write* with either raw pointers or Unsafe.Read & Unsafe.Write.
For legacy reasons, the Marshal classes seem to include exception handling for bad addresses; and in .NET the inclusion of exception handling will prevent your code from being inlined.

SharpLab sample to demonstrate.


public delegate int ReadFile(IntPtr[] resultStructure, string path);

Marshalling arrays causes unnecessary heap allocations, consider using raw pointer + count, and then creating Span<T>.

@Sewer56 Sewer56 changed the title API: Consider Replacing Marshal.Read API: Consider Replacing Marshal.Read & Array Marshalling Jul 25, 2022
@UnderMybrella
Copy link
Member

Ahh, ty for that. I'm still getting familiar with C# so thanks for the improvement!

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

No branches or pull requests

2 participants