Skip to content

perf(solver): open the scalar swap cursor in linear time - #256

Merged
blackopsrepl merged 2 commits into
mainfrom
perf/scalar-swap-open-linear
Nov 12, 2026
Merged

blackopsrepl merged 2 commits into
mainfrom
perf/scalar-swap-open-linear

Conversation

@blackopsrepl

Copy link
Copy Markdown
Collaborator

Closes #255.

The native scalar swap cursor copied every entity's legal value list at open ((0..count).collect() per entity for counted/ranged sources) and tested membership with a linear scan. It now resolves legality once at open:

  • counted or ranged source: a bounds check against the range read at open (the count is still read once, at open);
  • entity-slice source: a lookup in the borrowed slice;
  • empty source: any assigned value, unchanged.

Every emitted swap, in the same order, is unchanged.

Audit of the other static scalar cursors (issue item 3): change, nearby change/swap and pillar swap already test legality per candidate (swap_destination_is_legal / value_is_legal) with no open-time snapshot. Pillar change intersects candidate values per pillar while streaming, not at open.

Verification

  • New scalar_swap zero-regression gate case (192 entities, 4,096-value range). Smoke vs main: identical order and identity hashes, wall 145 ms -> 102 ms, allocated bytes 278 MB -> 77 MB, peak live 7.4 MB -> 1.1 MB.
  • Hospital-scale model (solverforge-bench, 100,000 shifts x 10,000 employees), reproducible, step_count_limit = 100040: identical assignments, score (0hard/-2358120soft) and evaluated-move count (18,302,127) on both sides. Local-search move generation 555.48 ms -> 5.12 ms per step; local-search phase 27.96 s -> 4.87 s for 40 steps. The 10,000 x 1,000 instance at 10,500 steps: identical, 6.17 -> 0.93 ms per step.
  • Unit tests: swap pairs equal the materialized-membership rule for every value source with and without unassigned values; the count is read once at open; a 2^40-value range opens without materializing (aborts with an allocation failure on main).
  • make pre-release and make examples pass.
  • Full qualifying make bench-zero-regression run against main (3ad8e48): result posted below when it completes.

The compiled scalar swap leaf is a production local-search loop with no zero-regression case, so a change to how its cursor opens or tests legality had no gate. scalar_swap drives it through CompiledRuntimeRunner over 192 entities whose values sit in a 4,096-value range: every distinct pair is a legal exchange and the range dwarfs the entity count, the shape where cursor open cost must not follow the value range. The case joins the default gate set and the AGENTS.md hot-path list.
The native swap cursor copied every entity's full legal value list at open and tested membership with a linear scan. For counted and ranged value sources that is entities x values work per step: on the 100,000-shift, 10,000-employee hospital model it spent 555 ms per local-search step generating moves against 2 ms evaluating them.

The cursor now resolves the slot's legality once at open: a counted or ranged source becomes a bounds check against the range read at open (the count is read once, as before), an entity-slice source is looked up in the borrowed slice, and an empty source keeps accepting any assigned value. Opening is linear in the entity count and every emitted swap, in the same order, is unchanged.

On that model at a fixed 100,040-step budget the solution, score and evaluated-move count are identical before and after, and move generation drops from 555 ms to 5 ms per step. The scalar_swap gate case reports identical order and identity hashes with 3.6x fewer allocated bytes. Change, nearby and pillar cursors already test legality per candidate without this snapshot.

Closes #255
@blackopsrepl

Copy link
Copy Markdown
Collaborator Author

Zero-regression gate (full qualifying run, efaa1da vs main 3ad8e48, 14 cases, 5 layouts, 51 trials): passed. Every case semantic_match=true. scalar_swap: wall -27.7%, instructions -21.2%, allocated bytes -72.4%. All other cases within ±1.1% wall, instructions within ±0.01%, allocations identical.

@blackopsrepl
blackopsrepl merged commit 4910340 into main Nov 12, 2026
7 checks passed
@blackopsrepl
blackopsrepl deleted the perf/scalar-swap-open-linear branch November 12, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make the scalar swap cursor open in O(entities), not O(entities × values), with identical moves

1 participant