Visitar URL original
perf: drop redundant char* fixed in MessagePackWriter.Write(string) by unsafePtr · Pull Request #2258 · MessagePack-CSharp/MessagePack-CSharp · GitHub
Skip to content

perf: drop redundant char* fixed in MessagePackWriter.Write(string) - #2258

Merged
AArnott merged 3 commits into
MessagePack-CSharp:masterfrom
unsafePtr:drop-redundant-fixed-writestring
Jul 8, 2026
Merged

AArnott merged 3 commits into
MessagePack-CSharp:masterfrom
unsafePtr:drop-redundant-fixed-writestring

Conversation

@unsafePtr

Copy link
Copy Markdown
Contributor

On SPAN_BUILTIN targets (netstandard2.1/net8.0/net9.0), use the span-based Encoding.GetBytes(ReadOnlySpan<char>, Span<byte>) overload so the fixed (char* pValue = value) pin can go away. The Encoding API pins the source itself for the duration of the encode, which is shorter than holding the pin across WriteString_PostEncoding.

Legacy targets (netstandard2.0/net472) keep the original fixed (char* …) path under #else.

The fixed (byte* pBuffer = &buffer) stays for now — WriteString_PostEncoding still takes byte* and uses Buffer.MemoryCopy for the overlapping prefix shift. Dropping that pin too would mean rewriting WriteString_PostEncoding to ref byte + MemoryMarshal.CreateSpan(...).CopyTo(...), but MemoryMarshal.CreateSpan is netstandard2.1+ only (not in the System.Memory backport for netstandard2.0/net472), so that's a bigger refactor for another PR.

Test plan

  • Builds clean on all 5 TFMs (netstandard2.0;netstandard2.1;net8.0;net9.0;net472)
  • MessagePack.Tests — 999/999 pass on net9.0, 968/968 on net472

@unsafePtr

Copy link
Copy Markdown
Contributor Author

@AArnott do I need to add any specific tests? Thought this change is straightforward

Comment thread src/MessagePack/MessagePackWriter.cs Resolved
Comment thread src/MessagePack/MessagePackWriter.cs Resolved
unsafePtr and others added 2 commits July 8, 2026 16:15
Co-authored-by: Andrew Arnott <andrewarnott@gmail.com>
Co-authored-by: Andrew Arnott <andrewarnott@gmail.com>
@unsafePtr
unsafePtr requested a review from AArnott July 8, 2026 13:15
@AArnott
AArnott merged commit 781513b into MessagePack-CSharp:master Jul 8, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants