Rust Intelligence 以 rustc 索引降低 Agent 工作區的 Rust 分析開銷
You Gave Every Agent Its Own Worktree. You Also Gave Each One Its Own rust-analyzer.
Alexander Panasenko 開發 Rust Intelligence,以 cargo check 產生的 rustc 索引供語言伺服器查詢,減少各工作區重複載入分析結果。
TL;DR. In my setup, one worktree per agent meant one language server per agent, each with its own copy of the same analysis. I tried to share rust-analyzer between worktrees twice, and both attempts half-worked. Then I built a language server that reads what rustc writes down during
cargo check. In one single-session run on Bevy with the index already built, it peaked at 917 MB and, after startup, got through the session's 1,635 requests in 5.5 seconds; rust-analyzer peaked at 11.3 GB and took 114.4 seconds. Several servers running at once are not measured yet.
git worktree add took between 1.1 and 1.7 seconds. The first workspace-wide symbol search in that fresh worktree took rust-analyzer 66.97 seconds. The same search again took 0.10. That was July 2026, on a private workspace of 431 Rust files.
An agent that has just been handed a worktree starts with a question of that kind, and it starts cold every time. But the minute is the smaller part of the bill. A worktree is a separate directory, so in my setup it got a separate language server, and every server built its own complete picture of the project in memory: the same dependencies, the same standard library, the same code no agent had touched. In the run further down, a single rust-analyzer session peaked at 1.5 GB on serde and 11.3 GB on Bevy.
On 26 September the bill arrived. One of my build machines, with 64 GiB of RAM, had 14 code-analysis engines loaded and sat above 85% memory. Requests hung for minutes, and then the kernel's OOM killer shot the service (the public issue). Those 14 were engines for every language, and I did not record how many of them were Rust. What I did know was how prod-code, the service that answers my agents' questions about code, handled Rust: it embeds rust-analyzer's analysis as a library, and it started one Rust engine per worktree copy. That was the part that grew with every agent.
"One worktree, one language server" is a habit, not a law. It took me two attempts to see which part of the habit was wrong.
Attempt one: one daemon, private overlays
In July I tested analyzed 0.4.2: one shared rust-analyzer world, and each session gets a private overlay with its own changed files.
The model held. Across 3,950 queries with up to 20 sessions, none returned another session's code. The operations did not hold. With five sessions, a new overlay waited 12 to 47 seconds, and after every session had closed the daemon still held between 9 and 21.6 GiB. That is version 0.4.2 in July; I have not retested later ones.
The plan recorded that week ruled out building a separate semantic index and merging its results above LSP.
Attempt two: teach rust-analyzer to share
After the OOM I forked rust-analyzer. In the fork, a base checkout and its worktree copies share every crate whose files, build-script output and dependencies are identical, and only what differs is analysed again.
I measured it on rust-analyzer's own repository: 328 crates, about 1.8 GB for the base, with build scripts and proc macros off. A second plain copy cost about 520 MB more. With sharing, an unchanged copy cost 25 to 40 MB, and sixteen copies, each with an edit in a leaf crate, fit in about 2.07 GB in total.
It worked, and it was about 9,000 lines through some of the most intricate parts of rust-analyzer. I sent it upstream as one pull request, and it was closed two days later. The maintainers' reasons were fair: I had not read the project's AI policy and the description broke it, 9,000 lines is not a first contribution, and I had not discussed any of it with the team, who have had their own discussion of a shared server open since July (#22765). Carrying a patch that size on top of a moving codebase was not a plan either.
The question I had been asking wrong
Both attempts asked how several worktrees could share the analyzer's memory. Neither asked why that memory exists.
rust-analyzer analyses Rust on its own. It parses, resolves names, expands macros and infers types, and it keeps the result in RAM. rustc does all of that too, on every cargo check. In my workflow cargo check was already part of the loop: rust-analyzer runs it on save by default to show the compiler's errors (checkOnSave), and my agents run it to see whether a change compiles. The compiler works out where every name points and what type every expression has, reports the errors and exits. Nothing keeps those answers in a form an editor can ask.
Rust Intelligence keeps them. ridx index runs cargo check with a small rustc driver that, after analysis, writes one binary file per crate: definitions, every resolved reference, types, impls. A method call points at the impl rustc selected, not at a guess by name. The language server maps those files from disk and answers lookups in them. It does not hold a second compiler in memory, because it is not one.
Build time on the left, edit time on the right. Cargo checks crates as needed, and worktrees reuse a dependency's index file when their keys match. From the architecture document.
In July the plan had ruled out a separate index. What changed is who writes it: the compiler, as a by-product of a check that was already running.
The compiler had already answered. Nobody was keeping the answers.
What one session costs
The same scripted editor session against both servers: 40 files of each repository opened one by one, with symbols, semantic tokens and inlay hints, then hover, definition, references and completion at sampled positions, and an edit.
| Repository | Peak memory, Rust Intelligence / rust-analyzer | Time for the requests, after startup | CPU for the requests |
|---|---|---|---|
| serde | 72 MB / 1.5 GB | 2.43 s / 5.40 s | 2.34 s / 6.97 s |
| tokio | 221 MB / 3.2 GB | 2.03 s / 28.1 s | 1.90 s / 35.9 s |
| rust-analyzer | 434 MB / 4.6 GB | 4.59 s / 52.3 s | 4.44 s / 66.7 s |
| bevy | 917 MB / 11.3 GB | 5.54 s / 114.4 s | 5.38 s / 170.5 s |
Read it with its conditions. This is one session per server and repository, run once, on Linux with a Ryzen 9 7950X and 64 GB, with Rust Intelligence at commit f7105fcc; the rust-analyzer version was not recorded. The index was built beforehand, which takes minutes on a large workspace the first time, and that time is not in the table, while rust-analyzer's own analysis is in its numbers. rust-analyzer answered 26 to 31 requests per repository with an error and Rust Intelligence none; I did not record what the errors were, so that says nothing about the quality of either server's answers. The table says what each server spent on the same requests, 1,635 of them on Bevy. Not every kind of request was faster: on Bevy the median references and documentHighlight were quicker on rust-analyzer, and the totals above are for the whole session. It does not say the answers were identical. And it is one session: several servers at once, indexing in parallel and the physical memory of a whole machine are not in it. The raw results and the script are in the repository.
What sixteen worktrees would and would not get
The first objection from anyone who runs agents is not about one session. It is: I have sixteen worktrees that diverge, some of them change Cargo.toml or features; what happens to the machine?
What the design gives them. The index file of a registry or git dependency is keyed by what it was compiled from and how: version, target, cfg flags, toolchain and the crates it was built against. Worktrees that build a dependency the same way reuse that file instead of writing it again, and a worktree that changes features or Cargo.toml gets new files only for the dependencies the change reaches.
And most of the index stays in memory-mapped files, which the kernel can page out and read back. The server keeps derived tables on the heap as well; the peak in the table above counts both, and I have not measured the split.
What it does not give them. Sixteen cargo check runs are still sixteen cargo check runs: every worktree checks its dependencies and its own crates in its own target directory, and the first index of a large workspace takes minutes. In 0.3.9 a worktree also writes the index of all its own workspace crates, including the ones identical to a sibling worktree's; sharing those is planned, not shipped. And I have not yet put sixteen of these servers on one machine and measured the total, the way I did with the fork. Until I do, the claim is a mechanism and one session, not a fleet.
"A language server must work while the code is broken"
This is the strongest objection to the whole idea, and it is why rust-analyzer is built the way it is. It answers on every keystroke, in code that does not compile, without waiting for a build. An index written by the last cargo check is one build behind by definition.
In 0.3.9, hover, completion, inlay hints and type errors for the code you are typing come from a small compiler-free front end that reads everything else from the index. References, rename and navigation catch up after a compile: once the edited buffers have been still for 250 ms, the crates that own them are compiled again from the buffers' text into an index for the session, and nothing is written to your files. Since 0.3.11 the compiler's own errors for the unsaved text arrive with that compile, not only after a save. The 250 ms is the pause before that compile starts, not the answer time. From an edit to a references answer that includes it, the project's own measurement gives a median of 0.36 s on a one-file crate and 3.31 s on a file of one of its library crates (integration notes, §4); until then the answer comes from the previous index. A crate with compile errors is indexed from everything that did resolve, inside the bodies with errors too. Two gaps remain: a crate that depends on the edited one sees the change only after a save, and a buffer the compiler cannot parse stays on its crate's previous index. That is a compile per pause, not an answer per keystroke. And for every file you have not touched, the answer is as fresh as the last check. The driver is pinned to one toolchain, Rust 1.98.1, because it links to the compiler's private libraries. There is no Windows and no Intel macOS build. Agreement with rust-analyzer is measured per method and recorded in the changelog, for example 95.5% for completion and 82.2% for references in 0.3.0, but that suite was not re-run for 0.3.7, 0.3.8 or 0.3.9.
If your day is one editor on one checkout with memory to spare, rust-analyzer's trade is a good one. An agent's day is different: it edits a few files, runs cargo check anyway, and mostly asks about code it did not write.
Check it on your own worktrees
Start with what you already run. Does your orchestrator or editor start one language server per worktree? After the first question in each, how much memory is every one of them holding?
Then compare on two working copies of your own repository. On Linux (x86_64, arm64) or macOS on Apple Silicon, with rustup:
curl -fsSL https://rust-intelligence.com/install.sh | sh
ridx compare puts an answer side by side with rust-analyzer's on your code, so you do not have to take my table on trust. ridx accepts rust-analyzer's command line, so an editor already set up for rust-analyzer can start it instead; in VS Code that is "rust-analyzer.server.path": "ridx". An agent does not need an editor session at all: ridx def, ridx refs and ridx hover answer from the shell.
Before you buy RAM for the next ten agents, count the language servers already running on that machine and ask what each one is holding. On mine, each was holding one more copy of what the compiler works out on every check.
Source: github.com/alex09x/rust-intelligence, MIT OR Apache-2.0. Cite: 10.5281/zenodo.24379816, which resolves to the latest archived version.
Originally published at prod.codes.
來源:DEV Community · dev.to