llvm / llvm/llvm-project

clangd: C++20 import declarations degrade performance of FrontendAction::Execute

Open
#219,730 8 comments 0 reactions 0 assignees View on GitHub
clang:modules
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

I've observed a significant slowdown of clangd in files that either directly or indirectly (through a header) import a named C++20 module.

For a trivial file like:
```c++
#include

import dummymod;

int main()
{
return 0;
}
```
and the module:
```c++
// dummymod.ixx
export module dummymod;
```
BuildAST takes 750ms, with 680ms spent in FrontendAction::Execute. After hooking up the frontend tracing as well, it looks like the issue is a large amount of calls to ParseDeclarationOrFunctionDefinition (620ms, count 5372).

If I comment out the `import` declaration, BuildAST takes 24ms instead and the frontend tracing registers only 2 counts of ParseDeclarationOrFunctionDefinition taking 100us total.

This slowdown scales up with (preprocessed) content size, BuildAST can take upwards of 5 seconds for a lot of files I work with.
Conversely, for the above example, using a smaller header like `` drops BuildAST down to 108ms (with import, 26ms without)

I primarily tested LSP mode, removing the `import` declaration in `--check` mode doesn't seem to have any impact on runtime.

Version used: `clangd version 24.0.0git (https://github.com/llvm/llvm-project.git 48378de650fc590d905377ec09fddce008c69f73)`

I put together a repository (https://github.com/itzexpoexpo/slow-clangd-repro) with sources, CMakeLists and the full clangd/frontend traces for both with/without the import declaration (as well as the LSP testing jig I used to produce them)

Contributor guide

Open the contributing guide

Research direction

Start with the slow-clangd-repro repository and reproduce the LSP-mode BuildAST slowdown using its sources, CMakeLists, and frontend traces. Compare the paths around FrontendAction::Execute and the repeated ParseDeclarationOrFunctionDefinition calls for files importing a named C++20 module. Done means the import case no longer shows the reported slowdown while preserving the existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.