perf: expand Encode() type switch with more common types#73
Open
xe-nvdk wants to merge 2 commits into
Open
Conversation
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.
There was a problem hiding this comment.
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.
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
Closes #60.
Adds fast paths to the
Encode()type switch forint8/int16/int32,uint8/uint16/uint32,[]string, andmap[string]bool. Each avoidsreflect.ValueOfboxing plus agetEncodersync.Map lookup. Also deduplicatesencodeMapStringBoolValue/encodeMapStringStringValueto delegate to the concrete encoders (same pattern asencodeStringSliceValue).Benchmarks (Apple M3 Max, benchstat, count=8)
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%).
StructMarshalandMapStringInterfaceMsgpack(default/reflection path) also flat. AddedBenchmarkEncodeFallthroughto track the reflection fall-through cost going forward.Testing
TestEncodeTypeSwitchFastPaths: every fast-pathed type produces byte-identical output to the reflection path (EncodeValue), including nil/empty/min/max edge values.TestEncodeMapStringBoolSorted:SetSortMapKeyshonored on the new map fast path, exact byte assertion.map[string]bool, nil, and namedmapStringBool(Convert path).go test ./...andgo 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