Skip to content

test: share numba kernel cache across manager tests instead of per-test recompile - #35

Merged
wlgys8 merged 2 commits into
mainfrom
fix/issue-32-pytest-numba-cache
Sep 12, 2026
Merged

wlgys8 merged 2 commits into
mainfrom
fix/issue-32-pytest-numba-cache

Conversation

@wlgys8

@wlgys8 wlgys8 commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

问题

motrix_env_core/tests/test_numba_manager.py 的 autouse fixture _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_writers
    • test_build_rematerializes_source_after_cache_invalidation
    • test_specialization_cache_failure_rebuilds_callable_dispatchers_before_retry
    • test_reset_descriptor_is_part_of_numba_kernel_cache_key

语义上也更准确:绝大多数测试验证的是 env 行为契约,不需要冷缓存隔离。

效果

修复前 修复后
test_numba_manager.py 28 passed in ~26–31s 28 passed in ~11.5s
motrix_env_core/tests 全量 — 127 passed,无回归

测试数量与覆盖不变。

Fixes #32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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_DIR for the file without clearing in-process caches.
  • Converts _isolated_numba_cache from 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_cache clears 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.

Comment thread motrix_env_core/tests/test_numba_manager.py Outdated
…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
@wlgys8
wlgys8 force-pushed the fix/issue-32-pytest-numba-cache branch from 2963f49 to b6e7977 Compare September 12, 2026 04:54
@wlgys8
wlgys8 requested a lite review from Copilot September 12, 2026 04:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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_dir is function-scoped and calls tmp_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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@wlgys8
wlgys8 merged commit 8481250 into main Sep 12, 2026
6 checks passed
@wlgys8
wlgys8 deleted the fix/issue-32-pytest-numba-cache branch September 17, 2026 02:42
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.

pytest 慢:autouse fixture 每个测试清空 numba kernel 缓存导致全量重编

2 participants