Skip to content

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

Open
unsafePtr wants to merge 1 commit into
MessagePack-CSharp:masterfrom
unsafePtr:drop-redundant-fixed-writestring
Open

perf: drop redundant char* fixed in MessagePackWriter.Write(string)#2258
unsafePtr wants to merge 1 commit into
MessagePack-CSharp:masterfrom
unsafePtr:drop-redundant-fixed-writestring

Conversation

@unsafePtr

Copy link
Copy Markdown

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
Author

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

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.

1 participant