What is rust-analyzer doing on startup?

At my company (Discord, for anyone interested), we have a Cargo workspace with hundreds of members and thousands of external deps.

Cargo itself handles this like a champ. But rust analyzer? That is a different story. On first launch, rust-analyzer takes a good 5-10 minutes using high CPU before it is at all usable. Even if I open a tiny crate with 0 deps, if its in the mega workspace, rust-analyzer is dog slow to startup.

I've set the obvious rust-analyzer.check.workspace = false. This improves responsiveness somewhat, but still only after a very lengthy startup.

If i take the same workspace member, and make it a standalone workspace, and open up vscode in that folder, then rust-analyzer runs like a champ and everything is so snappy.

So I'm trying to understand what all rust-analyzer is doing on startup so that I can avoid it or contribute a patch.

Rust-analyzer first runs cargo metadata to get the project structure. After this you will get a cargo check --all-targets run which is likely mostly cached if this isn't the first time you opened the project in your editor. This to compile proc macros and run build scripts as necessary for rust-analyzer to run various analysis. Next it loads the source code for all crates in the workspace and all dependencies into memory. After that by default it does cache priming which precomputes a bunch of often requested things so you don't have to wait ages when it would first be requested due to user interaction. You can disable this using rust-analyzer.cachePriming.enable. After that your editor likely requests semantic highlighting for the current file which necessitates macro expanding the current crate and running typeck on the current file. This by extension also requires parsing and macro expanding your direct dependencies (and in case of re-exports from indirect dependencies those dependencies) And finally even without semantic highlighting your editor will request errors and warnings for the current file, which also requires some work (though not as much as semantic highlighting as type errors are not yet reported by rust-analyzer)

Disabling cache priming is likely going to make the biggest difference in startup time.

6 Likes

I tried disabling cachePriming... doesn't seem to have made a significant difference in startup time. I also don't notice any obvious difference in responsiveness when hovering over types and whatnot.

I (still) see errors in the output that it is failing to run build scripts for projects in the workspace, but not in the dependency graph of the crate I have open. So it seems that rust-analyzer still wants to run all build scripts, which is not great.

My rust analyzer settings are as follows:

    "rust-analyzer.cargo.targetDir": true,
    "rust-analyzer.check.workspace": false,
    "rust-analyzer.cachePriming.enable": false,

I'll try and snoop exactly what commands are getting run to see if I can learn more.