Commit 29d42cb
committed
perf: MergeFieldTermLocations fast-path + Reset nil-guard
Two micro-optimizations on per-scored-candidate hot paths:
- MergeFieldTermLocations (search/util.go): return early when no constituent
match carries any FieldTermLocations (n == len(dest)). This is the common
case with no highlighting / location tracking, and skips the second
iteration and its per-match merge calls entirely.
- DocumentMatch.Reset (search/search.go): guard clear(scoreBreakdown) with a
nil check. clear(nil) still dispatches through the Go map runtime (~1ns),
and ScoreBreakdown is nil on the common path (no KNN, no score-breakdown
retrieval).
Benchmark (search package, added here), Apple M4 Pro, count=12 via benchstat:
MergeFieldTermLocations (no locations) 5.28ns -> 2.68ns -49% (p=0.000)
DocumentMatch.Reset (nil ScoreBreakdown) 6.62ns -> 5.76ns -13% (p=0.000)
Absolute per-call costs are small, but both run per scored candidate (many
millions of calls per query), so they trim steady per-candidate overhead.
No allocation change.1 parent 8fd0ead commit 29d42cb
3 files changed
Lines changed: 40 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
238 | | - | |
239 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
240 | 244 | | |
241 | 245 | | |
242 | 246 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
57 | 60 | | |
58 | 61 | | |
59 | 62 | | |
| |||
0 commit comments