Skip to content

perf(orm): cache SQL shapes and tighten row mapping - #103

Merged
Antruly merged 1 commit into
masterfrom
perf/orm-cache
Nov 27, 2026
Merged

Antruly merged 1 commit into
masterfrom
perf/orm-cache

Conversation

@Antruly

@Antruly Antruly commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Part of #95. Parent: #88. This is the ORM hot-path optimization found by the new uvcpp_bench_orm A/B run; the benchmark itself lands in a separate stacked PR.

What changed

src/orm/uvcpp_orm_repository.h only:

  • caches per-model/dialect SQL text for SELECT-by-PK / INSERT / UPDATE / DELETE (values and uvcpp_db_params are still built for every call; no query-result caching, no semantic change);
  • moves mapped rows into the caller instead of copying them (*out = std::move(mapped));
  • resolves each column's assign/reset function once during prepare_bindings, rather than running a type switch for every cell of every row;
  • formats the "row N:" label only when an error actually occurs, instead of allocating it for every successful row.

No DB, async, transaction or C ABI behavior changed.

Before/after (development machine, SQLite 3.45.1, 500 end-to-end samples, 50k pure-build ops)

Before (unoptimized ORM):

case            mean_extra%   p50_extra%   p99_extra%
pk                    32.97        32.19        57.78
find_all100           20.78        20.96         9.96
update                36.22        36.08        76.01
insert                 1.95         0.95        10.38
remove                -0.04         0.87       -10.47

After:

case            mean_extra%   p50_extra%   p99_extra%
pk                    14.68        14.01        57.71
find_all100           10.86        11.00         8.85
update                -0.34         0.17       -16.35
insert                -0.65         0.59       -14.78
remove                 0.73         1.06        14.10

Absolute point-select: raw mean 6.51 µs vs ORM 7.46 µs. Pure mapping dropped from 0.111 to 0.060 µs/row. The p99 pk figure is still noisy (outlier on a shared development machine); the 3-round gate report must be run on a fixed runner before release. The hard >25% gate is not claimed as passed on this machine.

Verification

ctest -R '^test_orm_(model|sql|sqlite|tx_async)_func$'
4/4 passed

The ORM SQLite and transaction/async tests pass after the optimization; model and SQL golden tests are unchanged. Full benchmark methodology/hardware caveats are in bench/orm/README.md from PR #97 and the forthcoming benchmark PR.

Unverified

  • Fixed-runner/3-round performance gate, p95, CPU/allocation counters.
  • MySQL/PostgreSQL/ODBC real servers.
  • Windows/macOS; this is a Linux development-machine measurement.
  • No release/tag is made by this PR.

Cache per-model/dialect SELECT/INSERT/UPDATE/DELETE text while still building parameters per call, move mapped rows instead of copying, and resolve each column converter once instead of switching on every cell. Row labels are formatted only on errors.
@Antruly

Antruly commented Nov 27, 2026

Copy link
Copy Markdown
Owner Author

Local review before merge: ORM model/SQL/SQLite/transaction tests pass (4/4), no DB or API behavior changes, and the A/B benchmark on the same machine shows point-select p50 overhead reduced from ~32% to ~14%, find_all from ~21% to ~11%, with write paths unaffected. The benchmark is on a shared development machine; the fixed-runner p99 gate remains open.

@Antruly
Antruly merged commit 83f247c into master Nov 27, 2026
45 of 52 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