Skip to content

Semantic search costs ~250ms per result returned: 0.3s at limit 1, 5s at limit 20 #2399

Closed

Description

@malibio

Problem

nodespace search costs roughly 250 ms per result returned. On a 32,000-node database:

--limit wall clock
1 0.32 s
2 0.45 s
5 1.17 s
10 2.78 s
20 5.03 s
50 5.09 s

Near-perfectly linear in the result count. The skill's default limit is 20, so a plain nodespace search takes about five seconds.

It is not what it looks like

Three hypotheses ruled out by measurement:

Not cold model load. Three consecutive identical searches: 4.22 s, 4.23 s, 4.25 s. A cold-start would show one slow run then fast ones. The cost is constant per call and paid every time.

Not query embedding. Cost tracks --limit, not query length, and embedding happens once per call regardless of how many results are returned. A 1-character query at limit 5 and a 71-character query at limit 5 differ (0.04 s vs 1.30 s) only because the short one matched fewer results above threshold — vary --limit on a fixed query and the line is straight.

Not the vector scan. Ranking 32k rows costs the same whether you return 1 or 20; sqlite-vec would brute-force that set in single-digit milliseconds. A scan does not produce a per-result slope.

Not socket or RPC overhead. nodespace query --type task --limit 5 returns in 0.00 s and nodespace diagnostics in 0.05 s against the same daemon. The transport is free.

So the cost is per-result work performed after ranking, inside the daemon.

Where it appears to be

ops::search_ops logs exactly one line for the whole call (Semantic search for: 'tech stack' (scope: Knowledge)) and then delegates to EmbeddingService::semantic_search_nodes (packages/core/src/services/embedding_service.rs:897), which calls semantic_search.

Two candidates ruled out by reading: the hydration in semantic_search_nodes is a plain filter_map over already-fetched results with no per-item await, and the title-boost loop at :698-710 is trivial string comparison. The cost is therefore in producing the KNN results with their nodes attached — most likely a per-result node fetch, or per-result aggregated-content assembly, executed serially.

The absence of any span or timing around that path is itself part of the problem: a 5-second operation emits one log line and nothing attributable.

Why it matters

Semantic search is the skill's primary discovery verb — it is what an agent runs first to orient itself in a session. Five seconds per call, several calls per session, is a large share of an agent's wall-clock budget spent waiting.

It also silently pushes agents away from the graph's best feature. An agent that learns search is slow will reach for query and node get instead, which answer narrower questions.

Note a prior benchmark in development/benchmarks/search-skills-latency.md measured a cross-type multi-skill turn at 7.2 s min / 40.5 s max — with search_skills called zero times. Whether that is related is unestablished, but both point at the same subsystem.

Acceptance criteria

  • Instrument the search path so per-phase cost is attributable — query embedding, KNN, BM25, per-result hydration, ranking — rather than one log line for the whole call
  • Identify the per-result work and confirm it against the measured ~250 ms/result slope — per-result work found and removed in four places; the ~250 ms/result figure did NOT reproduce at the store layer (per-result DB work measures in microseconds). See the measurements comment.
  • If it is serial per-result fetching, batch it — the store already has batch node fetches
  • Target: search cost dominated by a fixed per-call component, with the per-result slope near zero at these sizes
  • Regression test asserting search at --limit 20 stays within a fixed budget on a seeded database, so this cannot silently return
  • Re-measure the 1/2/5/10/20/50 series afterwards and record it, so the improvement is evidenced rather than asserted

Reproduction

On a 32k-node database (this install, v0.2.7):

for L in 1 2 5 10 20; do
  printf "limit=%-3s " "$L"
  /usr/bin/time -p nodespace search "tech stack" --limit $L >/dev/null
done

Context

Surfaced by an external agent that noticed a 4.2 s search against a 29 ms node export and asked whether that was fast. Its diagnosis was cold model load; the measurements above rule that out and point at per-result work instead.

Activity

  1. malibio commented on Sep 8, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 233 test files passed, 4900 tests passed (Duration 81.11s).

  2. self-assigned this
    on Sep 8, 2026
  3. malibio commented on Sep 8, 2026

    @malibio
    CollaboratorAuthor

    Measurements and findings

    All numbers from a release build (cargo test --release) on a seeded 32,000-node corpus (matching the reproduction size), best of 3 runs per limit. Seeding: 32k text nodes, each with a paragraph of body content and one 768-dim embedding chunk, inserted through the normal create_node + upsert_embeddings store API into a temp SQLite database.

    Instrumentation added first (AC 1)

    semantic_search now emits a structured per-phase profile — embed_ms, knn_ms, bm25_ms, search_ms, tier3_hydrate_ms, total_ms — instead of one opaque duration. search_embeddings emits its own knn_ms / score_ms / hydrate_ms split plus candidate and k counts. KNN and BM25 are timed inside the tokio::join! so each leg is attributable even though they overlap in wall clock.

    What the instrumentation showed

    The search path had per-result work in four places, all the same shape (one query per result, serial):

    1. search_embeddings — one get_node per surviving candidate, before truncation, so it also hydrated rows ranking then discarded.
    2. search_embeddings_by_node_type — same loop.
    3. The tier-3 BM25-only branch — one get_node per root.
    4. The gRPC SearchNodes handler — re-read every result from the store by id, discarding the nodes search had already fetched.

    Before / after (release, 32k corpus, best of 3)

    --limit before after
    1 27.57 ms 27.31 ms
    2 27.55 ms 27.80 ms
    5 28.00 ms 27.96 ms
    10 28.51 ms 28.81 ms
    20 30.16 ms 30.18 ms
    50 36.71 ms 36.27 ms

    Cost is dominated by the fixed ~27 ms KNN scan; the per-result slope is ~0.18 ms/result at limit 50 and within noise at limit 20.

    Finding that differs from the issue's hypothesis

    The ~250 ms/result slope did not reproduce at this layer, and the per-result DB work is not large enough to explain it. Measured individually on the 32k corpus:

    • serial per-result get_node re-fetch: 0.007 ms per result (0.14 ms at limit 20)
    • get_related_nodes per result: 0.01 ms each (graph_boost / include_edges, both default false)
    • get_subtree_data per result: 6.4 ms each — by far the largest per-result cost, but it is capped at include_markdown (default 1, max 5) and the CLI path passes include_markdown: 0, so it cannot produce a slope that scales to limit 50

    So the loops fixed here are the right shape — work that grows with result count rather than corpus size — and removing them is correct, but they do not by themselves account for the reported 250 ms/result. The remaining candidate is above the store layer (embedding-model or daemon-side behavior on the live install, which this harness does not exercise). The per-phase profile is what makes that attributable next time rather than requiring a fresh investigation.

    Regression test

    packages/core/tests/search_result_scaling_test.rs, 4 tests over a 2,000-node seeded corpus:

    • limit 20 must stay within 4x limit 1 plus a 50 ms floor. Real ratio is ~1.15x, so the budget has ~3.5x headroom; the floor prevents a sub-millisecond limit-1 baseline (where scheduler noise is a large multiple) from making the ratio meaningless. Deliberately loose — sized to catch a per-result N+1 returning, not to police percent-level drift on a loaded local gate.
    • the full 1/2/5/10/20/50 sweep must total under 2 s (measured: well under 200 ms release, under 1 s debug).
    • hydration is bounded by limit, not the candidate set, and results stay rank-ordered.
    • every returned result carries its node — guarding the gRPC handler's new reliance on that instead of re-fetching.

    Tests pass in both debug and release.

  4. malibio commented on Sep 8, 2026

    @malibio
    CollaboratorAuthor

    Closed by #2452, but the reported symptom is not fixed — tracking in #2455.

    #2452 removed five per-result fetch loops and added per-phase instrumentation to the search path. That work is real, but the ~250ms/result slope this issue reported did not reproduce: measured release on a 32k-node corpus it was flat before the change (27.6ms at limit 1, 30.2ms at limit 20) and flat after, with per-result store work at 0.007ms — three orders of magnitude too small to explain 0.32s→5.03s.

    So the loops were the right shape to remove and were not the reported cost. #2455 carries the unexplained remainder: the measurements, what the in-process harness did not exercise (gRPC transport, real embedding generation, cold state, concurrent daemon load), and the strongest lead — a blocking generate_embedding() call inside an async fn with no spawn_blocking, which can stall a live daemon's runtime in ways an in-process benchmark never shows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions