Skip to content

perf(testing): share one dialect and compiled-statement cache across in-memory SQLite test engines - #867

Merged
antosubash merged 3 commits into
mainfrom
perf/test-shared-compile-cache
Nov 6, 2026
Merged

antosubash merged 3 commits into
mainfrom
perf/test-shared-compile-cache

Conversation

@antosubash

Copy link
Copy Markdown
Owner

Summary

Follow-up to #866. After the schema template, most of what was left of each test's app build was SQL compilation, not running SQL.

SQLAlchemy keeps compiled statements per engine, and keys them on the dialect object. Every app and db_state fixture builds a new engine, so every test compiled every statement it ran from scratch: the boot path (data-compatibility checks, settings hydration), the admin seed and the test body. A production process has one engine and pays this once.

simple_module_test._compile_cache (installed at plugin import, like the cheap password hashing):

  • Every in-memory SQLite engine built through init_db with the same dialect options gets one shared dialect and compiled cache.
  • The cache key carries statement structure only; values are extracted as bound parameters, as with one production engine across requests and tenants. The databases behind the engines stay separate.
  • A dialect is shared only after its engine has connected. SQLAlchemy initializes a dialect on its engine's first connection; the first version shared one that was never initialized and broke reflection. The regression test reproduces that failure.
  • The share gets its own LRUCache, so the first engine and its database are not kept alive.
  • Not shared:
    • file and Postgres databases;
    • engines with other dialect options;
    • query_cache_size=0.
  • Opt-out: SM_TEST_SEPARATE_COMPILE_CACHES=1.

Numbers

Same machine, 8 workers.

after #866 this PR
make test-py 249–256 s 227–232 s
test bodies, worker time 566 s 511 s
warm app build (idle) 0.194 s 0.159 s

The two runs on this PR had the machine at a load of about 9–10 from other sessions.

Also investigated and left alone:

  • A cached inspect.signature for pydantic default_factory: no measurable gain outside cProfile.
  • Caching module discovery: about 6%, but a test patches discovery.entry_points.
  • Trimming the environment for pydantic-settings: about 4%, and it would touch production settings code.

Verification

  • make lint green.
  • make test-py: 8,179 passed, 47 skipped.
  • make test-js: 900 passed.
  • Two reviewers (correctness and maintainability), then a focused pass 2. All findings fixed or triaged.
  • framework/testing/tests/test_shared_compile_cache.py covers:
    • sharing;
    • reuse across engines;
    • separate databases;
    • a never-connected first engine;
    • file databases and other options not shared;
    • server URLs not keyed.

https://claude.ai/code/session_01M9neheZZEe3sVpDi2S3zT4

Reuse is_memory_sqlite, add the SM_TEST_SEPARATE_COMPILE_CACHES opt-out, give the
share its own LRU cache so the first engine is not pinned, document the scope.

Claude-Session: https://claude.ai/code/session_01M9neheZZEe3sVpDi2S3zT4
An engine built with query_cache_size=0 has no compiled cache and stays unshared.

Claude-Session: https://claude.ai/code/session_01M9neheZZEe3sVpDi2S3zT4
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-11-06T14:36:10.371965Z 65a5b42 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying simple-module-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 65a5b42
Status: ✅  Deploy successful!
Preview URL: https://d2c05b2c.simple-module-python.pages.dev
Branch Preview URL: https://perf-test-shared-compile-cac.simple-module-python.pages.dev

View logs

@antosubash
antosubash merged commit b3f2a22 into main Nov 6, 2026
17 checks passed
@antosubash
antosubash deleted the perf/test-shared-compile-cache branch November 6, 2026 14:45
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