-
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
StringMarshaller optimizations #69035
Conversation
jkotas
commented
May 8, 2022
- Make the StringMarshallers smaller. They do not need to store the full stackallocated Span.
- Use the stack allocated buffer more often.
- Other fixes for consistency
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @dotnet/interop-contrib Issue Details
|
...stem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf16StringMarshaller.cs
Show resolved
Hide resolved
src/libraries/System.Runtime.InteropServices/ref/System.Runtime.InteropServices.cs
Show resolved
Hide resolved
fixed (char* ptr = str) | ||
// Calculate accurate byte count when the provided stack-allocated buffer is not sufficient | ||
maxByteCount = Encoding.UTF8.GetByteCount(str) + 1; | ||
if (maxByteCount > buffer.Length) |
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.
This makes the marshaller faster for ascii strings of lengths between 85 and 256. The cost of calculating the exact buffer length is less than the cost of going through the Alloc/Free path.
_allocated = (byte*)Marshal.AllocCoTaskMem(maxByteCount); | ||
int byteCount; | ||
fixed (char* ptr = str) | ||
// Calculate accurate byte count when the provided stack-allocated buffer is not sufficient |
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.
The built-in marshallers use exact-sized buffers for some cases and conservatively-sized buffer for other cases. For example, check
runtime/src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
Lines 94 to 96 in 041933f
// Otherwise we use a slower "2-pass" mode where we first marshal the string into an intermediate buffer | |
// (managed byte array) and then allocate exactly the right amount of unmanaged memory. This is to avoid | |
// wasting memory on systems with multibyte character sets where the buffer we end up with is often much |
I think we should just do the exact-sized buffers everywhere. The long strings are rare. When the long strings do show up, the 3x over-allocation for them feels like a lot.
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.
I am wondering whether it would make sense to fix the Marshal
methods to do the exact-sized allocations as well.
This reduces the utf8 string marshaling overhead for a method with a single "Hello" argument by about 10%. #69031 has suggestion for how this can be optimized further. |
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs
Show resolved
Hide resolved
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf8StringMarshaller.cs
Show resolved
Hide resolved
...stem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf16StringMarshaller.cs
Show resolved
Hide resolved
- Make the StringMarshallers smaller. They do not need to store the full stackallocated Span. - Use the stack allocated buffer more often. - Other fixes for consistency
…pServices/Marshalling/Utf16StringMarshaller.cs Co-authored-by: Aaron Robinson <arobins@microsoft.com>