Skip to content

Feat: optimize performance on hot paths and reduce allocations#51

Merged
VoidClancy merged 2 commits into
masterfrom
hotpath-optimization
Jul 20, 2026
Merged

Feat: optimize performance on hot paths and reduce allocations#51
VoidClancy merged 2 commits into
masterfrom
hotpath-optimization

Conversation

@VoidClancy

Copy link
Copy Markdown
Owner

1-runtime:
- Avoid Interface dispatch on hot paths, use a struct embedded in the Queries struct, and flattened-out props
- Replace Dialect interface with a struct, holding dialect sepecific functions and keywords for an easily-extensible workflow
- Define WriteQuotedIdent(), WritePlaceholder() reciever methods on Dialect, zero alloc, writes directly in the string builder
- Define Quote, BindVar(), FormatLimitOffset() methods on Dialect
- define BuildConflictClause() as Dialect method using field-based conflict keywords

2-client:
- define a mutex on the Queries struct, stmtCache, to prepare and cache sql queries, dialect sepcific quoteChar, and placeholderFmt to prevent interface dispatch on hot paths (should be made into a dialect struct embedded in the Queries struct for future extensibility)
- define Prepare() with Rlocks/unlocks, use it in query() and queryRow()
- remove postgresDialect/sqliteDialect types and old Queries.dialect field

3- builders_create:
-remove generic executeInsert, executeCreateMany/AndReturn, remove groupRowMapsByKeys() and partitionRowMaps(), that takes a []map[string]any as it is model-specific now

4- builders_query:
- remove generic executeFindOne/FindMany, executeSingle/ManyWithRelations

5- model_create:
- inline executeCreate, executeCreateMany/AndReturn, early return direct d.runCreate path when no extensions, closure onion when extensions present
- define runCreate, runCreateFallback, runCreateMany, runCreateManyAndReturn (model-specific SQL execution)
- define buildBulkInsertSQL using run-time colMask union (no generic []map[string]any)
- define partitionXXInputs using colMask (dialect-aware, no generics)
- remove pkCols inline slice literal, use package-level {{ lowercase .Model.Name }}PKCols

6- model_query:
- add early return check on executeFindUnique/FindFirst/FindMany for if no extensions were present in the exec functions, it executes the underlying exec function, or define a closure, handles extensions (onion pattern), then execute the underlying exec function if there were any extensions present
- define shared queryOne() and queryMany() for single and bulk ops without duplication

7- relations_runtime.gotpl:
- remove generic buildBulkInsertSQL, mapToColsVals
- buildSingleInsertSQL uses Dialect.WriteQuotedIdent/WritePlaceholder/BuildConflictClause

8-model_structs:
- define colMask() that returns a bit mask of columns that are set, prevents extra allocation for no reason
- move PKCols to be a package level var instead of re-defining on each query for bulk ops, saves some alloc

9- generator/helpers:
- fix isKnownDefaultFunc to skip empty-string entries

@VoidClancy VoidClancy self-assigned this Jul 20, 2026
@VoidClancy
VoidClancy merged commit 82f6eb4 into master Jul 20, 2026
5 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