-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Provide API to reduce string allocations in user-defined custom converters #183
Comments
CC: @trippwill |
In adding support for this to vs-streamjsonrpc, we have a many instances of this pattern for CommonString like if (!reader.TryReadStringSpan(out ReadOnlySpan<byte> valueBytes))
{
// TODO: More specific exception type
throw new NBMP::MessagePackSerializationException(Resources.UnexpectedErrorProcessingJsonRpc);
}
// Recognize "2.0" since we expect it and can avoid decoding and allocating a new string for it.
if (Version2.TryRead(valueBytes))
{
return Version2.Value;
} |
Am I reading this right? It looks bad: if (!reader.TryReadStringSpan(out ReadOnlySpan<byte> valueBytes))
{
// TODO: More specific exception type
throw new NBMP::MessagePackSerializationException(Resources.UnexpectedErrorProcessingJsonRpc);
}
|
This is the code path from MessagePackFormatter. If we can't get the string as a span of bytes, there's nowhere to go: ReadOnlySpan<byte> valueBytes = MessagePack.Internal.CodeGenHelpers.ReadStringSpan(ref reader);
if (Version2.TryRead(valueBytes))
{
return Version2.Value;
} |
That one is fine, because if the string isn't contiguous, it'll allocate memory and make it contiguous. It's the |
See the
CommonString
struct defined in vs-streamjsonrpc (like this one) which currently uses MessagePack-CSharp APIs. We should expose equivalent APIs from this library, and/or expose aCommonString
-like struct so that the user doesn't have to write it.The text was updated successfully, but these errors were encountered: