Skip to content

Conversation

@tamirms
Copy link
Contributor

@tamirms tamirms commented Jan 6, 2026

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

  • DecodeFrom signature: now uses *xdr.Decoder (byte-slice based)
  • Generated Unmarshal function: (io.Reader, v) -> ([]byte, v)
  • Remove generated UnmarshalWithOptions wrapper (callers can use xdr.NewDecoderWithOptions directly for custom options)

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:

  • Contiguous enums: range check (v >= min && v <= max) - 11x faster
  • Non-contiguous enums: switch statement - 4x faster

3. Object Reuse

  • Union pointer reuse: nil check before allocating arm pointers
  • Slice capacity preservation: reuse existing capacity, grow-only

4. Union Value Types

Store small primitive union arms (≤8 bytes) as values instead of pointers to eliminate allocations during decode:

  • bool, int32, uint32, int64, uint64, float32, float64
  • Enums and typedefs resolving to primitives

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

  • Steady-state allocations: ~107K -> ~4-5K per decode cycle
  • Enum validation: 4-11x faster
  • Union accessors: 16-23x faster

🤖 Generated with Claude Code

@tamirms tamirms force-pushed the xdr-decoder-interface branch 2 times, most recently from 4dafd54 to 3b175b1 Compare January 8, 2026 19:31
Performance optimizations for Go XDR decoding:
- Use byte slice Decoder instead of io.Reader for zero-copy decoding
- Preserve slice capacity during decoding (grow-only, no shrinking)
- Handle optional types without unnecessary allocations
- Union arms with primitive types decode directly into value fields

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@tamirms tamirms force-pushed the xdr-decoder-interface branch from 3b175b1 to 34f3187 Compare January 8, 2026 19:38
@tamirms tamirms marked this pull request as draft January 8, 2026 19:38
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