perf: bulk fixint array deserialization (6.5x per int[] array)#2255
Open
kodroi wants to merge 1 commit intoMessagePack-CSharp:masterfrom
Open
perf: bulk fixint array deserialization (6.5x per int[] array)#2255kodroi wants to merge 1 commit intoMessagePack-CSharp:masterfrom
kodroi wants to merge 1 commit intoMessagePack-CSharp:masterfrom
Conversation
Author
|
@dotnet-policy-service agree |
Add TryReadFixIntArray to MessagePackReader that reads positive fixint (0x00-0x7f) encoded int arrays in a single pass instead of calling ReadInt32 per element. Falls back to element-by-element on non-fixint data with zero overhead. Profiled with metreja (5 runs, inlining enabled): - Deserialize inclusive: -15% - ReadInt32 calls: 3.49M → 1.00M (-71%) - Per-array: ~6,200ns → ~952ns (6.5x faster for 100-element fixint array) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
396dd47 to
c9bf0cd
Compare
Collaborator
|
Thanks for submitting. |
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.
Summary
Add a fast path to
Int32ArrayFormatter.Deserializethat bulk-reads fixint-encoded int arrays in a single pass instead of callingReadInt32()per element.How it works
MessagePack encodes small integers (0–127) as a single byte called "positive fixint" — the byte value is the integer. The existing code deserializes
int[]by callingReadInt32()in a loop, which for each element: dispatches through format detection, decodes the value, and advances the reader.The new
TryReadFixIntArraymethod iterates the unread span once, validating each byte is ≤ 0x7f and copying it to the output array in the same loop. If any byte fails the check, it bails early and the caller falls back to the original element-by-elementReadInt32loop — the fallback overwrites any partial data.Fixint values (0–127) are common in int arrays: enum values, indices, counts, flags, IDs, and game state data.
Per-array cost (100-element fixint array)
ReadInt32()× 100TryReadFixIntArray()× 1Changes
MessagePackReader.cs— addinternal TryReadFixIntArray(Int32[] array)PrimitiveFormatter.cs— callTryReadFixIntArraybefore element-by-element fallback inInt32ArrayFormatter.DeserializeProfiling
Profiled with metreja using a 1M serialize+deserialize harness,
method_statsevents, inlining enabled. 5 runs for statistical confidence.metreja v1.0.20, macOS ARM64 (Apple M1 Pro), .NET 10.0.2
Test plan
🤖 Generated with Claude Code