Repository navigation
perf(rust-lsp): share sealed Rust stdlib and isolate per-file work arena - #3211
Conversation
Stop re-registering and double-finalizing the 2,029-entry Rust stdlib + crate seed into every file's pipeline-lifetime result->arena: 1. Build and finalize the Rust stdlib seed once per process into a sealed (`read_only = true`), lock-free CAS-published registry (`rust_stdlib_shared`) that local and cross-file registries chain to via `reg->fallback` (matching `go_lsp.c`), with copy-on-write on `impl Trait for StdType` so foreign impls never mutate the shared fallback. 2. Remove the premature `cbm_registry_finalize(reg)` call at the end of `cbm_rust_stdlib_register()` so direct callers never pay for duplicate hash-table construction before adding project definitions. 3. Isolate `cbm_run_rust_lsp_with_manifest()` in a per-call `work_arena` (sealing the local registry to enable `neg_memo`), deep-copying only emitted resolved calls and synthetic macro/operator calls into the caller's result arena; move temporary builder maps/conversion buffers in local and cross-file registry builders into lazy scratch arenas destroyed before return. On an 8-line Rust fixture, retained bytes in `result->arena` drop from 946,312 B to 528 B (-99.94%, 1,792x reduction) with identical resolved calls. Signed-off-by: Guillaume Demay <guillaumedemay@hotmail.fr>
d7cdac1 to
ea7fc5f
Compare
|
Thank you, @GDemay, this is a substantial one and it holds up well. Building the Rust stdlib once, publishing it with the same acquire/compare-and-swap pattern the Go engine already uses, and reaching it through We also checked the graph: the six Rust repositories we score with compiler-built call truth (cargo, tokio, meilisearch, diesel, qdrant, tauri) come out with 0 pairs gained and 0 lost, and the full edge sets of cargo (126k edges) and qdrant (267k) are identical. A few requests before it lands:
Thanks again, great work on the memory side of the Rust engine! |
…trings Address review feedback on DeusData#3211: - Add a CBM_ENABLE_TEST_SEAMS build counter (cbm_rust_stdlib_test_build_count) and a deterministic counted test asserting zero additional stdlib builds across four subsequent files and exact per-file type_count + func_count. - Keep static literal strategy/reason pointers zero-copy in rust_copy_resolved_calls (while duplicating scratch-arena-owned reasons) and copy caller_qn once per enclosing function. - Document why rust_ensure_mutable_type does not reuse cbm_registry_type_for_update. Signed-off-by: Guillaume Demay <guillaumedemay@hotmail.fr>
|
Thanks for the thorough review and corpus verification, @DeusData! All four items are addressed in
Local verification: Authored with AI assistance on behalf of @GDemay; commits carry Guillaume's DCO sign-off. |
|
Merged in 8633025. Thank you, @GDemay! The counted test pins "built once" exactly as we hoped: zero stdlib builds after the first publish across five files, with each file's registry holding only its own definitions. The string deduplication is a nice bonus. We re-ran the comparison on the final head, and the six Rust truth repositories and the full cargo and qdrant edge sets, strategy and confidence included, are still identical. Much appreciated! |
Problem
Every
.rsfile extracted in Pass 1 callscbm_run_rust_lsp(a, result, ...)withresult->arena(which stays alive across the pipeline until the file result is freed). Previously:cbm_rust_stdlib_register()re-registered all 276 types and 1,753 functions (580,432 B) into every per-file and cross-file registry AND prematurely calledcbm_registry_finalize(reg)(143,632 Bof hash buckets/entries), before any local/project definitions were even added.cbm_rust_build_local_registry()andcbm_run_rust_lsp_with_manifest()allocated the local registry, builder maps (type_idx,trait_names), temporary type indexes, finalized hash tables (143,848 B), and AST walk scratch directly into the caller'sresult->arena— retaining946,312 B(~924 KiB) per 8-line Rust file (~1.89 GB across 2,000 Rust files).cbm_rust_build_cross_registry()allocated temporaryCBMRustLSPDefconversion arrays sized to total mixed-languagedef_count(plustype_idx/trait_names/ split return-type buffers) directly into the long-lived shared cross-file registry arena.Fix
internal/cbm/lsp/rust_lsp.c, mirroringgo_lsp.c):read_only = true), lock-free CAS-published registry (rust_stdlib_shared()), chained viareg->fallbackin both local and cross-file builders (with OOM fallback to direct registration).cbm_registry_finalize(reg)ininternal/cbm/lsp/generated/rust_stdlib_data.c.RustTraitNameIndex,rust_registry_lookup_trait_impl_method,rust_lookup_method_in_trait,rust_find_sole_trait_impl, and the three free-function short-name iterators) to traversereg -> reg->fallbackaccurately — walking links directly for Rust method buckets so inherent and distinct trait-impl methods sharing a QN are never falsely shadowed, and storing trait QNs inRustTraitNameIndexso indices stay valid asreg->typesgrows.rust_ensure_mutable_type) forimpl Trait for StdType(and guard method auto-stub registration againstreg->fallback) so attaching a project trait to a stdlib type (e.g.,std.path.PathBufwithDerefTarget:std.path.Path) preserves existing stdlib metadata and never mutates the shared fallback.CBM_ENABLE_TEST_SEAMSbuild counter (cbm_rust_stdlib_test_build_count()) for counted regression testing.work_arenaisolation (internal/cbm/lsp/rust_lsp.h,internal/cbm/lsp/rust_lsp.c):cbm_registry_finalize(®), sealing (reg.read_only = true), andrust_lsp_process_file()inside a per-callwork_arena.neg_memo): sealing the per-file registry (reg.read_only = true) incbm_run_rust_lsp_with_manifest()(andcbm_run_rust_lsp_cross_with_manifest()) also switches on the negative-lookup memos (ctx->neg_memo, gated onreg->read_only) on the Pass-1 per-file path, matching the Tier-2 cross-file path. The negative memos are documented as pure once the registry is sealed, and graph output remains identical.CBMResolvedCallentries (rust_copy_resolved_calls, also reused bycbm_batch_rust_lsp_cross) and route synthetic macro/operatorCBMCallinjections (ctx.syn_calls_arena) into the caller's result arena before destroyingwork_arena. Static literalstrategy/reasonstrings stay zero-copy (while scratch-arena-owned reasons such aslsp_callable_alias/lsp_callable_value_referenceare still duplicated) andcaller_qnis copied once per enclosing function.type_idx,trait_names, tail memos, normalized trait lookup keys, split return-type scratch, andcbm_rust_build_cross_registry()'s Rust-onlyrdefsconversion buffer in lazy scratch arenas destroyed before return.Verification
Reproduce-first baseline failures on
upstream/main(e00c82dc)rustlsp_stdlib_register_defers_finalize FAIL tests/test_rust_lsp.c:6298: reg.func_qn_buckets is not NULLrustlsp_per_file_finalize_keeps_indexes_out_of_result_arena FAIL tests/test_rust_lsp.c:6355: run_bytes (946312) not < build_bytes + idx_bytes (942496)Deterministic arena measurements (
cbm_arena_total())upstream/main(e00c82dc)ea7fc5f2)cbm_run_rust_lsp()retained inresult->arena946,312 B528 B(smaller still afterc806337astring dedup)cbm_rust_build_local_registry()retained in caller arena724,064 B+ (incl. 277 types / 1,756 funcs)15,192 B(1 type / 3 funcs + shared fallback)cbm_rust_build_cross_registry()(10,000 C + 2 Rust defs)~1,924,000 B15,104 B(1 type / 1 func + shared fallback)Throughput on 2 vCPU Ubuntu 24.04 (ASan+UBSan
-O1):cbm_run_rust_lsp()completes 200 iterations in151.54 ms(0.758 ms/file), and a cold 8-thread concurrent race (8 threads × 25 extractions) ong_rust_stdlib_sharedpassed with 0 errors.Local gates & tests run
tests/test_rust_lsp.c(counters and structural assertions only, no timing):rustlsp_stdlib_register_defers_finalizerustlsp_per_file_work_arena_and_shared_stdlibrustlsp_shared_stdlib_cow_isolates_foreign_trait_implsrustlsp_per_file_work_arena_preserves_synthetic_callsrustlsp_shared_stdlib_builds_once_across_filesrustlsp_copy_resolved_calls_dedups_caller_and_preserves_arena_reasonrust_lsp,registry,arena,mem,repro_lsp_ordered_signatures,lsp_resolution_probe,repro_reference_precision,repro_lsp_ordered_local(984/984 PASS)rust_lsp,pipeline(748/748 PASS)bash scripts/check-no-test-skips.sh— PASSmake -f Makefile.cbm lint-no-suppress lint-memory-core lint-vendored-scanners— PASSbash scripts/check-dco.sh upstream/main..HEAD— PASSAuthored with AI assistance on behalf of @GDemay; commits carry Guillaume's DCO sign-off.