I’m looking into improving how an autocomplete system at Google computes semantic autocompletions for a given C++ file + line + column. Currently, it’s mostly just calling out to clang_codeCompleteAt, and that’s where we’ve been seeing high latency.
A particular problem we’re having is that when we try to autocomplete just within some scope (that is, just inside a function body, rather than after a . or → etc), calling that method returns all possible autocompletion results, which includes all constants and class names defined in all the transitively included files as far as I can tell. This is technically correct, but in practice takes on the order of seconds to return, which is much longer than the desired timeframe for autocompletion results. So I’d like to know if there’s some way I can get a smaller set of autocompletions more quickly, e.g. by asking clang to complete only on method names and locals, or by giving a time limit to get results. I didn’t see how I might do that in the documentation so I’m wondering if there are some other APIs I’m not seeing.
I took a look at YouCompleteMe and it’s using the same API, so I suspect it might have the same problem. If there’s no way to do something like this using the current API methods, I’d be interested to know how possible/difficult would it be to implement additional methods or options?
I'm looking into improving how an autocomplete system at Google
computes semantic
autocompletions for a given C++ file + line + column. Currently, it's
mostly just calling out to clang_codeCompleteAt
<https://github.com/llvm-mirror/clang/blob/52ed5ec631b0bbf5c714baa0cd83c33eb
fe0c6aa/tools/libclang/CIndexCodeCompletion.cpp#L798>, and that's where
we've been seeing high latency.
A particular problem we're having is that when we try to autocomplete just
within some scope (that is, just inside a function body, rather than after
a . or -> etc), calling that method returns *all* possible autocompletion
results, which includes all constants and class names defined in all the
transitively included files as far as I can tell. This is technically
correct, but in practice takes on the order of seconds to return, which is
much longer than the desired timeframe for autocompletion results.
How many results do you get after this delay? Using KDevelop to work on code,
I've never seen such long delays just for the code completion stage. What
takes a long time is the initial parse with clang_parseTranslationUnit2. I
hope you guys have some sort of daemon which can keep the TU alive? Make sure
you pass CXTranslationUnit_PrecompiledPreamble, but note that the current
stable releases of clang won't actually save the precompiled preamble to disk
until you call clang_reparseTranslationUnit at least once. It's easy to check,
just look at /tmp and see whether you have preamble-*.pch files in there. Once
you do all of that, you can easily get an order of magnitude increase in
performance.
So I'd
like to know if there's some way I can get a smaller set of autocompletions
more quickly, e.g. by asking clang to complete only on method names and
locals, or by giving a time limit to get results. I didn't see how I might
do that in the documentation
<http://clang.llvm.org/doxygen/group__CINDEX__CODE__COMPLET.html#ga50fedfa85
d8d1517363952f2e10aa3bf> so
I'm wondering if there are some other APIs I'm not seeing.
I haven't seen such an API either. How do you envision it? Some filter
callback that one could pass to the clang_codeCompleteAt call?
Adding methods isn't that hard, it just takes time (which I personally lack
atm to advance my personal WIP patch). Just make sure you build an API that is
future proof and stable, and don't forget your tests.
What we do in https://github.com/Andersbakken/rtags is to preemptively generate completion results whenever the cursor is moved (with a short delay) and then cache them for faster lookup. This seems to help a little bit at least.