Skip to content

perf: expand Encode() type switch with more common types#73

Open
xe-nvdk wants to merge 2 commits into
v6from
perf/encode-type-switch
Open

perf: expand Encode() type switch with more common types#73
xe-nvdk wants to merge 2 commits into
v6from
perf/encode-type-switch

Conversation

@xe-nvdk

@xe-nvdk xe-nvdk commented Jun 12, 2026

Copy link
Copy Markdown
Member

Summary

Closes #60.

Adds fast paths to the Encode() type switch for int8/int16/int32, uint8/uint16/uint32, []string, and map[string]bool. Each avoids reflect.ValueOf boxing plus a getEncoder sync.Map lookup. Also deduplicates encodeMapStringBoolValue/encodeMapStringStringValue to delegate to the concrete encoders (same pattern as encodeStringSliceValue).

Benchmarks (Apple M3 Max, benchstat, count=8)

benchmark before after delta
EncodeInt8 12.03n 3.75n -68.9%
EncodeInt16 12.05n 3.85n -68.0%
EncodeInt32 12.58n 4.29n -65.9%
EncodeUint8 12.89n 3.75n -70.9%
EncodeUint16 12.30n 3.87n -68.6%
EncodeUint32 12.57n 4.30n -65.8%
EncodeStringSlice 38.16n 26.62n -30.2%
EncodeMapStringBool 65.24n 49.94n -23.5%

All p=0.000, zero allocs in both directions.

No regression on hot/fall-through paths

The larger switch was checked against hot encode types (string, int64, float64, time.Time, bool): all flat-to-marginally-faster (e.g. float64 -0.57%, time.Time -0.58%). StructMarshal and MapStringInterfaceMsgpack (default/reflection path) also flat. Added BenchmarkEncodeFallthrough to track the reflection fall-through cost going forward.

Testing

  • New TestEncodeTypeSwitchFastPaths: every fast-pathed type produces byte-identical output to the reflection path (EncodeValue), including nil/empty/min/max edge values.
  • New TestEncodeMapStringBoolSorted: SetSortMapKeys honored on the new map fast path, exact byte assertion.
  • New typeTests entries for map[string]bool, nil, and named mapStringBool (Convert path).
  • go test ./... and go test -race ./... pass.

Internal 4-agent review pass (correctness/security/quality/perf) completed; flagged switch-ordering concern measured and disproven, duplication finding fixed.

🤖 Generated with Claude Code

Ignacio Van Droogenbroeck added 2 commits June 12, 2026 14:34
Add fast paths for int8/int16/int32, uint8/uint16/uint32, []string,
and map[string]bool in the Encode() type switch. Each avoids
reflect.ValueOf boxing plus a getEncoder sync.Map lookup.

Benchmarks (Apple M3 Max, count=8, benchstat):

  EncodeInt8           12.030n ± 1%   3.745n ± 0%  -68.87%
  EncodeInt16          12.045n ± 0%   3.851n ± 0%  -68.03%
  EncodeInt32          12.575n ± 0%   4.288n ± 0%  -65.90%
  EncodeUint8          12.890n ± 0%   3.748n ± 0%  -70.93%
  EncodeUint16         12.300n ± 0%   3.866n ± 0%  -68.57%
  EncodeUint32         12.570n ± 0%   4.302n ± 0%  -65.78%
  EncodeStringSlice     38.16n ± 0%   26.62n ± 1%  -30.23%
  EncodeMapStringBool   65.24n ± 1%   49.94n ± 1%  -23.47%

Fall-through types (structs, map[string]interface{}) show no
regression from the larger switch.

Closes #60
- Deduplicate encodeMapStringBoolValue/encodeMapStringStringValue: both
  now delegate to the concrete encodeMapStringBool/encodeMapStringString
  after type extraction, matching the existing encodeStringSliceValue →
  encodeStringSlice pattern.
- Add BenchmarkEncodeFallthrough (named type that misses every switch
  case) so future switch growth that regresses the reflection
  fall-through is caught by benchmarks.
- Add typeTests coverage for map[string]bool, map[string]bool(nil), and
  a named mapStringBool (exercises the Convert path in
  encodeMapStringBoolValue).

Hot-type check (v6 vs branch, count=8): string -1.29%, int64 ~,
float64 -0.57%, time.Time -0.58%, bool -0.73% — the larger switch does
not regress hot encode paths.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces fast-path encoding for several types in the msgpack encoder, including int8, int16, int32, uint8, uint16, uint32, []string, and map[string]bool. It also refactors map encoding logic to reduce duplication and adds corresponding unit tests and benchmarks to verify correctness and performance. There are no review comments, and I have no additional feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

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.

perf: expand Encode() type switch with more common types

1 participant