Skip to content

Use generator in peek_ahead instead of list allocation#3413

Merged
quaquel merged 3 commits intomesa:mainfrom
souro26:optimize-peek-ahead-generator
Mar 2, 2026
Merged

Use generator in peek_ahead instead of list allocation#3413
quaquel merged 3 commits intomesa:mainfrom
souro26:optimize-peek-ahead-generator

Conversation

@souro26
Copy link
Copy Markdown
Contributor

@souro26 souro26 commented Mar 2, 2026

This PR makes a minor change by replacing intermediate list allocation in EventList.peek_ahead with a generator expression passed directly to heapq.nsmallest.

Previously, a full list of non-canceled events was constructed before calling nsmallest. Passing a generator avoids this temporary allocation while preserving identical behavior.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 2, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -6.1% [-6.5%, -5.6%] 🔵 +0.9% [+0.7%, +1.0%]
BoltzmannWealth large 🟢 -8.0% [-8.3%, -7.7%] 🔵 -1.3% [-2.9%, +0.3%]
Schelling small 🟢 -6.3% [-6.5%, -6.1%] 🔵 +0.8% [+0.6%, +1.0%]
Schelling large 🟢 -4.8% [-5.2%, -4.4%] 🔵 -2.1% [-3.6%, -0.7%]
WolfSheep small 🟢 -4.0% [-4.2%, -3.8%] 🔵 +0.7% [+0.5%, +0.8%]
WolfSheep large 🟢 -4.2% [-4.5%, -3.9%] 🔵 -0.6% [-1.1%, +0.0%]
BoidFlockers small 🔵 +0.2% [-0.5%, +0.9%] 🔵 -0.3% [-0.4%, -0.2%]
BoidFlockers large 🔵 -0.5% [-1.0%, +0.0%] 🔵 -0.9% [-1.0%, -0.8%]

@quaquel quaquel merged commit 77f6c87 into mesa:main Mar 2, 2026
12 checks passed
@EwoutH EwoutH added the performance Release notes label label Mar 13, 2026
EwoutH pushed a commit that referenced this pull request Mar 15, 2026
* Use generator in peek_ahead instead of list allocation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jan Kwakkel <[email protected]>
EwoutH pushed a commit that referenced this pull request Mar 15, 2026
* Use generator in peek_ahead instead of list allocation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jan Kwakkel <[email protected]>
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.

3 participants