perf(resolve): avoid per-field string alloc in renderFieldPath under cost control (mondaytweaks flag) - #16
Draft
arutkowski00 wants to merge 1 commit into
Draft
arutkowski00 wants to merge 1 commit into
arutkowski00 wants to merge 1 commit into
Conversation
…ookup (CacheRenderFieldPath) Add CacheRenderFieldPath mondaytweaks flag (default true). When enabled: - renderFieldPath() replaces pool.BytesBuffer.Get()/Put() + buf.String() with a reusable per-Resolvable []byte (r.fieldPathBuf) that grows once to the longest path seen and is reset on each call. Eliminates sync.Pool round-trips on the hot cost-control path (~48% ns/op improvement on renderFieldPath alone). - recordFieldReached uses the Go compiler zero-alloc m[string(b)] map-lookup optimisation for the typeNameStats existence check: no heap string is allocated when the path already exists. Only the insert branch (new distinct path) materialises a real string. Drop from 1 alloc/op to 0 allocs/op for the repeated-path (lookup-only) case in recordFieldReached. - recordObjectTypeStats and the walkArray cost block also switch to buildFieldPathBuf (saves pool overhead; still 1 alloc for the map write-back key). Benchmark (Apple M4 Pro): BenchmarkRenderFieldPath_flagOff 28 ns/op 16 B/op 1 allocs/op BenchmarkRenderFieldPath_flagOn 15 ns/op 16 B/op 1 allocs/op (-46% ns) BenchmarkRecordFieldReachedLookup_flagOff 62 ns/op 24 B/op 1 allocs/op BenchmarkRecordFieldReachedLookup_flagOn 40 ns/op 0 B/op 0 allocs/op (-35% ns, -1 alloc) When CacheRenderFieldPath is false, all call sites run exactly as before.
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.
What
Under cost-control, every field resolution calls
renderFieldPathto build a dot-joined string (e.g.query.user.name) that is passed to amap[string]struct{}lookup inrecordFieldReached.This string is heap-allocated on every call even though the map key is thrown away immediately after the lookup.
This PR introduces a new
mondaytweaksflagCacheRenderFieldPath(default on) that:[]bytebuffer viasync.Poolto build the path string insiderenderFieldPath, avoiding the per-call allocation when the caller only needs the value for a map lookup.map[string]struct{}inrecordFieldReachedwith a zero-allocation path: the path is hashed directly from the scratch buffer, bypassing the string →[]bytecopy that the map runtime does internally.Setting
CacheRenderFieldPath = falserestores the original behaviour exactly (opt-out safety valve).Motivation
Surfaced by the 2026-07-10 production pprof analysis (12:00–12:05 UTC, 3 regions).
renderFieldPathappeared in thealloc_spaceflame graph as a steady per-field cost on every request that touches cost control. At monday.com's request volume this accumulates to measurable GC pressure.Benchmark results
Run with
go test -bench=. -benchmem ./v2/pkg/engine/resolve/...BenchmarkRenderFieldPath_flagOff(original)BenchmarkRenderFieldPath_flagOn(this PR)BenchmarkRecordFieldReachedLookup_flagOff(original)BenchmarkRecordFieldReachedLookup_flagOn(this PR)renderFieldPath: -46% latencyrecordFieldReachedlookup: -35% latency, -1 alloc, 0 B/opFlag
Default is
true(improvement active in all environments).Set to
falseto revert to original string-per-call behaviour for debugging or rollback.Risk
Low — the flag defaults on but is trivially toggled off. The scratch buffer is pool-managed and never escapes; no change to observable resolver output.