[cfe-dev] Tips for optimising libclang

Tobias Grosser tobias at grosser.es
Tue Apr 2 10:45:58 PDT 2013

On 04/02/2013 07:19 PM, Miguel Guedes wrote:
> Hello List,
> I was hoping someone could give me some tips on how to optimise
> creation/reparsing of translation units via libclang as well as code
> completion requests.
> At the moment I'm getting over 1500 ms on average each time a TU is
> created for a control source file I use and only marginally less when
> it's reparsed. Code completion takes on average about 1000 ms (never
> less). This on an i7 with 12GB of memory; system is amd64 Linux 3.5.0-27
> and clang version 3.3 (trunk 170425).
> The project's PCH includes can be viewed on pastebin [0] - STL and Boost
> are among the includes.
> Here's how I'm handling a source file with libclang:
> 1) Create index if it hasn't been created yet:
> 	index = clang_createIndex(0, 0);
> 2) Create TU:
> 	tu = clang_parseTranslationUnit(
> 		index,
> 		filename,
> 		parameters,      [1]
> 		unsavedFiles, /* to keep it simple, I'm not using
> 				  unsaved files at the moment */
> 		0,
> 		CXTranslationUnit_PrecompiledPreamble
>                    | CXTranslationUnit_CacheCompletionResults);
> 3) When requesting code completion:
> 	results = clang_codeCompleteAt(
>      		tu,
>      		filename,
>      		line, col,
>      		unsavedFiles,
>      		0,
>      		CXCodeComplete_IncludeMacros);
> Questions:
> 1) I haven't noticed any improvement in performance when adding more
> source files (creating more TUs) that belong to the same project as the
> control source file I mentioned above. Shouldn't it be the case that
> libclang should reuse the outputs generated when parsing include files
> shared by more than one source file?
> 1.1) If not, can it be done manually?
> 2) How can performance be improved? Before starting this project I was
> hoping for (re-)parsing and code completion times not greater than
> 100ms.

Three points:


When developing clang_complete, we noticed that 
clang_parseTranslationUnit was not enough to initialize the 
PrecompiledPreamble completely. Instead, we had to issue a reparse 
between the initial parse and the codeComplete command. I never found 
out if this was something we did wrong or something that libclang did 
not get right.


Are you sure the file compiles without errors? If there are errors in 
the preamble, no caching will happen.


Where are you code completing? The preamble works only if you don't code 
complete before or directly around the first set of include directives.

With clang_complete I see completion times of around 45 mseconds for 
larger boost based projects. So there seems to be a problem somewhere in 
this setup.


More information about the cfe-dev mailing list