[cfe-dev] Avoiding unnecessary calls to Decl::getASTContext

Bruno Ricci via cfe-dev cfe-dev at lists.llvm.org
Sat Dec 15 08:27:37 PST 2018


Hi all,

I have been looking at Decl::getASTContext and DeclContext::getParentASTContext,
and it turns out that they are not that cheap.

The reason for this is that they must walk back to the translation unit declaration
along what is essentially a linked list. This is amplified by the fact that it does
look like a simple getter and therefore is called extremely often (the most egregious
case by far being DeclContext::lookup), even when most of the times the context is
already easily available.

To put some numbers on this I experimented with removing about half of the iterations
in Decl::getTranslationUnitDecl. This gives a speed up of about 1% when parsing all
of Boost. Extrapolating from this I get that about 1.5%-2% of the time spent
parsing Boost is spent getting the ASTContext, which seems a bit unfortunate.

Now to be clear 1.5%-2% is not that much, but the fix is simply to pass a ref to
the ASTContext more often as a function parameter. The downside is that this would
cause a some amount of churn in the C++ api. To be clear I am *not* suggesting
removing all of the calls to Decl::getASTContext, but only removing the ones causing
the most iterations in Decl::getTranslationUnitDecl (say the top 10 or 20).

What do you think ?

Bruno


More information about the cfe-dev mailing list