Repository navigation
perf(discrete_space): use flatnonzero in select_random_empty_cell fallback - #4004
Merged
quaquel merged 4 commits intoOct 28, 2026
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
quaquel
approved these changes
Oct 28, 2026
EwoutH
pushed a commit
that referenced
this pull request
Nov 22, 2026
Co-authored-by: Jan Kwakkel <[email protected]> (cherry picked from commit f3ec718)
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.
Summary
Use
np.flatnonzero+np.unravel_indexinstead ofnp.argwherein the O(n) fallback ofGrid.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.argwherebuilds 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 callsnp.flatnonzeroon theemptylayer, draws a random index, and maps it back withnp.unravel_index. Bothargwhereandflatnonzerolist empty cells in C order, so the same random index selects the same cell and seeded models are unaffected. The full-gridValueErrorand 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 theemptylayer and the cells agree, so a bad fixture cannot produce bogus timings.tests/discrete_space/test_discrete_space.py: the fallback selects exactly whatnp.argwhere+random.choiceselected (2D and 3D, several fill levels), and a full grid still raisesValueError.Usage Examples
No API change.
grid.select_random_empty_cell()behaves as before; reproduce the benchmark withpython 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:
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_spacepasses (141 tests) and ruff is clean.