Regression fix: Cache the vector index size#2341
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a performance regression in Scorch vector (kNN) search by avoiding repeated computation of per-segment vector index sizes, and adjusts kNN-related stats accounting to better reflect logical searcher counts.
Changes:
- Refactors
SegmentSnapshot.cachedMetato usesync.Mapand adds typed-ish accessor helpers (newCachedMeta,load,store,contains). - Updates kNN optimization flow to reuse cached vector index sizes (avoiding repeated
Size()calls when already cached). - Changes
TotKNNSearchesaccounting to increment once per vector reader lifecycle (viaIndexSnapshotVectorReader.Close()), and updatesSegmentSnapshotconstructors to initializecachedMeta.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
index/scorch/snapshot_segment.go |
Replaces mutex+map cached metadata with sync.Map wrapper and new constructor/helpers. |
index/scorch/optimize_knn.go |
Refactors kNN optimization execution and adds caching check to skip repeated Size() computation. |
index/scorch/snapshot_index_vr.go |
Adds TotKNNSearches increment on vector reader close. |
index/scorch/scorch_test.go |
Updates test helper to initialize cachedMeta via newCachedMeta(). |
index/scorch/persister.go |
Initializes cachedMeta via newCachedMeta() when loading segments. |
index/scorch/introducer.go |
Initializes cachedMeta via newCachedMeta() when introducing/merging segments. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
maneuvertomars
approved these changes
Jun 1, 2026
capemox
approved these changes
Jun 1, 2026
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.
cachedMetaor not. We use the cached value when triggering the searcher start callback, but do not check if it exists in thecachedMetabefore calling theSize()API.Size()API was fixed but at the cost of additional computation to get a precise estimate. This results in a perf regression due to the aforementioned point.LoadOrStoreoperation for the size instead, where we perform a read to check if the size was cached, and only compute it if not cached.cachedMetato use async.Mapinstead of a map with a RW mutex as its more suited for thewrite-once-read-manystyle of usage which we use it for in this perspective.TotKNNSearchesto be equal to the number of KNN searchers created, which mirrors the FTS equivalentTotTermSearchersFinished, currently this stat is equal toN*XwhereNis the number of segments, refactor it to report justX.