Optimise create_agents by replacing 'ListLike' approach with itertools#3163
Merged
Optimise create_agents by replacing 'ListLike' approach with itertools#3163
Conversation
This comment has been minimized.
This comment has been minimized.
Collaborator
Author
|
Please rerun the benchmarks |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
Performance benchmarks:
|
quaquel
approved these changes
Jan 17, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR optimizes the
Agent.create_agentsfactory method by replacing the customListLikehelper class with standarditertoolsfunctions. This reduces Python-level overhead during the creation of large batches of agents.Motivation
The previous implementation used a custom
ListLikeclass 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
ListLikeClass: The helper class has been removed entirely.itertools.repeat: Scalar arguments are now wrapped initertools.repeat(), allowing the loop to handle all arguments uniformly usingzipwithout 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.