Skip to content

avoid temporary list allocation in EventList.__len__#3512

Merged
quaquel merged 1 commit intomesa:mainfrom
souro26:optimize-eventlist
Mar 11, 2026
Merged

avoid temporary list allocation in EventList.__len__#3512
quaquel merged 1 commit intomesa:mainfrom
souro26:optimize-eventlist

Conversation

@souro26
Copy link
Copy Markdown
Contributor

@souro26 souro26 commented Mar 11, 2026

Summary

This is a small micro optimization that avoids unnecessary memory allocation in a frequently used method without changing the public API. Replaced list comprehension in EventList.len with a generator expression to avoid creating a temporary list during length calculation.

Implementation

sum(1 for e in self._events if not e.CANCELED)

instead of

len([e for e in self._events if not e.CANCELED])

Additional Notes

This is a minor change meant purely for performance

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

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.1% [-0.3%, +0.4%] 🔵 -0.2% [-0.4%, -0.0%]
BoltzmannWealth large 🔵 +0.2% [-1.1%, +1.2%] 🔵 +4.8% [-0.0%, +9.0%]
Schelling small 🔵 +2.0% [+1.5%, +2.5%] 🔵 +0.6% [+0.3%, +0.9%]
Schelling large 🔵 +1.6% [+0.8%, +2.4%] 🔵 +5.0% [+1.4%, +8.7%]
WolfSheep small 🔵 +3.0% [+2.6%, +3.4%] 🔵 +2.0% [+1.8%, +2.2%]
WolfSheep large 🔵 +2.8% [+0.4%, +4.8%] 🔵 +3.7% [+0.0%, +7.2%]
SugarscapeG1mt small 🔵 +2.8% [+2.2%, +3.5%] 🔵 +1.7% [+1.4%, +2.0%]
SugarscapeG1mt large 🔵 +0.7% [-1.9%, +3.5%] 🔵 -0.1% [-1.4%, +1.3%]
BoidFlockers small 🔵 +0.6% [+0.1%, +1.3%] 🔵 +0.5% [+0.2%, +0.7%]
BoidFlockers large 🔵 +1.3% [+0.8%, +1.8%] 🔵 +1.8% [+1.4%, +2.1%]

@quaquel quaquel merged commit 4023021 into mesa:main Mar 11, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants