Skip to content

Ensure Cell only uses __slots__#3121

Merged
quaquel merged 9 commits intomesa:mainfrom
quaquel:remove_dict
Jan 13, 2026
Merged

Ensure Cell only uses __slots__#3121
quaquel merged 9 commits intomesa:mainfrom
quaquel:remove_dict

Conversation

@quaquel
Copy link
Copy Markdown
Member

@quaquel quaquel commented Jan 12, 2026

Summary

This PR ensures that Cell does not use an internal dict and only relies on __slots__. Relying on __slots__ only has speed and memory benefits. This PR builds on #3108.

Implementation

It updates Cell itself by removing __dict__ from __slots__ and also removes the @cached_property for neighborhood. It also ensures that in grid, where we dynamically subclass Cellwe includeslotsexplicitly to avoid automagically addingdict`. This change is also included when unpickling cells. Tests are added to ensure all is behaving as intended

Additional Notes

Removing @cached_property has a small performance penalty, but this can be mitigated by using get_neighborhood directly.

Closing #3107

@quaquel quaquel added the performance Release notes label label Jan 12, 2026
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.3% [+1.0%, +1.6%] 🔴 +3.9% [+3.7%, +4.1%]
BoltzmannWealth large 🔵 +2.3% [+2.0%, +2.6%] 🔵 +1.5% [+0.6%, +2.3%]
Schelling small 🔵 -2.0% [-2.6%, -1.7%] 🔵 -2.5% [-2.8%, -2.3%]
Schelling large 🟢 -4.4% [-4.8%, -3.9%] 🔵 -2.6% [-3.2%, -1.9%]
WolfSheep small 🔵 +0.2% [-0.0%, +0.5%] 🔵 +2.5% [+2.4%, +2.7%]
WolfSheep large 🔵 +1.3% [+0.9%, +1.7%] 🔵 +1.5% [+1.0%, +2.0%]
BoidFlockers small 🔵 +2.3% [+1.8%, +2.8%] 🔵 +0.6% [+0.3%, +0.8%]
BoidFlockers large 🔵 +1.8% [+1.3%, +2.3%] 🔵 +0.4% [+0.2%, +0.6%]

@EwoutH EwoutH added the enhancement Release notes label label Jan 13, 2026
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Jan 13, 2026

Looks great. Have you ran function/method level benchmarks for this?

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 13, 2026

Yes, the neighborhood property becomes slightly slower, but it's in the nanoseconds and, as seen by the benchmarks, does not matter in the bigger scheme of things.

Copy link
Copy Markdown
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good.

If you can post any benchmark numbers I think that would be great to have as historical reference.

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Jan 13, 2026

Would you like to make performance a distinct release-notes label, so these PRs are listed in a separate entry in the release notes?

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 13, 2026

I'll leave that for you to decide. I don't recall the exact details of the release cycle. I am fine either way.

@quaquel quaquel linked an issue Jan 13, 2026 that may be closed by this pull request
@quaquel quaquel merged commit efd5078 into mesa:main Jan 13, 2026
15 checks passed
@quaquel quaquel deleted the remove_dict branch January 13, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label performance Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory overhead in large grids due to __dict__ in Cell.__slots__

2 participants