perf(test): replace per-instance _database_initialized flag with process-global cache #1264

Merged
freemo merged 1 commits from perf/m3-global-db-initialized-cache into master 2026-04-02 16:58:40 +00:00

1 Commits

Author SHA1 Message Date
brent.edwards 2bf1c627eb perf(test): replace per-instance _database_initialized flag with process-global cache
CI / lint (pull_request) Successful in 25s
CI / typecheck (pull_request) Successful in 55s
CI / quality (pull_request) Successful in 56s
CI / security (pull_request) Successful in 1m13s
CI / build (pull_request) Successful in 26s
CI / helm (pull_request) Successful in 26s
CI / unit_tests (pull_request) Failing after 6m14s
CI / docker (pull_request) Has been skipped
CI / coverage (pull_request) Successful in 9m46s
CI / e2e_tests (pull_request) Successful in 18m45s
CI / integration_tests (pull_request) Successful in 22m5s
CI / status-check (pull_request) Failing after 1s
CI / benchmark-publish (pull_request) Has been skipped
CI / benchmark-regression (pull_request) Successful in 55m7s
Add a process-global `_INITIALIZED_DBS: set[str]` to `features/environment.py`
that caches database URLs after `_fast_init_or_upgrade` has processed them.
On subsequent calls with the same URL, the function returns immediately —
skipping all URL parsing, path extraction, prefix matching, and `stat()`
syscalls that previously executed on every new `UnitOfWork` instance.

The cache is cleared at the start of each scenario in `before_scenario` to
prevent cross-scenario state leaks, since each scenario receives fresh
temp-DB paths via `tempfile.mktemp()`.

Key implementation detail: test step definitions access `_INITIALIZED_DBS`
through `_fast_init_or_upgrade.__globals__` rather than importing from
`features.environment`, because Behave loads environment.py via `exec_file()`
into a private hooks dict — a separate namespace from the Python module.

Four new Behave scenarios validate cache hits on repeated calls, cache
clearing between scenarios, and non-caching of non-matching-prefix URLs.

Estimated savings: ~65,000 unnecessary function-body executions eliminated
per full test run (~7 UnitOfWork instantiations × ~10,700 scenarios).

ISSUES CLOSED: #735
2026-04-02 06:02:25 +00:00