Skip to content
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

Implement stateful, incremental skip #156

Merged
merged 2 commits into from
Dec 10, 2024
Merged

Implement stateful, incremental skip #156

merged 2 commits into from
Dec 10, 2024

Conversation

AArnott
Copy link
Owner

@AArnott AArnott commented Dec 10, 2024

This is a perf optimization for streaming scenarios where a converter needs to skip a structure but the buffer may not have the entire structure in memory. This optimization records how much of the structure has been skipped already so that the reader can advance, freeing memory, and resume skipping when we get more bytes to decode.

In particular, this avoids the previous behavior that skipping had to be restarted from the beginning of the structure each time new bytes are brought in until the whole structure was in memory together.

While this is theoretically a memory use improvement (for skipping scenarios only), this is mostly about avoiding CPU work to parse partial structures repeatedly until we get the whole thing in memory. With this change, at most one msgpack token (rather than whole structure) will be decoded more than once, and that limited to only once per fetch of new bytes.

Closes #154

@AArnott AArnott requested a review from Copilot December 10, 2024 21:32
@AArnott AArnott enabled auto-merge December 10, 2024 21:33
@AArnott AArnott disabled auto-merge December 10, 2024 21:33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no suggestions.

Comments skipped due to low confidence (1)

src/Nerdbank.MessagePack/MessagePackStreamingReader.cs:804

  • The word 'self' should be 'this'.
// We don't actually expect to ever hit self point, since every code is supported.
This is a perf optimization for streaming scenarios where a converter needs to skip a structure but the buffer may not have the entire structure in memory. This optimization records how much of the structure has been skipped already so that the reader can advance, freeing memory, and resume skipping when we get more bytes to decode.

In particular, this avoids the previous behavior that skipping had to be restarted from the beginning of the structure each time new bytes are brought in until the whole structure was in memory together.

While this is theoretically a memory use improvement (for skipping scenarios only), this is mostly about avoiding CPU work to parse partial structures repeatedly until we get the whole thing in memory. With this change, at most one msgpack *token* (rather than whole structure) will be decoded more than once, and that limited to only once per fetch of new bytes.

Closes #154
@AArnott AArnott changed the title Fix MessagePackStreamingReader.TrySkip to skip the whole structure … Implement stateful, incremental skip Dec 10, 2024
@AArnott AArnott added the enhancement New feature or request label Dec 10, 2024
@AArnott AArnott requested a review from Copilot December 10, 2024 22:36
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 10 changed files in this pull request and generated 1 suggestion.

Files not reviewed (5)
  • src/Nerdbank.MessagePack/net8.0/PublicAPI.Unshipped.txt: Language not supported
  • src/Nerdbank.MessagePack/netstandard2.0/PublicAPI.Unshipped.txt: Language not supported
  • samples/CustomConverters.cs: Evaluated as low risk
  • src/Nerdbank.MessagePack/Converters/ObjectMapConverter`1.cs: Evaluated as low risk
  • src/Nerdbank.MessagePack/Converters/ObjectMapWithNonDefaultCtorConverter`2.cs: Evaluated as low risk

@AArnott AArnott merged commit 2987c94 into main Dec 10, 2024
2 checks passed
@AArnott AArnott deleted the fixStreamingSkip branch December 10, 2024 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stateful (incremental) skip
1 participant