Skip to content

Optimize Cell is_empty/is_full#3069

Merged
quaquel merged 3 commits intomesa:mainfrom
codebyNJ:fix/cell-performance-and-exceptions
Jan 4, 2026
Merged

Optimize Cell is_empty/is_full#3069
quaquel merged 3 commits intomesa:mainfrom
codebyNJ:fix/cell-performance-and-exceptions

Conversation

@codebyNJ
Copy link
Copy Markdown
Contributor

@codebyNJ codebyNJ commented Jan 4, 2026

Summary

Optimize Cell to remove an avoidable O(n) copy and improve error semantics.
This prevents unnecessary allocations on frequent remove_agent() calls and makes capacity errors catchable.

Bug / Issue

  • remove_agent() did self.empty = self.is_emptylen(self.agents)self._agents.copy() O(n) every removal.

Implementation

cell.py

  • is_emptyreturn len(self._agents) == 0
  • is_fullreturn len(self._agents) >= self.capacity

Testing

  • Added a concise test in test_discrete_space.py verifying that no list replacement occurs and that .agents still returns a copy.
  • Updated capacity-related tests to expect a ValueError.
  • Unit: all discrete_space tests passed locally (72/72).

@github-actions

This comment was marked as outdated.

@quaquel quaquel added trigger-benchmarks Special label that triggers the benchmarking CI enhancement Release notes label labels Jan 4, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 4, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.9% [+0.3%, +1.4%] 🔵 +1.2% [+1.1%, +1.3%]
BoltzmannWealth large 🔵 -0.1% [-0.5%, +0.3%] 🔵 +2.5% [-0.4%, +5.2%]
Schelling small 🔵 -0.2% [-1.8%, +1.5%] 🔵 -0.9% [-1.9%, +0.3%]
Schelling large 🔵 +0.1% [-0.0%, +0.2%] 🔵 -3.5% [-4.6%, -2.4%]
WolfSheep small 🔵 +0.0% [-0.1%, +0.2%] 🔵 -0.8% [-0.9%, -0.7%]
WolfSheep large 🔵 +1.9% [-5.5%, +11.0%] 🔵 -0.1% [-3.0%, +2.3%]
BoidFlockers small 🔵 -0.4% [-0.7%, -0.1%] 🔵 -0.7% [-0.8%, -0.6%]
BoidFlockers large 🔵 -0.4% [-0.8%, +0.1%] 🔵 -0.5% [-0.7%, -0.4%]

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 4, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.0% [-1.9%, -0.0%] 🔵 -1.3% [-1.5%, -1.2%]
BoltzmannWealth large 🔵 +0.2% [-0.7%, +1.2%] 🔵 -1.2% [-3.0%, +0.7%]
Schelling small 🔵 -0.2% [-1.2%, +0.9%] 🔵 -0.3% [-0.9%, +0.2%]
Schelling large 🔵 +0.8% [+0.5%, +1.0%] 🟢 -5.4% [-6.2%, -4.5%]
WolfSheep small 🔵 +0.4% [+0.1%, +0.8%] 🔵 -1.6% [-1.8%, -1.5%]
WolfSheep large 🔵 +0.3% [-5.4%, +6.9%] 🔵 +0.3% [-2.2%, +2.2%]
BoidFlockers small 🔵 +2.2% [+1.6%, +2.7%] 🔵 +1.0% [+0.8%, +1.3%]
BoidFlockers large 🔵 +2.2% [+1.9%, +2.5%] 🔵 +0.4% [+0.1%, +0.7%]

@quaquel quaquel removed the trigger-benchmarks Special label that triggers the benchmarking CI label Jan 4, 2026
@quaquel quaquel changed the title Optimize Cell is_empty/is_full and use ValueError for capacity Optimize Cell is_empty/is_full Jan 4, 2026
@quaquel quaquel merged commit 2ef2f61 into mesa:main Jan 4, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants