Skip to content

Optimise create_agents by replacing 'ListLike' approach with itertools#3163

Merged
quaquel merged 5 commits intomesa:mainfrom
codebreaker32:optimise_creat_agent
Jan 17, 2026
Merged

Optimise create_agents by replacing 'ListLike' approach with itertools#3163
quaquel merged 5 commits intomesa:mainfrom
codebreaker32:optimise_creat_agent

Conversation

@codebreaker32
Copy link
Copy Markdown
Collaborator

Description
This PR optimizes the Agent.create_agents factory method by replacing the custom ListLike helper class with standard itertools functions. This reduces Python-level overhead during the creation of large batches of agents.

Motivation
The previous implementation used a custom ListLike class to handle the distinction between scalar arguments (shared by all agents) and list arguments (distributed among agents). This introduced unnecessary attribute lookups (self.value) and method calls (__getitem__) inside the creation loop.

Changes

  1. Removed ListLike Class: The helper class has been removed entirely.
  2. Adopted itertools.repeat: Scalar arguments are now wrapped in itertools.repeat(), allowing the loop to handle all arguments uniformly using zip without manual indexing or Python-side conditional logic.

Performance Impact
Moves the iteration logic to C-level iterators, reducing the per-agent overhead during initialization.

Backwards Compatibility
This is a purely internal refactor of the method logic. The public API signature and behavior remain identical.

@github-actions

This comment has been minimized.

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Please rerun the benchmarks

@quaquel quaquel added performance Release notes label trigger-benchmarks Special label that triggers the benchmarking CI labels Jan 17, 2026
@github-actions

This comment was marked as outdated.

@github-actions

This comment was marked as outdated.

@quaquel quaquel removed the trigger-benchmarks Special label that triggers the benchmarking CI label Jan 17, 2026
@quaquel quaquel added the trigger-benchmarks Special label that triggers the benchmarking CI label Jan 17, 2026
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.5% [-1.2%, +0.2%] 🔵 +0.2% [+0.0%, +0.3%]
BoltzmannWealth large 🟢 -3.8% [-4.2%, -3.3%] 🔵 -4.4% [-7.4%, -0.9%]
Schelling small 🔵 -1.1% [-1.3%, -0.9%] 🔵 -0.7% [-0.8%, -0.5%]
Schelling large 🔵 -1.7% [-2.4%, -1.0%] 🔵 -5.0% [-7.8%, -2.4%]
WolfSheep small 🔵 -1.4% [-2.1%, -0.8%] 🔵 +1.6% [+1.3%, +1.9%]
WolfSheep large 🔵 -2.3% [-3.6%, -1.0%] 🟢 -5.4% [-6.4%, -4.3%]
BoidFlockers small 🔵 -2.4% [-2.9%, -1.9%] 🔵 -0.3% [-0.5%, -0.2%]
BoidFlockers large 🔵 -1.9% [-2.6%, -1.1%] 🔵 -0.7% [-0.8%, -0.6%]

@quaquel quaquel removed the trigger-benchmarks Special label that triggers the benchmarking CI label Jan 17, 2026
@quaquel quaquel merged commit 5ae4c77 into mesa:main Jan 17, 2026
15 checks passed
@codebreaker32 codebreaker32 deleted the optimise_creat_agent branch January 18, 2026 12:39
quaquel added a commit to quaquel/mesa that referenced this pull request Jan 18, 2026
mesa#3163)

* Optimise create_agents by replacing 'ListLike' approach

* Optimised tuple slicing overhead

* Add test for fast path

---------

Co-authored-by: Jan Kwakkel <[email protected]>
@EwoutH EwoutH added the enhancement Release notes label label Feb 11, 2026
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.

3 participants