Skip to content

Conversation

@jkotas
Copy link
Member

@jkotas 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

@ghost ghost assigned jkotas May 8, 2022
@ghost
Copy link

ghost commented May 8, 2022

Note regarding the new-api-needs-documentation label:

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.

@ghost
Copy link

ghost commented May 8, 2022

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details
  • 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
Author: jkotas
Assignees: jkotas
Labels:

area-System.Runtime.InteropServices, new-api-needs-documentation

Milestone: -

Copy link
Member Author

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.

Copy link
Member Author

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

// 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.

Copy link
Member Author

@jkotas jkotas May 8, 2022

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.

@jkotas
Copy link
Member Author

jkotas commented May 8, 2022

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.

jkotas and others added 4 commits May 10, 2022 18:29
- 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>
@jkotas jkotas force-pushed the string-marshaling branch from 98397db to e0daa5d Compare May 11, 2022 01:32
@jkotas jkotas merged commit 130ef3c into dotnet:main May 11, 2022
@jkotas jkotas deleted the string-marshaling branch May 11, 2022 15:41
@jkotas jkotas added the tenet-performance Performance related issue label May 12, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants