rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.24k stars 1.6k forks source link

Improve `Semantics` construction performance #17116

Open Veykril opened 6 months ago

Veykril commented 6 months ago

hir::Semantics is a general cache used to be able to resolve any syntax node to its semantic definition. This is generally slow given the kind of work it does, but quite often we do a lot more work than necessary, populating unnecessary DynMap tables and more importantly, we populate it via parsing, where the parsing happens through direct database calls, even though the Semantics itself caches these. That means for big enough files we can end up with thrashing the LRU cache a fair bit. We should fix that, a related problem is the HasSource trait going through the database instead of Semantics which means we can end up with nodes not cached by Semantics ultimately ending up in panics when it tries to lookup stuff (which has happened in the past).

So the things that need to be changed are:

This will most likely require adding callbacks to some of the hir-def crate APIs to abstract away the parsing (and then add / keep the current APIs as convenience APIs where Semantics won't be involved).

lnicola commented 6 months ago

Mostly unrelated, but in the past we used to construct multiple Semantics during the execution of a single request. It might be worth checking whether that's still the case.

Kohei316 commented 4 months ago

@rustbot claim