Skip to content

fix(discrete_space): make CellCollection.cells return a tuple (#4142) - #4146

Merged
quaquel merged 3 commits into
mesa:mainfrom
deba2k5:fix/cellcollection-cells-copy-4142
Nov 20, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
deba2k5:fix/cellcollection-cells-copy-4142

Conversation

@deba2k5

@deba2k5 deba2k5 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Note

Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

CellCollection.cells returned its cached list itself, so changing that list changed every later read of .cells, while len(collection) and iteration still used the real cells. The fix is to return a tuple instead of list.

Bug / Issue

Fixes #4142. Reproduced on main (7787437): after cells = collection.cells; cells.clear(), len(collection) is 2 and len(collection.cells) is 0. I have not seen this in a real model; it is the case reported in the issue.

Implementation

  • _cell_list (a private cached_property) holds the cells and is what select_random_cell uses, so that path is unchanged.
  • cells is now a property that returns a new list on each access.

Testing

Two new tests in tests/discrete_space/test_discrete_space.py: the issue's reproduction (and that each access returns its own list), and that select_random_cell gives the same cells whether or not the caller reordered or cleared .cells. Both fail on main.

pytest tests --ignore=tests/examples -Werror -Wdefault::PendingDeprecationWarning: 1160 passed (Python 3.12, Windows). ruff check and ruff format --check on the two changed files: clean. Not run: the browser example tests, which need Playwright.

Additional Notes

Trade-off: .cells now costs O(n) per access, measured with timeit on this machine:

cells before after
100 0.16 µs 1.1 µs
10,000 0.17 µs 157 µs

select_random_cell is unchanged at about 1 µs. If a copy per access is too costly, alternatives are returning a cached tuple (no cost, but the type changes from list, so it needs the deprecation cycle) or only documenting that the list is shared. I chose the copy because it keeps the list type; I am happy to change it.

AI use: the change, its tests and this description were written with Claude Code (an AI coding assistant), which also ran the tests and timings above.

CellCollection.cells was a cached_property that handed out the cached list
itself, so changing the returned list changed every later read of .cells,
while len(collection) and iteration still used the real cells.

Keep the cached list in a private property for select_random_cell, which
needs it in O(1), and make cells return a new list on each access.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@github-actions github-actions Bot added the testing Release notes label label Nov 19, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.3% [-0.9%, +0.3%] 🔵 +0.9% [+0.6%, +1.1%]
BoltzmannWealth large 🔵 +1.0% [-0.1%, +2.3%] 🔵 +1.3% [-2.8%, +5.4%]
Schelling small 🔵 -0.4% [-0.9%, +0.2%] 🔵 +0.7% [-0.1%, +1.4%]
Schelling large 🔵 +2.5% [+1.7%, +3.3%] 🔴 +17.2% [+13.5%, +20.6%]
WolfSheep small 🔵 +2.8% [+2.1%, +3.5%] 🔵 +2.9% [+1.3%, +4.5%]
WolfSheep large 🔵 +0.7% [-0.3%, +1.8%] 🔵 +0.2% [-0.8%, +1.0%]
SugarscapeG1mt small 🔵 -1.5% [-2.1%, -0.9%] 🔵 -1.8% [-3.4%, -0.3%]
SugarscapeG1mt large 🔵 -0.1% [-1.7%, +1.5%] 🔵 -0.3% [-2.2%, +1.7%]
BoidFlockers small 🔵 -0.1% [-1.1%, +0.8%] 🔵 -0.4% [-1.0%, +0.2%]
BoidFlockers large 🔵 +1.0% [-0.2%, +2.2%] 🔵 -0.7% [-1.2%, -0.1%]
MultiLevelAllianceModel small 🔵 -0.5% [-2.0%, +1.0%] 🔵 -1.8% [-2.3%, -1.3%]
MultiLevelAllianceModel large 🔵 -0.7% [-2.7%, +0.9%] 🔵 +0.1% [-2.6%, +2.5%]

@mesa-triage-bot

Copy link
Copy Markdown

Thanks for the reproducer and timings, @deba2k5. The cached-list inconsistency is reproducible, but #4142 gives no real-model example, and CI reports a 17.2% slowdown for large Schelling.

@quaquel, should .cells return a copy or retain its cached-list contract? I recommend deferring unconditional copying until a realistic workflow in #4142 establishes the need and the reported slowdown is investigated. Please settle that trade-off there before further implementation, following the problem → concept → implementation workflow.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@quaquel

quaquel commented Nov 20, 2026

Copy link
Copy Markdown
Member

The docstring seems clear: ""An immutable collection of cells.". So, it is correct that we use cached cells here.

@quaquel quaquel closed this Nov 20, 2026
@quaquel

quaquel commented Nov 20, 2026

Copy link
Copy Markdown
Member

See also the issue, please change it to just retuning a tuple.

@quaquel quaquel reopened this Nov 20, 2026
@sangitabera

sangitabera commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

hii @deba2k5 , I have seen that one of your test is failing. Could you fix this or else i will work on this issue.

deba2k5 and others added 2 commits November 20, 2026 21:29
)

CellCollection is documented as an immutable collection of cells, but
cells was a cached list, so changing the returned list changed every later
read of cells. Return a cached tuple instead, as the maintainers suggested
on the issue. The tuple is built once, so access stays as cheap as before,
and select_random_cell uses it directly again.

Replace the tests for the copying approach with one that checks cells is a
tuple that cannot be changed.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@quaquel

quaquel commented Nov 20, 2026

Copy link
Copy Markdown
Member

Can you update the PR description. Otherwise it looks good.

@deba2k5

deba2k5 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@quaquel updated PR description can you merge it now

@deba2k5

deba2k5 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@sangitabera I have done the changes now you can check

@quaquel quaquel added bug Release notes label trigger-benchmarks Special label that triggers the benchmarking CI labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔴 +10.1% [+9.0%, +11.3%] 🔵 +2.1% [+1.8%, +2.3%]
BoltzmannWealth large 🔵 +3.0% [+2.3%, +3.6%] 🔴 +9.1% [+7.8%, +10.6%]
Schelling small 🔴 +4.5% [+4.0%, +5.0%] 🔵 +1.4% [+0.6%, +2.2%]
Schelling large 🔵 +3.3% [+2.6%, +3.9%] 🔴 +15.0% [+11.6%, +18.6%]
WolfSheep small 🔵 +1.3% [+0.7%, +1.8%] 🔵 -0.8% [-2.6%, +0.8%]
WolfSheep large 🔵 -0.5% [-1.9%, +0.8%] 🔵 +0.3% [-2.0%, +2.8%]
SugarscapeG1mt small 🔵 +2.8% [+2.3%, +3.2%] 🔵 +2.9% [+1.1%, +4.7%]
SugarscapeG1mt large 🔵 +4.9% [+2.1%, +7.7%] 🔵 -0.4% [-1.9%, +0.9%]
BoidFlockers small 🔵 +2.4% [+1.7%, +3.0%] 🔵 +1.8% [+1.2%, +2.5%]
BoidFlockers large 🔵 +0.4% [-0.1%, +1.0%] 🔵 -0.2% [-0.6%, +0.2%]
MultiLevelAllianceModel small 🔵 -4.6% [-6.4%, -2.8%] 🔵 -0.6% [-0.9%, -0.3%]
MultiLevelAllianceModel large 🔵 -3.0% [-3.8%, -2.1%] 🔵 -1.2% [-1.5%, -1.0%]

@quaquel quaquel changed the title fix(discrete_space): make CellCollection.cells return a copy (#4142) fix(discrete_space): make CellCollection.cells return a tuple (#4142) Nov 20, 2026
@quaquel quaquel removed the trigger-benchmarks Special label that triggers the benchmarking CI label Nov 20, 2026
@quaquel
quaquel merged commit 393c3f5 into mesa:main Nov 20, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CellCollection.cells exposes a mutable cached list, causing inconsistent collection behavior

3 participants