You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Search: read the field clock once per query, and don't look up each row's hash twice in FT.AGGREGATE KNN #670
Follow-ups from the review of #643 (kept out of it so it stays a correctness fix).
field_now() (a clock read) runs once per document in a query. Most of the +3% on queries over hashes with field TTLs comes from that, and one query can judge two documents at different moments. Read it once per query and pass it down.
FT.AGGREGATE with KNN looks up each row's hash a second time (src/commands/vector.rs around 826-828), +10.3% when indexed hashes have field TTLs. Reuse the first lookup.
nearest_shown (src/commands/vector.rs ~1572-1592) keeps doubling the KNN search size up to the whole index and ignores the query TIMEOUT. Cap it at 2-3 rounds, then fall back to the exact filtered path.
Measure with callgrind (scripts/perf/run.sh or a search workload), with and without field TTLs.
Tagged good first issue for items 1 and 2 (item 3 needs more design, leave it out or do it in a separate PR). Both are local changes: read the field clock once per query and pass it down; reuse the first hash lookup in FT.AGGREGATE with KNN. A good PR includes before/after instruction counts from scripts/perf/run.sh (or a small search workload) with and without field TTLs, and the existing search tests passing. Comment here before starting.
@murtazayusuf items 1 and 2 went in with #830. I'd like to take item 3 as its own PR. My plan:
nearest_shown gets at most 3 rounds (k, then 2k, then 4k, plus whatever was hidden). If it still has fewer than k visible hits, it returns that it ran out instead of growing to the whole index.
On that, the caller (FT.SEARCH at search.rs:2299, FT.HYBRID at hybrid.rs:676) falls back to the exact filtered path (FilteredKnn) with "field not hidden" as the filter. That gives the same k results the doubling would have found.
Each round and the fallback check the query deadline. A timeout goes through the existing ON_TIMEOUT handling: fail errors, and return gives what was found so far.
Tests: a query where most of the nearest hits have an expired vector field returns the same results as today after 3 rounds, and a small TIMEOUT on a large index with mostly hidden hits stops instead of scanning everything. Callgrind numbers with and without field TTLs, as in perf(search): read the field clock once per query, and each aggregate row's hash once #830.
Does that match what you had in mind for the fallback and ON_TIMEOUT?
@magic-peach you're right, 1 and 2 went in with #830; sorry for pointing you at a half-done issue. Item 3 is yours, and the plan matches what I had in mind. A few points:
The fallback's results need not equal what the doubling would have found: the doubled search is approximate and FilteredKnn is exact, so where they differ the fallback is the better answer. Test for "the k nearest visible documents", not for equality with today's output.
Cover every caller of nearest_shown, not only FT.SEARCH and FT.HYBRID: check FT.AGGREGATE with KNN and any path through vector sets, and say in the PR which ones there are.
Deadline: check it between rounds and inside the fallback's scan at the same granularity the other search paths use, not once per document if that reads the clock (item 1's lesson). With no TIMEOUT given the default applies as elsewhere.
ON_TIMEOUT return: what was found so far must still be only visible documents, in distance order.
Numbers: callgrind for a plain KNN with no field TTLs (must not move), with TTLs but nothing hidden (must not move), and the mostly-hidden case before and after.
Activity
murtazayusuf commentedon Nov 14, 2026
Tagged good first issue for items 1 and 2 (item 3 needs more design, leave it out or do it in a separate PR). Both are local changes: read the field clock once per query and pass it down; reuse the first hash lookup in FT.AGGREGATE with KNN. A good PR includes before/after instruction counts from scripts/perf/run.sh (or a small search workload) with and without field TTLs, and the existing search tests passing. Comment here before starting.
perf(search): read the field clock once per query, and each aggregate…
magic-peach commentedon Nov 18, 2026
@murtazayusuf items 1 and 2 went in with #830. I'd like to take item 3 as its own PR. My plan:
nearest_showngets at most 3 rounds (k, then 2k, then 4k, plus whatever was hidden). If it still has fewer thankvisible hits, it returns that it ran out instead of growing to the whole index.FT.SEARCHat search.rs:2299,FT.HYBRIDat hybrid.rs:676) falls back to the exact filtered path (FilteredKnn) with "field not hidden" as the filter. That gives the samekresults the doubling would have found.ON_TIMEOUThandling:failerrors, andreturngives what was found so far.TIMEOUTon a large index with mostly hidden hits stops instead of scanning everything. Callgrind numbers with and without field TTLs, as in perf(search): read the field clock once per query, and each aggregate row's hash once #830.Does that match what you had in mind for the fallback and
ON_TIMEOUT?murtazayusuf commentedon Nov 18, 2026
@magic-peach you're right, 1 and 2 went in with #830; sorry for pointing you at a half-done issue. Item 3 is yours, and the plan matches what I had in mind. A few points:
FilteredKnnis exact, so where they differ the fallback is the better answer. Test for "the k nearest visible documents", not for equality with today's output.nearest_shown, not onlyFT.SEARCHandFT.HYBRID: checkFT.AGGREGATEwith KNN and any path through vector sets, and say in the PR which ones there are.TIMEOUTgiven the default applies as elsewhere.ON_TIMEOUT return: what was found so far must still be only visible documents, in distance order.search: nearest_shown takes the graph's locks in the graph's order; F…
search: an unfiltered KNN with hidden nearest vectors stops at TIMEOUT (
Merge pull request #918 from magic-peach/perf/knn-hidden-rounds