Skip to content

perf: move typed-query bodies off the generic types - #222

Merged
piwi3910 merged 13 commits into
mainfrom
perf/typed-query-fanout
Nov 17, 2026
Merged

piwi3910 merged 13 commits into
mainfrom
perf/typed-query-fanout

Conversation

@piwi3910

@piwi3910 piwi3910 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Refs #217. Moves the T-independent bodies of the typed builders onto non-generic receivers so they are compiled once instead of once per model type. No exported API change (apidiff-docs green).

Peak compiler RSS of the compile-memory fixture (100 models, 500 queries, go1.27.4), MiB default / -l / -c=1:

chunk head run default -l -c=1
baseline (main) 44039243561 9255 6064 7867
1 select bodies on selectCore/selectRef f7e755a 44043101666 7608 4822 5942
1b select clause builders go:noinline cfb4b74 44045606979 6521 4693 5650
2 insert conflict/identity/upsert helpers on insertRef 417c40f 44048154585 5995 4661 5151
fix + budget f103a66 44050440764 6074 4461 5094
final 17cc1e4 44053092210 6189 4813 5097

Run-to-run noise is about 3 percent. PEAK_RSS_BUDGET_KB lowered from 10900000 to 7150000 (measured 6220036 KiB plus about 15 percent).

nm top 5 by total size, baseline: Iter.func1 2.04 MB, renderDistinctOnWindow 1.61 MB, expandJoins 1.06 MB, keysetPredicate 0.98 MB, One 0.92 MB. Final: Iter.func1 1.54 MB, One 0.73 MB, All 0.60 MB, execReconcile 0.59 MB, execReadBack 0.54 MB.

What moved where: select clause builders, SQL rendering (render, keyset, DISTINCT ON emulation, locks), compile, count, explain, join expansion and the row loops' non-T steps moved to selectCore, selectRef (clauses plus a T-free table description) and plain functions; the insert conflict, explicit-column, identity and upsert-default helpers moved to insertRef. Generic methods are wrappers. Small builders are go:noinline so they are not copied into every instantiation.

CI: ci green on 17cc1e4 (unit, race, integration PG and MySQL/MariaDB, windows, macos, bench-module allocation gate, fuzz). Chunk 1 broke TestSelectNoHooksAllocBudget (33 vs 32 allocs); fixed by removing callback closures and holding the clauses by value in selectRef.

Not done: the large T-dependent insert bodies (execReconcile, execReadBack, valueRows, planBulk), update, delete and relation loading are unchanged. Not merged.

Adds a deterministic fixture generator (N models through momo generate plus a caller package of Q typed queries), a compile-memory workflow that records the Go compiler's peak RSS for the default, -l and -c=1 builds, a symbolised compiler memory profile, an instantiated-generics report from go tool nm, and a budget guard on the default peak. Refs #217.
Refs #217. Every distinct struct T gives SelectQuery[T] its own GC shape, so each method body was compiled once per model and Row type. The T-independent bodies (clause builders, SQL rendering, compile, count, explain, join expansion, row iteration) now live on selectCore, selectRef and plain functions; the generic methods only hand over the clauses and keep scanning and hooks.
Refs #217. The builders are small enough to inline, which copies their bodies into every instantiation of SelectQuery[T]. Mark them go:noinline so each wrapper is one call.
Refs #217. The conflict, explicit-column, identity and upsert-default helpers read only the clauses and the table description. They now live on a non-generic insertRef; the generic methods are one-line wrappers. scanner.scan is no longer inlined into the four row loops.
Refs #217. The closure passed to the row helpers added an allocation per One call and broke the select allocation budget. The helpers are now plain steps (firstRow, lastRow, nextRow) that the generic loops call.
Refs #217. The select allocation budget still measured one extra allocation per One call. A pointer to the clauses captured by the render closure is the one thing that differs from the generic code, so selectRef now owns a copy.
Refs #217. Run 44050440764: 6220036 KiB (6074 MiB), was 9477388 KiB. Budget is that plus about 15%.
Refs #217. The generic wrappers build the view eagerly, so a zero InsertQuery with no Meta must not dereference it.
Refs #217. Run 44053092210 measured 6338328 KiB (6189 MiB) on the final head; budget is that plus about 15 percent, rounded.
@piwi3910
piwi3910 merged commit 82e8a90 into main Nov 17, 2026
31 checks passed
@piwi3910
piwi3910 deleted the perf/typed-query-fanout branch November 17, 2026 10:09
@github-actions github-actions Bot mentioned this pull request Nov 17, 2026
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