Skip to content

Invalidate the tagged-mesh cache when the source file changes - #5705

Closed
aabills wants to merge 2 commits into
mainfrom
claude/tagged-mesh-cache
Closed

Invalidate the tagged-mesh cache when the source file changes#5705
aabills wants to merge 2 commits into
mainfrom
claude/tagged-mesh-cache

Conversation

@aabills

@aabills aabills commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Description

TaggedSubMeshGenerator caches parsed .msh files so a multi-domain model reading the same file for several regions parses it once. The cache was an unbounded, class-level dict keyed on the raw path object:

_mesh_cache: dict = {}

@classmethod
def _read(cls, path):
    if path not in cls._mesh_cache:
        cls._mesh_cache[path] = meshio.read(str(path))
    return cls._mesh_cache[path]

Three problems:

  1. Never invalidated on file change. The key is just the path, so editing the .msh on disk and re-running in the same session silently returns the old mesh. This bites anyone iterating on a mesh during development — their edits are ignored until the process restarts.
  2. Unbounded and process-global. A class attribute shared across all instances, never cleared; every file read stays in memory for the process lifetime.
  3. str vs Path double-caches. The key is the raw object, so "x.msh" and Path("x.msh") create two entries for the same file.

Verified the stale read empirically: after rewriting a file from 9 to 18 points on disk, _read returned the same 9-point object.

Fix

Key on (os.fspath(path), st_mtime_ns) via functools.lru_cache:

  • os.fspath collapses str/Path to one key,
  • the modification time in the key makes an edited file a cache miss → re-read,
  • lru_cache(maxsize=8) bounds retained meshes.

Testing

  • New test test_tagged_generator_cache_reads_and_invalidates: same unchanged file returns the same object (cache hit), str/Path collapse to one entry, and bumping the file's mtime forces a re-read.
  • The four existing test_tagged_generator_* tests injected fake meshes into _mesh_cache directly; they now monkeypatch _read (they test region extraction, not caching).
  • uv run --group dev pytest packages/pybamm/tests/unit/test_meshes — 163 passed.
  • uv run pre-commit run --all-files clean.

🤖 Generated with Claude Code

TaggedSubMeshGenerator cached parsed .msh files in an unbounded,
class-level dict keyed on the raw path object. An edited file was never
re-read (a stale mesh was returned silently), str and pathlib.Path
paths double-cached, and the cache grew without bound. Replace it with
functools.lru_cache keyed on (os.fspath(path), st_mtime_ns): editing the
file invalidates the entry, str/Path collapse to one key, and the cache
is size-bounded.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@aabills
aabills requested a review from a team as a code owner August 7, 2026 00:18
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@aabills

aabills commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Closing: out of scope for #5688. Pre-existing item in already-merged #5687 mesh code, not part of the spatial-method PR.

@aabills aabills closed this Aug 7, 2026
@aabills
aabills deleted the claude/tagged-mesh-cache branch August 7, 2026 00:26
@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.12%. Comparing base (f82976f) to head (6957fad).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5705   +/-   ##
=======================================
  Coverage   98.11%   98.12%           
=======================================
  Files         340      340           
  Lines       32744    32748    +4     
=======================================
+ Hits        32128    32134    +6     
+ Misses        616      614    -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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