Reduce main-thread cost of FT.SEARCH replies #1669
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.43) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 365 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.353
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #1669"
general
weight: 0.35
ethanglaser
Summary
Under high concurrency,
FT.SEARCHthroughput can be limited by valkey's main thread rather than by index search. Profiling the main thread with a fast vector index showed about half of its time going to building the reply, much of it bookkeeping that the reply doesn't need. This PR removes two sources of that overhead. The reply sent to the client is unchanged.Changes
1. Reply arrays with their length up front.
SendReply,ReplyRowsand theRETURNfields array usedVALKEYMODULE_POSTPONED_LENand set the length after writing the elements. Each postponed length creates a deferred node in the client's reply list, allocated and later spliced back on the main thread: one per result row, plus one for the top-level array. The number of elements per row follows from the command's options (RowElementCount()), and theRETURNfields a document has are counted by the same code that then writes them, so every array can be replied with its length.2. Reply strings without temporary
ValkeyModuleStrings. Document keys, scores, sort keys and distance values were replied asValkeyModule_ReplyWithString(MakeUniqueValkeyString(...)), which allocates and frees a string object per element. They now useValkeyModule_ReplyWithStringBuffer.Performance
Setup: valkey 9.1.3 with
io-threads 8andsearch.reader-threads 28; server on 56 cores of one Xeon Platinum 8480L socket, 56 closed-loop clients on the other socket, 15 s per point. Cohere 1M × 768 (VectorDBBenchcohere_medium_1m), COSINE. Measured with the same code on top of the SVS integration branch; these two commits apply cleanly tomain.HNSW (M 32,
EF_CONSTRUCTION200),EF_RUNTIME50,RETURN 1 id, QPS:| | k=1 | k=10 | k=100 |
|---|---|---|---|
| before | 36,689 | 33,359 | 6,776 |
| after | 38,910 (+6%) | 36,643 (+10%) | 17,254 (+155%) |
SVS LeanVec4x8 (256 dims), search window 50, QPS:
| | k=1 | k=10 | k=100 |
|---|---|---|---|
| before,
RETURN 1 id| 93,302 | 45,428 | 7,529 || after,
RETURN 1 id| 108,451 (+16%) | 76,190 (+68%) | 20,729 (+175%) || before,
NOCONTENT| 120,552 | 90,718 | 29,598 || after,
NOCONTENT| 128,414 | 117,081 | 42,691 |The gain appears wherever reply building limits throughput: at large k for every index, and at all k for indexes whose search is fast. With HNSW at k ≤ 10 the search threads are the limit on this setup, so the change has little effect there; at k=100, HNSW
RETURN 1 idgoes from 33% to 85% of itsNOCONTENTthroughput (about its search limit). Recall is identical before and after.Testing
commands_test(154),ft_search_parser_test(100),query_test(254).DCHECKs assert that the number of elements written matches the declared array lengths.🤖 Generated with Claude Code