Skip to content

gen: stop generated packages compiling keel's generics - #6

Merged
emoss08 merged 3 commits into
masterfrom
build-time-generics
Nov 6, 2026
Merged

emoss08 merged 3 commits into
masterfrom
build-time-generics

Conversation

@emoss08

@emoss08 emoss08 commented Nov 6, 2026

Copy link
Copy Markdown
Owner

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/api build that was about 267k of the 618k functions its own packages compiled. This removes most of it:

  • Non-generic row core. Generated writes and row terminals call new non-generic functions on row addresses (CreatePtr, CreateManyPtrs, SavePtr, SaveExceptPtr, DeleteRowPtr, UpsertPtrs, CreateOrSkipPtrs, CopyPtrs, OnePtr, FirstPtr, ListPtrs, AllPtrs, UpdateReturningPtrs, PagePtrs, EachPtrs) and convert the rows themselves. One, List, Create and the other generic functions remain as thin shims over the same core for models keel gen does not cover. fetchInto, pageRead and the batch items now work on unsafe.Pointer rows; 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 new keel.TypedStringCol (Col's methods, Text() and the StringValue marker) instead of StringCol, whose ~70 promoted methods cost each enum column about 150 compiled wrappers. In/NotIn pass the slice as []string instead of instantiating Strings per type.
  • No generated keel.Handle assertion. It compiled 38 interface wrappers per model; keel's tests check generated tables implement Handle instead (internal/testmodels/*/handle_test.go).
  • CTETable.Query removed. It named Query[*Row] from a type reachable from QueryCore, DB and every column, so every package that touched keel compiled the interface's 50 method wrappers. Read a CTE with keel.RowsOf(cte.TableRef()).
  • Docs and the skill correct the claim that an instantiated generic interface and a generic function cost nothing outside the package that uses them, and teach the new rules.

Breaking changes (release as v0.10.0)

  • CTETable.Query() is gone: use keel.RowsOf(cte.TableRef()).
  • A generated named-string column no longer has StringCol's text functions or its StringCol field: t.Status.Lower() becomes t.Status.Text().Lower(), t.ID.Collate(...) becomes t.ID.Text().Collate(...), x.Status.StringCol becomes x.Status.Text(). Its Coalesce is now Col's (returns Expr); use Text().Coalesce(...) for a string expression.
  • Regenerate with keel gen after upgrading.

Measured effect (Trenova, 4 cores, cmd/api watch build)

  • Functions compiled by first-party packages: 618,670 → 517,717 (−16%); machine code 255 → 231 MB; binary 335 → 320 MB.
  • Cold-build CPU: 789 → ~752 s (−4%). Rebuild scenarios S1–S6: unchanged within noise. The removed functions were cheap wrappers, so this is a modest win; the large rebuild win in Trenova came from Go 1.27.1 + gocut.

Checks

  • go vet ./...: clean.
  • go test ./gen/...: passes (expectations updated; asserts generated code has no keel.Handle[, keel.One[, keel.List[, keel.PageOf[, keel.Each[, keel.Create(, keel.Upsert().
  • go test ./... against Postgres 16 with KEEL_TEST_REQUIRE=1: every package passes except five tests that fail identically on unmodified master: three need Postgres 18 (RETURNING old) and two need the keelpg Docker container for pg_dump.
  • Trenova master already runs this branch (pinned at dd8386e): builds, go vet with integration tags clean, keel gen -check up to date. The last commit here (e3cd8dc) changes only the skill's docs.

Generated by Claude Code

Eric Moss and others added 3 commits November 6, 2026 00:40
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
@emoss08
emoss08 merged commit 6f67218 into master Nov 6, 2026
2 checks passed
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