← Back to Pull requests

Reduce main-thread cost of FT.SEARCH replies #1669

Open
unknown opened this pull request with 365 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 06, 2026 00:46 UTC
Author

Summary

Under high concurrency, FT.SEARCH throughput 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, ReplyRows and the RETURN fields array used VALKEYMODULE_POSTPONED_LEN and 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 the RETURN fields 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 as ValkeyModule_ReplyWithString(MakeUniqueValkeyString(...)), which allocates and frees a string object per element. They now use ValkeyModule_ReplyWithStringBuffer.

Performance

Setup: valkey 9.1.3 with io-threads 8 and search.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 (VectorDBBench cohere_medium_1m), COSINE. Measured with the same code on top of the SVS integration branch; these two commits apply cleanly to main.

HNSW (M 32, EF_CONSTRUCTION 200), EF_RUNTIME 50, 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 id goes from 33% to 85% of its NOCONTENT throughput (about its search limit). Recall is identical before and after.

Testing

  • Existing unit tests pass: commands_test (154), ft_search_parser_test (100), query_test (254).

  • DCHECKs assert that the number of elements written matches the declared array lengths.

  • Integration tests: TODO.
  • 🤖 Generated with Claude Code

    Diff: 365 lines (+273 / -92) across 4 files Reviewer: ethanglaser Review duration: 35507m
    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
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.7% (MEDIUM) · Higher = larger blast radius
    42.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.4% · Safe to merge
    24.4%