Skip to content

perf(discrete_space): use flatnonzero in select_random_empty_cell fallback - #4004

Merged
quaquel merged 4 commits into
mesa:mainfrom
dietcoke-17:select_random_empty_cell_flatnonzero
Oct 28, 2026
Merged

quaquel merged 4 commits into
mesa:mainfrom
dietcoke-17:select_random_empty_cell_flatnonzero

Conversation

@dietcoke-17

Copy link
Copy Markdown
Contributor

Summary

Use np.flatnonzero + np.unravel_index instead of np.argwhere in the O(n) fallback of Grid.select_random_empty_cell, and add a reproducible micro-benchmark for the probe-budget discussion in #3626. There is no API change, and for a given seed the same cell is selected as before.

Motive

Near-full grids reach the fallback often, and np.argwhere builds an (m, ndim) coordinate array only to pick one row. #3619 tried to speed this up but was closed because moving the O(n) scan earlier regressed Schelling large. This PR leaves the 50-probe fast path untouched and changes only the fallback, which is the behaviour-preserving first step of #3626. Tuning the probe budget is deliberately left for follow-up discussion.

Implementation

  • mesa/discrete_space/grid.py: the fallback now calls np.flatnonzero on the empty layer, draws a random index, and maps it back with np.unravel_index. Both argwhere and flatnonzero list empty cells in C order, so the same random index selects the same cell and seeded models are unaffected. The full-grid ValueError and its message are unchanged.
  • benchmarks/grid_empty_cell_cutoff_benchmark.py: sweeps grid size x empty fraction over candidate strategies on identical grid states and reports the minimum-over-repeats mean time per call (same estimator as New strategy to choose optimal cutoff in Grid.move_to_empty #1565). It asserts the empty layer and the cells agree, so a bad fixture cannot produce bogus timings.
  • tests/discrete_space/test_discrete_space.py: the fallback selects exactly what np.argwhere + random.choice selected (2D and 3D, several fill levels), and a full grid still raises ValueError.

Usage Examples

No API change. grid.select_random_empty_cell() behaves as before; reproduce the benchmark with python benchmarks/grid_empty_cell_cutoff_benchmark.py --sizes 20 50 100.

Additional Notes

Evidence (local, one machine, one run; micro differences under ~10% are noise). 100x100 grid, microseconds per call:

empty fraction before after
0.05 14.7 10.8
0.02 27.1 17.3
0.01 38.1 22.9
0.005 46.2 23.8

At 10% empties or more the code path is identical. In paired Schelling runs with the same seeds, density 0.99 is about 31% faster. Schelling large (density 0.8) shows a new/old ratio of about 1.00 with a 95% interval of roughly +-6%, which is too loose to claim "no regression", so I'd rely on the CI benchmark for that. Equivalence check: 4,500 seeded selections over 2D and 3D grids, 0 mismatches. tests/discrete_space passes (141 tests) and ruff is clean.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-27T18:31:58.048849Z
We couldn’t complete this review. Comment @codex review to try again.
52f63c2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@quaquel quaquel added performance Release notes label trigger-benchmarks Special label that triggers the benchmarking CI labels Oct 28, 2026
@quaquel quaquel removed the trigger-benchmarks Special label that triggers the benchmarking CI label Oct 28, 2026
@quaquel
quaquel merged commit f3ec718 into mesa:main Oct 28, 2026
12 of 14 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
Co-authored-by: Jan Kwakkel <[email protected]>
(cherry picked from commit f3ec718)
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.

2 participants