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

Potential Perf Gain: Use LibraryImport instead of DllImport when using .NET 7 #105

Open
nikeee opened this issue Jan 4, 2023 · 0 comments

Comments

@nikeee
Copy link
Contributor

nikeee commented Jan 4, 2023

When using .NET 7, there is a new attribute that can be used to mark p/invokes:
https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation

The API is kinda similar:

[DllImport(
    "nativelib",
    EntryPoint = "to_lower",
    CharSet = CharSet.Unicode)]
internal static extern string ToLower(string str);

becomes

[LibraryImport(
    "nativelib",
    EntryPoint = "to_lower",
    StringMarshalling = StringMarshalling.Utf16)]
internal static partial string ToLower(string str);

The key difference is that the code used to marshal the parameters is emitted during compile-time instead of run-time. This is faster but also opens the ability to do native AOT compilations.

I think d2dlib uses .NET 6, so this might not be relevant yet. But it might be for the next major LTS version.

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

1 participant