Skip to content

Prevent Cell neighborhood caches from retaining discarded models - #4030

Open
RohitKattimani wants to merge 6 commits into
mesa:mainfrom
RohitKattimani:fix-cell-neighborhood-cache-retention
Open

RohitKattimani wants to merge 6 commits into
mesa:mainfrom
RohitKattimani:fix-cell-neighborhood-cache-retention

Conversation

@RohitKattimani

Copy link
Copy Markdown

Summary

This PR fixes memory retention caused by the class-level neighborhood caches in Cell.

Changes

  • Replace class-level functools.cache decorators with per-cell neighborhood cache holders.
  • Track populated cache holders through a weakref.WeakSet, preventing the global registry from retaining cells and their associated models.
  • Preserve neighborhood cache invalidation when connections are added or removed, including changes affecting neighborhoods with radius greater than one.
  • Exclude derived neighborhood cache state from serialized Cell instances.
  • Add regression tests covering garbage collection, topology invalidation, and pickle compatibility.

Motivation

The previous implementation cached instance methods using functools.cache. Since these caches are shared at the function level, their keys retain strong references to Cell instances.

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.py
  • ruff format --check mesa/discrete_space/cell.py tests/discrete_space/test_discrete_space.py
  • git diff --check

Summary

This change focuses on cache lifetime and memory retention. It preserves the current global invalidation behavior rather than attempting to introduce local invalidation.

@github-actions github-actions Bot added the testing Release notes label label Nov 3, 2026
@quaquel

quaquel commented Nov 3, 2026

Copy link
Copy Markdown
Member

for some reason, the automatic upload of benchmarks is not working

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.4% [+1.9%, +3.0%] 🔵 +1.2% [+1.0%, +1.4%]
BoltzmannWealth large 🔵 +2.1% [+1.0%, +3.2%] 🔴 +10.1% [+7.2%, +13.1%]
Schelling small 🔴 +4.7% [+4.0%, +5.3%] 🔵 +0.2% [-0.6%, +1.0%]
Schelling large 🔴 +4.6% [+4.3%, +4.9%] 🔴 +14.9% [+13.1%, +16.8%]
WolfSheep small 🔵 +0.6% [+0.1%, +1.0%] 🔵 +1.8% [-2.1%, +5.6%]
WolfSheep large 🔵 -0.8% [-1.9%, +0.2%] 🔵 +3.4% [+1.5%, +5.6%]
SugarscapeG1mt small 🔵 +0.4% [-0.3%, +1.0%] 🔵 +0.3% [-1.4%, +2.0%]
SugarscapeG1mt large 🟢 -12.0% [-13.2%, -11.1%] 🔵 -1.0% [-3.2%, +1.3%]
BoidFlockers small 🔵 +1.2% [+0.7%, +1.7%] 🔵 -0.1% [-0.6%, +0.4%]
BoidFlockers large 🔵 +2.1% [+1.4%, +2.9%] 🔵 +1.3% [+1.2%, +1.5%]
MultiLevelAllianceModel small 🔵 +2.2% [+0.5%, +3.9%] 🔵 -1.1% [-1.6%, -0.6%]
MultiLevelAllianceModel large 🔵 +3.6% [+1.9%, +5.2%] 🔵 +0.7% [-1.4%, +2.8%]

@quaquel

quaquel commented Nov 3, 2026

Copy link
Copy Markdown
Member

I find the loss of performance on even fairly simple benchmark models concerning. We use the caching exactly because of its performance benefits.

@RohitKattimani

Copy link
Copy Markdown
Author

Hi @quaquel,

I have pushed a revised implementation addressing the performance concerns raised in your earlier feedback.

The main changes I made are:

  • Dedicated per-cell cache slots for the frequently used radius-1 and radius-2 neighborhood queries.
  • A dictionary fallback for less common neighborhood configurations.
  • Weak-reference-based tracking of cells with populated caches, also avoiding strong global references.
  • Preserved graph-wide invalidation to maintain correctness for neighborhoods with radius greater than one.

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!

@quaquel quaquel added performance Release notes label trigger-benchmarks Special label that triggers the benchmarking CI labels Nov 4, 2026
@quaquel

quaquel commented Nov 4, 2026

Copy link
Copy Markdown
Member
Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -15.5% [-15.9%, -15.1%] 🟢 -4.3% [-4.5%, -4.0%]
BoltzmannWealth large 🟢 -24.9% [-25.8%, -23.9%] 🔵 -3.3% [-5.4%, -1.6%]
Schelling small 🟢 -25.7% [-26.2%, -25.2%] 🟢 -9.0% [-9.7%, -8.2%]
Schelling large 🟢 -14.9% [-16.3%, -13.5%] 🔵 -4.7% [-7.4%, -2.3%]
WolfSheep small 🟢 -12.7% [-13.2%, -12.2%] 🔵 +2.7% [-1.0%, +6.4%]
WolfSheep large 🟢 -15.3% [-16.7%, -13.8%] 🔵 +4.9% [+2.5%, +7.4%]
SugarscapeG1mt small 🟢 -32.3% [-32.8%, -31.7%] 🔵 -2.5% [-4.5%, -0.3%]
SugarscapeG1mt large 🟢 -40.3% [-41.8%, -38.6%] 🔵 +0.4% [-1.4%, +1.9%]
BoidFlockers small 🔵 +1.7% [+1.2%, +2.4%] 🔵 -0.8% [-1.2%, -0.4%]
BoidFlockers large 🔵 +0.4% [-0.5%, +1.3%] 🔵 -0.7% [-1.0%, -0.4%]
MultiLevelAllianceModel small 🔴 +8.9% [+7.4%, +10.4%] 🔵 +2.3% [+1.7%, +2.9%]
MultiLevelAllianceModel large 🔵 +4.1% [+2.6%, +5.8%] 🔵 +3.8% [+1.8%, +5.6%]

Comment thread mesa/discrete_space/cell.py Resolved
@quaquel quaquel removed the trigger-benchmarks Special label that triggers the benchmarking CI label Nov 10, 2026
@mesa-triage-bot

Copy link
Copy Markdown

Thanks for the benchmark follow-up, @RohitKattimani. The branch now has merge conflicts, and main already includes #3934, which removed the inner _neighborhood cache.

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.

@RohitKattimani
RohitKattimani force-pushed the fix-cell-neighborhood-cache-retention branch from 94f72c3 to 978c356 Compare November 16, 2026 04:02
@RohitKattimani

Copy link
Copy Markdown
Author

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!

@mesa-triage-bot

Copy link
Copy Markdown

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.

Comment thread mesa/discrete_space/cell.py Resolved
Comment thread mesa/discrete_space/cell.py Resolved
Comment thread mesa/discrete_space/cell.py Resolved
@RohitKattimani

Copy link
Copy Markdown
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants