go: optimize XDR decoding performance and reduce allocations #225
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.
This commit combines several optimizations to significantly reduce allocations and improve decode performance for generated Go code.
depends on stellar/go-xdr#21
API Changes
*xdr.Decoder(byte-slice based)(io.Reader, v)->([]byte, v)Optimizations
1. Byte-Slice Decoder Integration
Replace streaming Decoder with byte-slice based Decoder for direct decoding, providing simpler API and better performance.
2. Enum Validation
Replace map-based validation with faster alternatives:
3. Object Reuse
4. Union Value Types
Store small primitive union arms (≤8 bytes) as values instead of pointers to eliminate allocations during decode:
Larger types (fixed opaque, structs) remain as pointers to prevent memory bloat in unions with many arms.
5. Union Accessor Optimization
Replace ArmForSwitch + string comparison with direct discriminant enum comparison for ~16-23x faster accessor performance.
6. Float/Double and var_array Support
Add explicit encode/decode handling for Float/Double types and variable-length arrays of all primitive types.
Expected Impact
🤖 Generated with Claude Code