Repository navigation
perf(solver): open the scalar swap cursor in linear time - #256
Merged
Merged
Conversation
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
Collaborator
Author
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.
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: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
scalar_swapzero-regression gate case (192 entities, 4,096-value range). Smoke vsmain: identical order and identity hashes, wall 145 ms -> 102 ms, allocated bytes 278 MB -> 77 MB, peak live 7.4 MB -> 1.1 MB.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.main).make pre-releaseandmake examplespass.make bench-zero-regressionrun againstmain(3ad8e48): result posted below when it completes.