-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 ArrayPool in CSTRMarshaler.ConvertToNative #113991
Conversation
@@ -103,13 +104,19 @@ internal static unsafe IntPtr ConvertToNative(int flags, string strManaged, IntP | |||
// wasting memory on systems with multibyte character sets where the buffer we end up with is often much | |||
// smaller than the upper bound for the given managed string. | |||
|
|||
byte[] bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, | |||
fBestFit: 0 != (flags & 0xFF), fThrowOnUnmappableChar: 0 != (flags >> 8), out nb); | |||
byte[] bytes = ArrayPool<byte>.Shared.Rent(checked((strManaged.Length + 1) * Marshal.SystemMaxDBCSCharSize)); |
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.
All of the marshallers in this file are quite legacy. There are many other opportunities to update.
I'd suggest to not touch it.
Looks like this change is triggering a JIT bug: https://helix.dot.net/api/2019-06-17/jobs/0e6c868f-9ce9-4ad2-8e79-f61acd524e57/workitems/System.Security.Cryptography.OpenSsl.Tests/console
cc @dotnet/jit-contrib |
What's the signature you have encountered it with? Have you considered switching to LibraryImport to fix the problem? How does LibraryImport perf compare to what you get with this fix for the signature you have hit the problem with? |
Tagging subscribers to this area: @dotnet/interop-contrib |
Thank you for We are trying to reduce GC count/pause during UI drawing and the temporary byte arrays during p/invoke are the top most garbage creation in |
Updating main branch would not improve .NET 6 environment. Such performance change typically doesn't meet the bar for backport. Moreover, 6.0 has reached EOL. If you provide your build, you can do update in your branch. |
We'll use .NET 10 next year (hopefully) |
At that point then |
Closing, as we are unlikely to take any changes here. |
Thank you for the time and consideration. |
This reduces temporary byte array creation during p/invoke string param marshaling.