[cfe-dev] Expression Templates for AST matchers

Samuel Benzaquen sbenza at google.com
Thu Apr 9 14:31:06 PDT 2015


On Thu, Apr 9, 2015 at 1:15 PM, Richard <legalize at xmission.com> wrote:

>
> In article <CAOsfVvmev4Y7Q=Wr=
> 8RJQFs49S94J0OE97-iJyaWRZVvCD60Jg at mail.gmail.com>,
>     Manuel Klimek <klimek at google.com> writes:
>
> > We actually went from a more expression templatey to a less expression
> > templatey design.
>
> Interesting!  So far, I feel like what I've been trying to match is
> completely expressable at compile-time and deferring things to runtime
> is making things slow.


What I found is that using type erasure actually sped up clang-tidy
considerably.
The template approach we used at the beginning was causing a huge amount of
code to be generated. More code == slower code.
Type erasure allowed to collapse common things into a single implementation.
In any case, most of the matching happens dynamically anyway because the
type of the children of nodes is not statically known. They are Expr or
Decl and we have to dyn_cast<> them anyway.

I optimized the runtime cost of the matchers until they stopped being the
bottle neck.
Right now, most of the runtime comes from the parsing phase, memory
allocation due to caching and hits found in uninteresting code.
It doesn't seem that running the matchers themselves is the slow section
anymore.
For most compilation units, parsing is still more than half the walltime.


>   This seems to be consistent with the findings
> of the imlpementors of OCLint and why they make the statement I quoted
> earlier.
>
> > On Tue, Apr 7, 2015, 3:31 PM Sean Silva <chisophugis at gmail.com> wrote:
> >
> > > Although I'm mostly an armchair spectator right now w.r.t. clang
> tooling
> > > stuff, I think that use cases like clang-query and in general the
> dynamic
> > > AST matchers suggests that expression templates aren't a very good
> solution
> > > because they will require reimplementing things from scratch for the
> > > "dynamic" case.
>
> When I look at a library like Boost.Spirit that heavily uses
> expression templates I don't see how I'm losing the dynamic case at
> all.  Maybe I'm misunderstanding what you're saying.
>
> > Sam has worked on making clang tidy use cases faster. I
> > expect in the end the right thing is to do less duplicate work which I
> > think we will be able to do with modules.
>

I have done work on many fronts, like:
 - Skip redundant work. Eg: filter the matchers by type and run only the
ones that are compatible.
 - Reduce overhead of the system. Eg: collapse virtual functions to avoid
multiple consecutive vtable jumps.
 - Extract common ops: Eg: move the type checking outside of the virtual
dispatch.


>
> While modules are nice, I don't think it's reasonable to expect that
> modules will be deployed over a significant chunk of the world's C++
> code anytime soon.  So it doesn't seem practical to say "modules will
> fix this".
>
> > > Regardless, I don't think that the primary bottleneck is not doing
> enough
> > > at compile time (of the matchers). Rather we aren't effectively
> indexing
> > > the underlying data set we are trying to query (right now, we're more
> like
> > > grep and less like google).


Right now we do a single pass on the AST to run all checks.
Adding an indexing step would not help much, unless we can do it directly
in the parsing.
Maybe with modules we could add an index into the module and avoid the full
AST traversal.


> [...] (a
> > > good example is using a trigram index to prune the search space for
> regexp
> > > matching http://swtch.com/~rsc/regexp/regexp4.html).
> > >
> > > * "multiple" here also scales numerically with the number of TU's. An
> > > index can avoid visiting certain TU's at all in many cases.
>
> Thanks for that link Sean, that was an interesting read.  I couldn't
> find it "live" on the web, but was able to get it via the wayback
> machine on archive.org.
> --
> "The Direct3D Graphics Pipeline" free book <
> http://tinyurl.com/d3d-pipeline>
>      The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
>          The Terminals Wiki <http://terminals.classiccmp.org>
>   Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150409/d33a9951/attachment.html>


More information about the cfe-dev mailing list