`Cell._clear_cache()` globally invalidates neighborhood cache across all cells
Describe the bug
Cell._clear_cache() in mesa.discrete_space.cell.Cell clears neighborhood caches globally instead of locally.
Although it is called from a single cell during connect() / disconnect(), it invokes cache_clear() on methods decorated with @functools.cache, which wipes cached neighborhoods for all Cell instances. This causes avoidable recomputation and performance degradation in dynamic spaces.
Expected behavior Cache invalidation should be scoped to affected cells (or affected cache entries), not global across every cell in the space. A local topology change should not invalidate unrelated cells’ neighborhood caches
To Reproduce
from mesa.discrete_space.grid import OrthogonalVonNeumannGrid
import random
space = OrthogonalVonNeumannGrid((10,), torus=False, random=random.Random(1))
cells = [space[(i,)] for i in range(10)]
# Warm neighborhood cache for many cells
for c in cells:
c.get_neighborhood(radius=2)
print("Before:", cells[0].get_neighborhood.cache_info())
# Example: CacheInfo(hits=0, misses=10, maxsize=None, currsize=10)
# Local topology change between two cells
space.remove_connection(cells[5], cells[6])
print("After:", cells[0].get_neighborhood.cache_info())
# Observed: CacheInfo(..., currsize=0) <-- global cache cleared
Additional context This is both correctness-of-cache-invalidation behavior and performance impact for dynamic/disconnected/reconnected graphs.
We are using functools.cache, which can only be done at the class level.
I raised this because cache_clear() currently clears the shared method cache globally. I’ll open a PR that replaces functools.cache here with per-cell caching so invalidation is local to affected cells.
I doubt that will be performant. But we can take a look.
I'm working on this locally, so let's see.
For future reference: see #3446. In short, caching is tied to dynamic grids. We need to first align on dynamics grids. Then, reverse engineer how to handle caching.
@quaquel I had opened a discussion on Dynamic Grids in https://github.com/mesa/mesa/discussions/3534
Something that came up while reviewing #3934, worth keeping in mind for whatever comes out of the dynamic grids discussion (#3534).
Because the clear is global today, radius > 1 neighborhoods get invalidated correctly more or less by accident. If invalidation is scoped to just the two cells being connected or disconnected, that stops being true. A's cached radius-2 neighborhood includes cells reached through B, so B.connect(C) has to invalidate A too, even though A itself wasn't touched:
import random
from mesa.discrete_space.cell import Cell
a, b, c = (Cell((i,), random=random.Random()) for i in range(3))
a.connect(b)
a.get_neighborhood(radius=2) # {b}
b.connect(c) # a is not touched
a.get_neighborhood(radius=2) # must be {b, c}; passes today only because the clear is global
In general a change at a cell has to invalidate every cell within the largest cached radius of it, and for a disconnect that set has to be worked out from the graph before the edge is removed. So a per-cell cache has to do a bounded graph walk on every topology change, which is probably part of why quaquel doubted it would be faster.
There is no test for this on main right now. #3934 adds one, once a small fix lands, and any per-cell caching PR here should keep it passing.