Invalidate the tagged-mesh cache when the source file changes - #5705
Closed
aabills wants to merge 2 commits into
Closed
Invalidate the tagged-mesh cache when the source file changes#5705aabills wants to merge 2 commits into
aabills wants to merge 2 commits into
Conversation
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>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
Author
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
TaggedSubMeshGeneratorcaches parsed.mshfiles 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:Three problems:
.mshon 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.strvsPathdouble-caches. The key is the raw object, so"x.msh"andPath("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,
_readreturned the same 9-point object.Fix
Key on
(os.fspath(path), st_mtime_ns)viafunctools.lru_cache:os.fspathcollapsesstr/Pathto one key,lru_cache(maxsize=8)bounds retained meshes.Testing
test_tagged_generator_cache_reads_and_invalidates: same unchanged file returns the same object (cache hit),str/Pathcollapse to one entry, and bumping the file's mtime forces a re-read.test_tagged_generator_*tests injected fake meshes into_mesh_cachedirectly; 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-filesclean.🤖 Generated with Claude Code