Repository navigation
Prevent Cell neighborhood caches from retaining discarded models - #4030
RohitKattimani wants to merge 6 commits into
Conversation
|
for some reason, the automatic upload of benchmarks is not working
|
|
I find the loss of performance on even fairly simple benchmark models concerning. We use the caching exactly because of its performance benefits. |
|
Hi @quaquel, I have pushed a revised implementation addressing the performance concerns raised in your earlier feedback. The main changes I made are:
I also added regression tests for cache reuse, topology invalidation, and legacy pickle compatibility. The local cache-hit benchmarks are encouraging. Could you please rerun the official benchmark suite against the latest revision, particularly for large BoltzmannWealth and Schelling? I would like to verify that the previous runtime regressions have been resolved without compromising the memory-retention fix. Thank you for the detailed feedback and for your valuable time to review this contribution! |
|
|
Thanks for the benchmark follow-up, @RohitKattimani. The branch now has merge conflicts, and main already includes #3934, which removed the inner Please rebase onto current main and rerun the discrete-space tests so review can continue against the current implementation. Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
94f72c3 to
978c356
Compare
|
Hi @quaquel and @mesa-triage-bot, Thanks for the reminder! I have rebased the branch onto the latest upstream, including the changes from #3934, and resolved the conflicts in cell.py. I have kept the neighborhood cache optimizations, including the radius-1 and radius-2 fast paths, weak-reference-based cache tracking, and regression tests. I've also rerun the discrete-space test suite, and all tests pass. Ruff linting, formatting, and git diff --check pass as well. The updated branch has been pushed, so CI should now run against the rebased changes. Thanks for your patience and review! |
|
Thanks for rebasing and rerunning the tests, @RohitKattimani. @quaquel, the branch is now mergeable with all checks passing, and the author answered your fast-path question with the direct-neighborhood and Schelling use cases. Please resume review of the cache trade-off against the rebased implementation. I'd assess this as a scoped memory-retention fix, leaving the global-invalidation change tracked in #3445 separate. Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
|
Hi @quaquel, Just following up on this PR. I've addressed the review comments, simplified the neighborhood cache construction, and pushed the changes on top of the latest main. The discrete-space tests and CI checks are passing. Whenever you get a chance, I'd be grateful for another look at the updated implementation. Thanks again for your time and feedback! |
sh: line 1: wq: command not found
ce43bdc to
fe8cf47
Compare
Summary
This PR fixes memory retention caused by the class-level neighborhood caches in
Cell.Changes
functools.cachedecorators with per-cell neighborhood cache holders.weakref.WeakSet, preventing the global registry from retaining cells and their associated models.Motivation
The previous implementation cached instance methods using
functools.cache. Since these caches are shared at the function level, their keys retain strong references toCellinstances.Cached neighborhood collections also reference neighboring cells and their agent lists. Consequently, models, agents, and cells can remain alive even after the simulation has been discarded and all user-held references have been removed.
This change moves cached results into per-cell cache holders and uses weak references to track active caches without making the registry a source of strong references.
Testing
pytest tests/discrete_space/test_discrete_space.py -q(129 passed)ruff check mesa/discrete_space/cell.py tests/discrete_space/test_discrete_space.pyruff format --check mesa/discrete_space/cell.py tests/discrete_space/test_discrete_space.pygit diff --checkSummary
This change focuses on cache lifetime and memory retention. It preserves the current global invalidation behavior rather than attempting to introduce local invalidation.