Repository navigation
gen: stop generated packages compiling keel's generics - #6
Merged
Merged
Conversation
Every generated table package, and every package that reached keel's core types, compiled its own copy of keel generic code. Measured on Trenova's API build, that was about 267k of the 618k functions its own packages compiled. - Generated writes and row terminals call new non-generic functions on row addresses (CreatePtr, ListPtrs, PagePtrs, ...) and convert the rows themselves. One, List, Create and the other generic functions are now shims over the same core, and fetchInto, pageRead and the batch items work on unsafe.Pointer rows. - A generated column of a named string type embeds the new TypedStringCol (Col's methods plus Text and the StringValue marker) instead of StringCol, whose text functions each enum column promoted as about 150 wrappers. In/NotIn pass the slice as []string instead of instantiating Strings. - Generated code no longer asserts keel.Handle per table, which compiled 38 interface wrappers per model; keel's tests check generated tables implement it. - CTETable has no Query method: it named Query[*Row] from a type reachable from QueryCore, DB and every column, so every package that touched keel compiled 50 interface wrappers. Read a CTE with keel.RowsOf(cte.TableRef()). Docs: correct the claim that an instantiated generic interface and a generic function cost nothing outside the package that uses them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L7Gd5tC3EjqWxcw9Y51RWx
intoItem was one generic closure holding the statement, the layout lookup and the scan loop, compiled for every result type in every package that calls Into: in Trenova's API build, 1,916 copies (2.5 MB of machine code) in 148 packages. The loop is now intoRows, which decodes into rows a small per-type sink allocates, so each result type compiles three short closures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L7Gd5tC3EjqWxcw9Y51RWx
The keel skill described generated wrapper columns without the rule that a named string type's text functions now go through Text(), and said nothing about reading a CTE now that CTETable has no Query method, so an agent following it would write code that no longer compiles. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L7Gd5tC3EjqWxcw9Y51RWx
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 changed and why
Every generated table package, and every package that reached keel's core types, compiled its own copy of keel's generic code. In Trenova's
cmd/apibuild that was about 267k of the 618k functions its own packages compiled. This removes most of it:CreatePtr,CreateManyPtrs,SavePtr,SaveExceptPtr,DeleteRowPtr,UpsertPtrs,CreateOrSkipPtrs,CopyPtrs,OnePtr,FirstPtr,ListPtrs,AllPtrs,UpdateReturningPtrs,PagePtrs,EachPtrs) and convert the rows themselves.One,List,Createand the other generic functions remain as thin shims over the same core for models keel gen does not cover.fetchInto,pageReadand the batch items now work onunsafe.Pointerrows;Into's scan loop is non-generic (intoRows), with a three-closure sink per result type.TypedStringCol. A generated column of a named string type embeds the newkeel.TypedStringCol(Col's methods,Text()and theStringValuemarker) instead ofStringCol, whose ~70 promoted methods cost each enum column about 150 compiled wrappers.In/NotInpass the slice as[]stringinstead of instantiatingStringsper type.keel.Handleassertion. It compiled 38 interface wrappers per model; keel's tests check generated tables implementHandleinstead (internal/testmodels/*/handle_test.go).CTETable.Queryremoved. It namedQuery[*Row]from a type reachable fromQueryCore,DBand every column, so every package that touched keel compiled the interface's 50 method wrappers. Read a CTE withkeel.RowsOf(cte.TableRef()).Breaking changes (release as v0.10.0)
CTETable.Query()is gone: usekeel.RowsOf(cte.TableRef()).StringCol's text functions or itsStringColfield:t.Status.Lower()becomest.Status.Text().Lower(),t.ID.Collate(...)becomest.ID.Text().Collate(...),x.Status.StringColbecomesx.Status.Text(). ItsCoalesceis nowCol's (returnsExpr); useText().Coalesce(...)for a string expression.keel genafter upgrading.Measured effect (Trenova, 4 cores,
cmd/apiwatch build)Checks
go vet ./...: clean.go test ./gen/...: passes (expectations updated; asserts generated code has nokeel.Handle[,keel.One[,keel.List[,keel.PageOf[,keel.Each[,keel.Create(,keel.Upsert().go test ./...against Postgres 16 withKEEL_TEST_REQUIRE=1: every package passes except five tests that fail identically on unmodifiedmaster: three need Postgres 18 (RETURNING old) and two need thekeelpgDocker container forpg_dump.masteralready runs this branch (pinned atdd8386e): builds,go vetwith integration tags clean,keel gen -checkup to date. The last commit here (e3cd8dc) changes only the skill's docs.Generated by Claude Code