perf(index): bulk-write the graph, parallelise parse, resolve in-memory (~12–15× faster)#8
Merged
Conversation
Indexing was bound almost entirely by the store-write path. Profiling aeontis-backend (119 files / 338 symbols) put the per-file write drain at ~83% of wall-clock, the parse at ~3%, and the resolve passes at ~11%. Four changes, each surgical and verified to leave output byte-identical (symbol/edge counts and deterministic `related` output checked at every step): A. Resolve passes stop scanning the store per name. resolve_supertypes and resolve_references issued one unindexed `symbols_matching` (a full Symbol-table scan under the global mutex) per supertype and per distinct referenced name. Both now resolve against a single in-memory SymbolIndex built from one full scan: by_name_ci (case-insensitive) and by_file, each bucket sorted by (start_line, end_line, id) for deterministic from/child selection. Semantics unchanged (case-insensitive match, same-file -> same-project -> all ambiguity policy, deterministic edge order, the no-declared-symbol guard). B. The per-file scan parses in parallel. read + blake3 + detect + the four tree-sitter extractors + the manifest parse are pure, owned, Send work, so they run across rayon's pool (par_iter, order-preserving collect). A sequential drain in candidate order then does every store write, so symbol-insertion and pending_* order are byte-identical to the old loop. The store is never touched from a rayon thread. C. File + symbol node writes go in one transaction. Each upsert_symbol / link_file_declares_symbol was its own auto-committed statement (~2 per symbol). The new GraphStore::write_files_batch collects a file's nodes and writes them under one BEGIN/COMMIT (default impl falls back to per-file writes for the in-memory test store). D. The batched writes use UNWIND $rows. write_files_batch and link_edges pass all rows of a kind as one list-of-structs parameter and MERGE them in a single `execute` (UNWIND $rows AS r ...), so a 61k-edge or 11k-symbol batch is a handful of FFI calls instead of tens of thousands. This was the dominant win at scale. Measured (index --force): aeontis-backend (119f / 338s / 648 ref edges): 6.8s -> 0.58s (~12x) aeontis-rn (1593f / 10935s / 61547 edges): 311s -> 21.4s (~15x) rayon is a new unconditional dependency. No schema, trait-read, or async changes beyond the additive write_files_batch method.
Review Summary by QodoOptimize indexing: parallel parse, in-memory resolve, batched writes
WalkthroughsDescription• Parallelize file parsing with rayon for 3% speedup • Build in-memory symbol index to eliminate per-lookup store scans • Batch file/symbol writes in single transaction via UNWIND • Batch edge writes using UNWIND $rows for 61k→5 FFI calls • Achieves ~12–15× overall indexing speedup on real repos Diagramflowchart LR
A["Parse candidates<br/>in parallel<br/>rayon"] --> B["Drain in order<br/>collect FileWork"]
B --> C["Batch file+symbol<br/>writes in one<br/>transaction"]
C --> D["Build SymbolIndex<br/>from one scan"]
D --> E["Resolve supertypes<br/>& references<br/>against index"]
E --> F["Batch edge writes<br/>via UNWIND"]
F --> G["Result:<br/>12–15× faster"]
File Changes1. src/indexer/mod.rs
|
Code Review by Qodo
1. Batch rows cloned unnecessarily
|
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.
Summary
Indexing is bound almost entirely by the store-write path, not parsing. Profiling
aeontis-backend(119 files / 338 symbols) put the per-file write drain at ~83% of wall-clock, the parse at ~3%, and the resolve passes at ~11%. This PR attacks all three, with the write path being the dominant win.Every change is verified to leave output byte-identical — symbol/edge counts and deterministic
relatedJSON were checked at each step on real repos.Counts identical on both at every step;
related --symbol <X> --jsonbyte-identical across two re-indexes (determinism) and identical to the pre-change graph.What changed (all in
src/indexer/mod.rs+ the store layer)A — Resolve passes stop scanning the store per name.
resolve_supertypes/resolve_referencesissued one unindexedsymbols_matching(a full Symbol-table scan under the global mutex) per supertype and per distinct referenced name. Both now resolve against a single in-memorySymbolIndexbuilt from one full scan —by_name_ci(case-insensitive) +by_file, each bucket sorted by(start_line, end_line, id)for deterministic selection. Semantics unchanged: case-insensitive match, same-file → same-project → all-candidates ambiguity policy, deterministic edge order, the no-declared-symbol false-positive guard.B — The per-file scan parses in parallel (rayon).
read + blake3 + detect + 4× tree-sitter extract + manifest parseare pure, owned,Sendwork, so they run across rayon's pool (par_iter, order-preservingcollect). A sequential drain in candidate order then does every store write, so symbol-insertion andpending_*order are byte-identical to the old loop. The store is never touched from a rayon thread (lbugConnectionis&mutunder one mutex — writes stay serial).C — File + symbol node writes go in one transaction. Each
upsert_symbol/link_file_declares_symbolused to be its own auto-committed statement (~2 per symbol). New additiveGraphStore::write_files_batchwrites a batch under oneBEGIN/COMMIT(default trait impl falls back to per-file writes, so the in-memory test store is unchanged).D — The batched writes use
UNWIND $rows.write_files_batchandlink_edgespass all rows of a kind as one list-of-structs parameter andMERGEthem in a singleexecute(UNWIND $rows AS r ...). A 61k-edge or 11k-symbol batch becomes a handful of FFI calls instead of tens of thousands — the dominant win at scale (RN: 177s → 21.4s came almost entirely from this).Why writes aren't parallelised
lbug allows concurrent connections but only one write transaction at a time (per the LadybugDB transaction docs), so threading the write path wouldn't beat that ceiling — it'd just move the serialization point and add contention. The lever is fewer, bigger write operations (C + D), which is what this does.
Notes
rayon.write_files_batch.Test plan
cargo fmt --check && cargo clippy --all-targets && cargo test— all green (91 tests). The resolution correctness guards (index_creates_inherits_and_implements_edges,index_creates_reference_edges_*,reference_ambiguous_name_links_all_candidates,reference_local_variable_creates_no_edge,reference_lookup_is_case_insensitive,reference_same_project_is_segment_safe) pass unchanged — proving identical semantics.status --jsoncounts and deterministic, identicalrelatedoutput at every step.