Repository navigation
perf: move typed-query bodies off the generic types - #222
Merged
Merged
Conversation
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.
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.
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:
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.