xoofx / CppAst.NET

CppAst is a .NET library providing a C/C++ parser for header files powered by Clang/libclang with access to the full AST, comments and macros
BSD 2-Clause "Simplified" License
482 stars 63 forks source link

Optimize Attribute Parsing #22

Closed wackoisgod closed 4 years ago

wackoisgod commented 4 years ago

In the previous implementation, the function GetNextLocation is very slow because of its relying on tu.GetLocationForOffset which has to do some extra scanning over the entire TranslationUnit.

Calling this function is actually not required in most cases, because when scanning for attributes there is a common pattern where the attribute is on the same line, so we really can deal with things on a line/column basis instead of a offset into the entire translation unit. LibClang has a function that allows you to get the location based on passing in a line and column instead of an offset. This function is significantly faster.

When using the test code from #20:

var sw = new System.Diagnostics.Stopwatch();
sw.Start();

for (var i = 0; i < 10; ++i)
{
    var options = new CppAst.CppParserOptions().ConfigureForWindowsMsvc(CppAst.CppTargetCpu.X86_64, CppAst.CppVisualStudioVersion.VS2019);
    options.Defines.Add("_ALLOW_COMPILER_AND_STL_VERSION_MISMATCH");
    options.IncludeFolders.Add(@"flatbuffers\include");

    var compilation = CppAst.CppParser.ParseFile(
        cppFilename: @"flatbuffers\include\flatbuffers\flatbuffers.h",
        options:options);
}

sw.Stop();
Console.WriteLine(sw.Elapsed);

The results are as following:

00:00:54.6905552 - without optimizations 00:00:08.4002980 - with optimizations

Should be noted that if you also set ParseSystemIncludes to false in this example the overall time is ~2s

wackoisgod commented 4 years ago

@xoofx any thoughts ?