perf(kv): skip merge of absent context/user/tenant maps in snapshot#138
Conversation
mergeMaps allocates a fresh map even for empty input, so every
snapshot() paid up to three map allocations for fields the error
chain never set. Consumers only ever check len() on these fields, so
leaving them nil is equivalent; ToMap's in-place "id" insertion now
guards against the nil case explicitly.
benchstat (darwin/arm64, Apple M3, -count=6 -cpu=1):
before after
ToMap 5.120Ki ± 0% 5.073Ki ± 0% -0.92% (p=0.002 n=6)
LogValue 5.264Ki ± 0% 5.217Ki ± 0% -0.89% (p=0.002 n=6)
ToMap 62.00 allocs 61.00 allocs -1.61% (p=0.002 n=6)
LogValue 60.00 allocs 59.00 allocs -1.67% (p=0.002 n=6)
Wall-clock deltas were not statistically significant on this run
(background noise on the benchmark machine), but the B/op and
allocs/op reductions are fully reproducible (0% variance, p=0.002).
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #138 +/- ##
==========================================
- Coverage 90.43% 90.24% -0.20%
==========================================
Files 15 15
Lines 1234 1250 +16
==========================================
+ Hits 1116 1128 +12
- Misses 93 95 +2
- Partials 25 27 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR reduces per-call allocations in the internal snapshot() path by avoiding unnecessary map merges when the error chain contains no context/user/tenant maps, while preserving existing outward behavior for ToMap, LogValue, and verbose formatting.
Changes:
- Skip
mergeMaps/lazyMapEvaluation/dereferencePointersforcontext/userData/tenantDatawhen no maps were collected during snapshotting. - Update
OopsError.ToMap()to safely handlenils.userData/s.tenantDatawhen only the corresponding ID was set, before inserting"id".
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| kv.go | Avoids map merge/allocation work in snapshot() when no context/user/tenant maps exist in the chain. |
| error.go | Guards ToMap()’s in-place "id" insertion against nil snapshot user/tenant maps. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
mergeMapsallocates a fresh map even for empty input, so everysnapshot()(called once perToMap/LogValue/formatVerbose) paid up to three map allocations forcontext/userData/tenantDatafields the error chain never set.len()on these fields, so leaving them nil when nothing was merged is equivalent.ToMap's in-place"id"insertion intos.userData/s.tenantDatanow guards against the nil case explicitly.Benchmarks
benchstat(darwin/arm64, Apple M3,-count=6 -cpu=1):Wall-clock deltas were not statistically significant on this run (background noise on the benchmark machine), but the
B/opandallocs/opreductions are fully reproducible (0% variance, p=0.002).Test plan
Verified with
go build ./...,go vet ./..., andgo test ./....