Repository navigation
test: share numba kernel cache across manager tests instead of per-test recompile - #35
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The new “shared cache dir” fixture is currently function-scoped (creating a new cache dir per test) and the isolated-cache fixture clears global caches without restoring them, making the behavior order-dependent and not fully matching the stated intent.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR optimizes motrix_env_core’s Numba manager test runtime by stopping per-test forced cold-cache recompilation and instead allowing kernel compilation reuse across tests, while keeping explicit “cold cache” isolation for the few tests that validate cache behavior.
Changes:
- Introduces an autouse fixture intended to provide a hermetic
NUMBA_CACHE_DIRfor the file without clearing in-process caches. - Converts
_isolated_numba_cachefrom an autouse fixture to an opt-in fixture and wires it into the specific cache-isolation tests.
File summaries
| File | Description |
|---|---|
| motrix_env_core/tests/test_numba_manager.py | Refactors pytest fixtures to reduce redundant Numba kernel recompilation and makes cache-isolation behavior opt-in for specific tests. |
Review details
Suppressed comments (1)
motrix_env_core/tests/test_numba_manager.py:301
_isolated_numba_cacheclears the global in-process caches but never restores them, so tests that run after an isolated-cache test will see a cold cache again (order-dependent behavior and avoidable recompilation). Consider snapshotting and restoring the caches in a yield fixture so isolation is limited to the tests that request it.
@pytest.fixture
def _isolated_numba_cache(tmp_path, monkeypatch):
monkeypatch.setenv("NUMBA_CACHE_DIR", str(tmp_path / "numba-cache"))
compiler_module._KERNEL_CACHE.clear()
compiler_module._TERM_CACHE.clear()
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…st recompile The autouse _isolated_numba_cache fixture cleared the in-process kernel caches and pointed NUMBA_CACHE_DIR at a fresh empty tmp directory for every test, so each of the 28 tests in test_numba_manager.py paid a full Numba recompilation (~26-31s total). Split it into two fixtures: - _shared_numba_cache_dir (autouse): sets a single hermetic on-disk cache directory per test run without clearing in-process caches, so each distinct plan compiles once; - _isolated_numba_cache (opt-in): the original cold-cache isolation, referenced explicitly only by tests that verify cache isolation behavior. 28 passed in ~11.5s (was ~26-31s); test count and coverage unchanged. Also add a pre-push check guideline (prek run --all-files) to AGENTS.md. Fixes #32
2963f49 to
b6e7977
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The new “shared cache dir” fixture currently creates a fresh tmp cache directory per test, contradicting the intended single shared on-disk cache directory behavior.
Review details
Suppressed comments (1)
motrix_env_core/tests/test_numba_manager.py:292
_shared_numba_cache_diris function-scoped and callstmp_path_factory.mktemp(...)each test, so it creates a new cache directory per test rather than sharing a single on-disk cache directory across the whole file as described. This defeats cross-test disk-cache reuse and causes repeated source materialization in different directories even when the in-process cache hits.
# Share a single on-disk cache directory across the whole test file (hermetic,
# never touches the user cache), but do not clear the in-process kernel caches:
# each distinct plan is compiled once instead of per-test full recompilation
# caused by cache-clearing autouse fixtures (issue #32).
@pytest.fixture(autouse=True)
def _shared_numba_cache_dir(tmp_path_factory, monkeypatch):
monkeypatch.setenv("NUMBA_CACHE_DIR", str(tmp_path_factory.mktemp("numba-cache")))
- Files reviewed: 2/2 changed files
- Comments generated: 0 new
- Review effort level: Lite
- Make _shared_numba_cache_dir module-scoped so the whole test module shares one on-disk cache directory instead of creating a fresh one per test; use a local MonkeyPatch since the monkeypatch fixture is function-scoped. - Make _isolated_numba_cache snapshot and restore the in-process caches so cache isolation is not order-dependent for later tests.
There was a problem hiding this comment.
🟢 Approval recommended
The change is confined to test fixtures/documentation and preserves isolation coverage by making cold-cache behavior explicit only where required.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0 new
- Review effort level: Lite
问题
motrix_env_core/tests/test_numba_manager.py的autousefixture_isolated_numba_cache在每个测试前清空进程内_KERNEL_CACHE/_TERM_CACHE,并把NUMBA_CACHE_DIR指向每个测试独立的空 tmp 目录。框架自带的同 plan 去重被完全绕过,28 个测试每个都全额重编译 Numba kernel(每个 ~1.2s,总计 ~26–31s)。修改
把原 autouse fixture 拆成两层:
_shared_numba_cache_dir(autouse):为整个测试文件设置一个共享的 hermetic 磁盘缓存目录(tmp_path_factory,不落用户缓存),但不清空进程内缓存——同一 plan 全文件只编译一次;_isolated_numba_cache(普通 fixture):保留原有的冷缓存隔离语义,仅由真正验证缓存隔离行为的测试显式引用:test_materialize_source_is_safe_for_concurrent_same_plan_writerstest_build_rematerializes_source_after_cache_invalidationtest_specialization_cache_failure_rebuilds_callable_dispatchers_before_retrytest_reset_descriptor_is_part_of_numba_kernel_cache_key语义上也更准确:绝大多数测试验证的是 env 行为契约,不需要冷缓存隔离。
效果
test_numba_manager.pymotrix_env_core/tests全量测试数量与覆盖不变。
Fixes #32