perf(core): allocation-free query path with pooled SearchScratch - #46
Merged
Merged
Conversation
Search allocated 30-68 KB per query (HashSet, two PriorityQueues, PreparedQuery, ArrayPool neighbour copies, LINQ tail) and triggered a gen1 collection every 1-4k queries. Rename InsertScratch to SearchScratch, extend it with reusable PreparedQuery / normalised query / int8 code buffers, and rent it from a ConcurrentBag pool in both Search overloads. SearchLayerNearest, SearchLayerFiltered and GreedyClosest now read neighbour lists in place via NeighborPointer under the read lock, and the LINQ OrderByDescending/Take/Select/ToList tail is replaced by a stable insertion sort (rescored mode only) and a pre-sized result list. Visiting order is unchanged, so recall and the built graph are identical. Micro-benchmark (reference machine, 10k-node index, topK 10, ef 100): allocated per query 30.8/68 KB -> 1.48 KB, gen1 0, 128-d float 92 -> 51 us, 768-d int8 212 -> 164 us. Recorded in docs/design-performance.md section 2.1 and section 4.1/4.3. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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
PR 3 of the performance programme (
docs/design-performance.md§4.1 + §4.3): makes the query path allocation-free apart from the result the caller receives.Changes
InsertScratch→SearchScratch, extended with reusablePreparedQuery, normalised-query buffer and int8 query-code buffer; pooled viaConcurrentBag, rented in bothSearchoverloads, returned infinally.SearchLayerNearest,SearchLayerFiltered,GreedyClosestiterateNeighborPointerin place (read lock held; slots are whole int32 writes) instead of copying into anArrayPoolbuffer per hop.SearchLayerFilteredkeeps metadata strings only for admitted nodes.Measured (reference machine,
SearchBenchmarks,--job short)Gen1 collections: gone. Gen0: 7.4–16.6 → 0–0.31 per 1k ops.
Validation
dotnet build Qvec.slnx -c Release: 0 warningsSlow: 415/415; full solution fast: 507/507