Commit 8fd0ead
committed
perf: specialized score-descending comparator for top-N collector
SortOrder.Compare is called on every heap comparison and on every
lowestMatchOutsideResults / searchAfter check in the top-N collector. The
generic path iterates the sort-field slice and checks two bool flags
(cachedScoring, cachedDesc) per call, adding overhead to what is usually a
single float64 comparison.
Detect the overwhelmingly common case at collector-construction time — a
single score-descending sort — and store a specialized comparator (hc.cmp)
that compares Score directly, with HitNumber as the tie-break. Share it
across the heap store, the lowestMatchOutsideResults fast-path, and
searchAfter pagination. Falls back to the generic SortOrder.Compare for any
other sort order.
Extracted from a larger change; the companion collectStoreHeap.Final
sort.Slice optimization depended on the ternary-heap rework and is not
included here.
Benchmark (BenchmarkTop{K}of{N}Scores, search/collector, score-descending),
parent vs this commit, Apple M4 Pro, count=10 via benchstat:
Top100of10000 -15.0%
Top1000of100000 -13.5%
Top1000of10000 -13.1%
Top100of100000 -11.2%
Top10of10000 -10.5%
Top10of100000 ~ (noisy, not significant)
geomean -11.6%
Consistent ~11-15% collector speedup for score-sorted queries (the common
case), no meaningful allocation change.1 parent 82e20dd commit 8fd0ead
1 file changed
Lines changed: 36 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| |||
132 | 133 | | |
133 | 134 | | |
134 | 135 | | |
135 | | - | |
136 | | - | |
137 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
138 | 168 | | |
139 | 169 | | |
140 | 170 | | |
| |||
163 | 193 | | |
164 | 194 | | |
165 | 195 | | |
166 | | - | |
167 | 196 | | |
168 | 197 | | |
169 | 198 | | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | 199 | | |
174 | 200 | | |
175 | 201 | | |
| |||
561 | 587 | | |
562 | 588 | | |
563 | 589 | | |
564 | | - | |
| 590 | + | |
565 | 591 | | |
566 | 592 | | |
567 | 593 | | |
| |||
571 | 597 | | |
572 | 598 | | |
573 | 599 | | |
574 | | - | |
575 | | - | |
576 | | - | |
| 600 | + | |
577 | 601 | | |
578 | 602 | | |
579 | 603 | | |
| |||
585 | 609 | | |
586 | 610 | | |
587 | 611 | | |
588 | | - | |
589 | | - | |
| 612 | + | |
590 | 613 | | |
591 | 614 | | |
592 | 615 | | |
| |||
0 commit comments