rust-lang / rust-analyzer

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

Performance degradation when using big tt-muncher `macro_rules` #14101

Open Robbepop opened 1 year ago

Robbepop commented 1 year ago

I recently noticed a big performance degradation regarding rust-analyzer in one of my projects: https://github.com/paritytech/wasmi

The problem is that rust-analyzer consumes much more memory than it has used to and also very often get stuck and shows incorrect warnings and won't finish analysis of the crate and therefore is pretty much unusable for the project. I noticed a very similar behavior of rust-analyzer in the https://github.com/bytecodealliance/wasm-tools repository.

After some backtracking I found that the performance regressions started after merging these PRs:

What do the PRs do? They refactor use of wasmparser's for_each_operator macro which is supposed to be used as tt-muncher macro in order to perform operations on the over 500 different Wasm operators.

My best guess at what portion of wasmi causes the choking for rust-analyzer is probably these code segments:

Note that in particular in https://github.com/paritytech/wasmi/pull/645 we had to increase macro recursion limit in lib.rs:

#![recursion_limit = "750"]

Sometimes I succeed in getting rust-analyzer unstuck by performing a cargo clean on my workspace. However, this is notably not a very neat solution for obvious reasons.

lnicola commented 1 year ago

There are a couple of open issues about MBE performance. I suspect one of the problems is that we convert too often from token trees to a linear form.