Simplify Decoder API to use byte slices#21
Draft
tamirms wants to merge 2 commits into
Draft
Conversation
ff794e4 to
82e681b
Compare
Member
Would this mean that it's no longer possible to stream decode some XDR types? I'm thinking of a bucket which is a stream of framed bucket entries. How would I decode bucket entries with it being a byte slice? Does that mean the entire bucket has to be loaded into memory in a byte slice? There may be some cases where it would be preferred to still be able to use a reader. |
Member
Do we understand what's causing this improvement? I'm wondering if this improvement a result of removing the decoder interface, or if it's to do with buffering. Because we can do buffered reads using the |
This refactors the Decoder to work directly with byte slices instead of io.Reader for improved performance, and simplifies the DecoderFrom interface. API Changes: - Decoder now takes []byte instead of io.Reader - Unmarshal/UnmarshalWithOptions take []byte instead of io.Reader - Add DecoderFrom interface for types to implement fast-path decoding - Add Decoder methods: Reset(), Remaining(), Position(), MaxDepth() - Remove MaxInputLen from DecodeOptions (now uses Remaining()) - Remove redundant maxDepth parameter from DecoderFrom interface Depth Tracking: - Add EnterScope()/LeaveScope() methods for stateful depth tracking - Add currentDepth field to Decoder struct - Allows generated code to track recursion depth across nested DecodeFrom calls without passing maxDepth as a parameter Performance: - Eliminate io.Reader overhead with direct buffer access Bug Fixes: - Fix setUnionArmsToNil to nil individual fields instead of whole struct - Support value-type union arms in encode/decode (not just pointers) - Fix error messages to show actual bytes written on partial writes Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Change DecoderFrom interface to accept maxDepth parameter directly, eliminating the need for EnterScope/LeaveScope methods and the currentDepth field in Decoder. This reduces overhead by: - Removing defer calls for LeaveScope - Eliminating struct field access for depth tracking - Enabling better inlining opportunities Implementations should decrement maxDepth when calling DecodeFrom on nested types and return an error if maxDepth reaches 0. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
7 tasks
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
Key API Changes
New Decoder Reuse Pattern
Benchmarks show ~10% improvement with decoder reuse.
Breaking Changes
Alternatively, I could introduce these changes in an xdr4 package to avoid breaking changes. However, I think we are the only users of this library and I preferred to remove existing code to make the codebase easier to maintain. But I am open to reconsidering this decision.