@ChuanqiXu thank you for your time!
I tried the patch at: [C++20] [Modules] Don't load declaration eagerly for named modules · llvm/llvm-project@af86957 · GitHub. It significantly reduced the time spent in Module Load, however WriteAst still takes the same time. I was unable to create a minimal reproducer that highlights this specific issue.
As a test, I ran this file through callgrind with and without import really_big_module:
module;
#include <iostream>
export module test;
import really_big_module;
export {
void hello() {
std::cout << "hello\n";
}
}
The number of calls to ASTWriter::WriteDecl, ASTDeclWriter::Visit, and DeclContext::lookup stayed nearly the same. The most significant difference is that ASTReader::FindExternalVisibleDeclsByName gets called often when using import. I noticed that the call chains while reading the AST are deep. I tried to print the Declaration it visits by modifying the AST reader, however, I only got crashes.
I created a repo that highlights this issue, note this is neither minimal nor beautiful and only a bad port to C++ Modules, but it could help to find the root cause of this issue. Please notify me if there are problems building this.