Speed up leaving-variable test with a linear argmin ratio scan#5
Merged
Conversation
The minimum-ratio (leaving-variable) test only ever needs the argmin, but the old code built a list of Q.make ratios and fully sorted it every pivot. Q.make reduces each candidate by a GCD, and Q.compare in the sort cross-multiplies on top — two GCD-carrying steps per comparison, plus an O(k log k) sort to read a single element. Profiling the hot rand_25x60 solve showed Q.compare as the 3rd hottest symbol, rivalling the core fraction-free Z.sub/Z.divexact. Scan linearly for the min ratio (ties broken by lowest row index, matching the old stable-sort order) and compare candidates with ratio_compare: a direct cross-multiplication (sign of b1*p2 - b2*p1, corrected for pivot signs) using only exact Z.mul/Z.sub/Z.sign on the shared-denominator integers -- no GCD, no allocation of reduced rationals. Leaving choice is bit-identical: all golden cram tests pass unchanged (they diff full -v per-pivot tableaux) and pivot counts + rational objectives match on every heldout instance. Isolated high-repeat A/B: +10.9% geomean, every instance faster or equal.
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
Speeds up the leaving-variable (minimum-ratio) test, which runs on every pivot regardless of pricing rule.
choose_leavingonly ever needs the argmin of the candidate ratios, but the old code:Q.make b pvratios — a GCD per candidate,List.fast_sorted it on(Q.compare, index)— cross-multiplying GCD-reduced fractions,Profiling the hot
rand_25x60solve showedQ.compareas the 3rd hottest symbol, rivalling the core fraction-freeZ.sub/Z.divexact.Change
Replace the build-list-and-sort with a single linear argmin scan. Candidates are compared by
ratio_compare, a direct cross-multiplication:Only exact
Z.mul/Z.sub/Z.signon the shared-denominator integers — no GCD, no reduced-rational allocation, no sort. The sign correction makes it valid on theignore_negtransition path too (negative pivots), so both paths share it.Correctness
Bit-identical selection. Scanning rows upward and replacing only on a strict ratio decrease reproduces the old stable
(ratio, index)ascending order, so the same leaving variable is chosen every time.-vper-pivot tableaux).Performance
bench/evaluate.py --baseline main --candidate alain/simplex-leaving-argmin --rule bland --trials 60:Every instance is faster or equal (worst +4.7%). Verdict: ACCEPTED.
🤖 Generated with Claude Code