Commit 07e2a36
perf: skip unselected scan in BnbIter via per-branch cursor
The flamegraph after the delta-aware refactor showed ~32% of run_bnb
time spent walking candidate_order and checking
Bitset::contains(selected) || Bitset::contains(banned) per element,
inlined into insert_new_branches's `cs.unselected().next()`. As BnB
descends and more candidates get selected/banned, each .next() call
scans further before finding the next viable candidate.
But BnB never re-considers a position: each branch's exploration only
moves forward in candidate_order. Inclusion advances by 1; exclusion
advances past every consecutive same-(value, weight) candidate. So we
can store a per-Branch cursor and avoid the scan entirely.
Add `Branch::cursor: usize` (the position the branch will expand on
next). The init branch starts at 0; insert_new_branches advances past
any pre-selected/pre-banned positions on demand, then expands at the
located cursor and hands children their new cursors directly.
One subtlety: the exclusion branch's same-(value, weight) dedup run now
walks raw candidate_order positions, where the old `unselected()` scan
skipped already-decided candidates implicitly. The run must do the same
explicitly -- skip pre-selected/pre-banned positions (advancing the
cursor past them) rather than banning them or letting them end the run.
Otherwise a caller-pre-selected candidate that duplicates an excluded
one ends up simultaneously selected and banned in the selector that
run_bnb hands back, and a pre-decided candidate inside a duplicate run
fragments the equivalence class into redundant branches. Covered by
`bnb_exclusion_dedup_skips_decided_candidates`.
Bench (run_bnb_lowest_fee, n = pool size):
n=20 166 us -> 147 us (11%)
n=50 5.3 ms -> 4.5 ms (16%)
n=100 12.6 ms -> 11.5 ms (9%)
n=500 200 ms -> 171 ms (14%)
n=1000 365 ms -> 248 ms (32%)
(n >= 200 rows are the exhaust-cap group: fixed 100k rounds of frontier
expansion, so they measure pure per-round cost.)
Largest win at large n where the unselected scan was burning the most
time. Flamegraph confirms the unselected-scan hot spot is gone; new top
is LowestFee::bound itself (the metric's float math + lookahead).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent 7524a0d commit 07e2a36
3 files changed
Lines changed: 115 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
1 | 15 | | |
2 | 16 | | |
3 | 17 | | |
| |||
61 | 75 | | |
62 | 76 | | |
63 | 77 | | |
| 78 | + | |
64 | 79 | | |
65 | 80 | | |
66 | 81 | | |
| |||
79 | 94 | | |
80 | 95 | | |
81 | 96 | | |
82 | | - | |
| 97 | + | |
83 | 98 | | |
84 | 99 | | |
85 | 100 | | |
| |||
98 | 113 | | |
99 | 114 | | |
100 | 115 | | |
101 | | - | |
| 116 | + | |
102 | 117 | | |
103 | 118 | | |
104 | 119 | | |
| |||
108 | 123 | | |
109 | 124 | | |
110 | 125 | | |
| 126 | + | |
111 | 127 | | |
112 | 128 | | |
113 | 129 | | |
| |||
123 | 139 | | |
124 | 140 | | |
125 | 141 | | |
| 142 | + | |
126 | 143 | | |
127 | 144 | | |
128 | 145 | | |
129 | 146 | | |
130 | 147 | | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
135 | 165 | | |
| 166 | + | |
136 | 167 | | |
137 | | - | |
| 168 | + | |
138 | 169 | | |
139 | 170 | | |
140 | | - | |
141 | | - | |
142 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
143 | 174 | | |
144 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
145 | 180 | | |
146 | | - | |
147 | | - | |
148 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
149 | 193 | | |
150 | 194 | | |
151 | | - | |
| 195 | + | |
| 196 | + | |
152 | 197 | | |
153 | | - | |
| 198 | + | |
154 | 199 | | |
155 | 200 | | |
156 | 201 | | |
| |||
160 | 205 | | |
161 | 206 | | |
162 | 207 | | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
163 | 212 | | |
164 | 213 | | |
165 | 214 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
151 | 196 | | |
152 | 197 | | |
153 | 198 | | |
| |||
0 commit comments