Skip to content

Optimize Event.__lt__ to avoid tuple allocation#3336

Merged
quaquel merged 2 commits intomesa:mainfrom
souro26:optimize-event-lt-v2
Feb 17, 2026
Merged

Optimize Event.__lt__ to avoid tuple allocation#3336
quaquel merged 2 commits intomesa:mainfrom
souro26:optimize-event-lt-v2

Conversation

@souro26
Copy link
Copy Markdown
Contributor

@souro26 souro26 commented Feb 17, 2026

Summary

Replaces the tuple-based comparison in Event.__lt__ with direct attribute comparisons to avoid temporary tuple allocation during heap operations. Ordering semantics remains the same (time, priority, unique_id).

Fixes #3333

Motive

Event.__lt__ is called frequently by heapq during event scheduling. The previous implementation constructed temporary tuples on every comparison In heavy scheduling workloads, this leads to repeated small allocations and unnecessary overhead. Avoiding tuple creation reduces comparison cost while preserving the exact same ordering semantics.

Implementation

The tuple comparison

return (self.time, self.priority, self.unique_id) < (
    other.time,
    other.priority,
    other.unique_id,
)

was replaced with direct sequential comparison:

if self.time != other.time:
    return self.time < other.time
if self.priority != other.priority:
    return self.priority < other.priority
return self.unique_id < other.unique_id

No changes were made to event semantics, scheduling logic, or public APIs.

Additional Notes

  • All existing mesa.time tests pass.
  • Benchmarks under medium/heavy scheduling loads show consistent performance improvements.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 17, 2026

Caution

Review failed

An error occurred during the review process. Please try again later.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.8% [-2.4%, -1.2%] 🟢 -3.5% [-3.8%, -3.2%]
BoltzmannWealth large 🔵 +0.1% [-0.8%, +1.1%] 🔵 -0.0% [-3.3%, +3.4%]
Schelling small 🔵 -0.1% [-0.6%, +0.3%] 🔵 -0.2% [-0.4%, +0.1%]
Schelling large 🔵 -0.1% [-0.7%, +0.7%] 🔵 +0.2% [-1.4%, +1.5%]
WolfSheep small 🔵 -1.2% [-1.6%, -0.7%] 🔵 -1.6% [-1.9%, -1.2%]
WolfSheep large 🔵 -1.7% [-3.1%, -0.2%] 🔵 -2.4% [-4.6%, -0.2%]
BoidFlockers small 🔵 -1.1% [-1.6%, -0.6%] 🔵 -1.3% [-1.6%, -1.0%]
BoidFlockers large 🔵 -1.9% [-2.7%, -1.0%] 🔵 -1.9% [-2.3%, -1.4%]

@quaquel quaquel merged commit 38df98e into mesa:main Feb 17, 2026
14 checks passed
Krishsharma179 pushed a commit to Krishsharma179/mesa that referenced this pull request Feb 21, 2026
* Optimize Event.__lt__ to avoid tuple 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>
@quaquel quaquel added the performance Release notes label label Feb 23, 2026
EwoutH pushed a commit that referenced this pull request Mar 15, 2026
* Optimize Event.__lt__ to avoid tuple 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>
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.

Improve performance of Event.__lt__ by avoiding temporary tuple allocation

2 participants