Introduce _HardKeyAgentSet in agents.py#3219
Conversation
|
Performance benchmarks:
|
|
As |
8b8b872 to
5804776
Compare
We have to fix the model anyway because of the deprecation. The source seems to be |
|
I think # Looks like O(N), but is actually O(N²)
for _ in range(self.n):
# random.sample calls __getitem__ repeatedly.
# Since our __getitem__ rebuilds the list every time (O(N)), this line is expensive.
agent_a, agent_b = self.random.sample(self.agents, 2)Initially user was doing this, unaware of what was going under the hood agents_list = self.agents.to_list()
for _ in range(self.n):
agent_a, agent_b = self.random.sample(agents_list, 2)
# Truly O(N)However this was only possible because loop didn't involve any addition or deletion but I think it is still |
And this was exactly the reason why I wanted to deprecate it. I saw other models doing exactly this without realizing the overhead. |
|
Coverage sits at 87%. Can you check this and see if you can add relevant tests to bring this up? I'll try to review later today. |
ee7fec0 to
9d6f25f
Compare
Done |
mesa/model.py
Outdated
|
|
||
| @property | ||
| def agents(self) -> AgentSet[A]: | ||
| def agents(self) -> _HardKeyAgentSet[A]: |
There was a problem hiding this comment.
Might it be a good idea to update the docstrings for agents and agents_by_type to highlight that they return an agentset with hard refs and should not be messed with, since they are the key agent registration system in Mesa?
quaquel
left a comment
There was a problem hiding this comment.
One remaining question regarding how to properly document this. Codewise, I am fine with merging this.
I see two options:
If you prefer option 2, I will revert the changes in |
|
I prefer option 2, we can finalize the storage refactor there |
|
Option 2 is fine with me as well. I'll take a last look at this and merge it hopefully this afternoon. |
98f9c0f to
4f40fb9
Compare
_HardKeyAgentSet and refactor Model storage to use it_HardKeyAgentSet in agents.py
|
PR title and description updated to reflect reverted |
Summary
This PR introduces
_HardKeyAgentSet, that inherits fromAbstractAgentSetusing strong references (standard dict) instead of weak references. This eliminates WeakKeyDictionary overhead for Model-managed collections where lifecycle is explicitly controlled.Motive
See #3128 and #3160
Implementation:
AbstractAgentSetdict[A, None]for storage (vsWeakKeyDictionary)AgentSet, we get best of both worldsChanges
_HardKeyAgentSetto_list()from AgentSet to AbstractAgentSet✅ Fully backward compatible - no changes to existing APIs. This is a new internal class.
Ticks (2) from #3209