Skip to content

perf(csvpp,csvpputil): cache reflection type info and optimize YAML encoding#11

Merged
osamingo merged 2 commits intomainfrom
perf/cache-reflection-type-info
Feb 6, 2026
Merged

perf(csvpp,csvpputil): cache reflection type info and optimize YAML encoding#11
osamingo merged 2 commits intomainfrom
perf/cache-reflection-type-info

Conversation

@osamingo
Copy link
Owner

@osamingo osamingo commented Feb 6, 2026

Summary

  • Cache reflection type info (sync.Map) in Marshal/Unmarshal following encoding/json pattern
  • Optimize YAML batch encoding with direct slice construction
  • Add WithYAMLCapacity option for pre-allocating internal buffer

Changes

csvpp package (reflection cache)

  • New file type_cache.go: typeInfo struct with sync.Map cache keyed by reflect.Type
  • marshal.go: Refactor buildFieldMap, encodeRecord, MarshalWriter to use cached type info. Remove redundant buildHeaders
  • export_test.go: Export CachedTypeInfo for testing
  • benchmark_test.go: Add 100-record Marshal/Unmarshal benchmarks

csvpputil package (YAML optimization)

  • yaml_array_writer.go: Add YAMLArrayWriterOption, WithYAMLCapacity, and encodeYAMLRecords for direct batch encoding in MarshalYAML/WriteYAML
  • yaml_array_writer_test.go: Add capacity hint tests
  • benchmark_test.go: Add WriteWithCapacity benchmark

Benchmark Results

Marshal/Unmarshal (reflection cache)

                  │   old    │           new            │
                  │  sec/op  │  sec/op    vs base       │
Marshal-12          1.810µ     1.173µ    -35.18%
Unmarshal-12        2.170µ     1.987µ     -8.44%

                  │   old    │           new            │
                  │ allocs   │  allocs   vs base        │
Marshal-12          36.00      30.00     -16.67%

YAML batch encoding

                  │    old     │          new            │
                  │   B/op     │   B/op    vs base       │
MarshalYAML-12      11.85Mi     11.82Mi   -0.28%
WriteYAML-12        11.76Mi     11.72Mi   -0.28%

Test Plan

  • GOEXPERIMENT=jsonv2 go test -race ./... all pass
  • Benchmark comparison with benchstat
  • Review public API backward compatibility (no changes)

🤖 Generated with Claude Code

osamingo and others added 2 commits February 6, 2026 18:43
- Add type_cache.go with sync.Map-based per-type cache (encoding/json pattern)
- Eliminate per-record tag scanning in encodeRecord by using cached field mappings
- Replace buildHeaders/buildFieldMap with cache lookups
- Add 100-record Marshal/Unmarshal benchmarks

Marshal: -35% latency, -17% allocs, -4% memory (3 records)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add encodeYAMLRecords for direct slice construction in MarshalYAML/WriteYAML
- Add WithYAMLCapacity option for pre-allocating internal buffer
- Add YAMLArrayWriterOption type following JSONArrayWriterOption pattern

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@osamingo osamingo self-assigned this Feb 6, 2026
@osamingo osamingo marked this pull request as ready for review February 6, 2026 09:44
@osamingo osamingo merged commit c0bb838 into main Feb 6, 2026
3 checks passed
@osamingo osamingo deleted the perf/cache-reflection-type-info branch February 6, 2026 09:45
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