Port span-based Encoding.GetBytes to Write(string) and Write(ReadOnlySpan<char>) (#1004) - #1008
Merged
Conversation
AArnott
commented
Jul 8, 2026
Owner
- Port MessagePack-CSharp PR #2258: use span-based Encoding.GetBytes in Write(string) and Write(ReadOnlySpan)
- Remove unnecessary .AsSpan() in Write(string) - string implicitly converts to ReadOnlySpan
- Fix legacy path buffer size: use bufferSize - useOffset in GetBytes calls
…Span<char>) (#1004) * Port MessagePack-CSharp PR #2258: use span-based Encoding.GetBytes in Write(string) and Write(ReadOnlySpan<char>) Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com> * Remove unnecessary .AsSpan() in Write(string) - string implicitly converts to ReadOnlySpan<char> Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com> * Fix legacy path buffer size: use bufferSize - useOffset in GetBytes calls Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates MessagePackWriter’s string-writing paths to use span-based Encoding.GetBytes where available, and corrects the legacy unsafe encoding call to respect the available destination buffer length when an offset is applied.
Changes:
- Use span-based
Encoding.GetBytesinWrite(string)andWrite(ReadOnlySpan<char>)under#if NET. - Remove unnecessary
fixed (char*)usage on the modern path while keeping the pointer-based fallback for older TFMs. - Fix destination buffer sizing in the legacy pointer-based
GetBytescall by usingbufferSize - useOffset.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.