chetant / LibClang

Haskell FFI to libclang
Other
58 stars 10 forks source link

API cleanup and massive performance improvements #40

Closed sethfowler closed 10 years ago

sethfowler commented 10 years ago

After determining that LibClang was a bottleneck in my application I was able to get huge performance improvements (at least 5x) by optimizing LibClang.

The most important gains came from new traversal functions, implemented in C, that collect all of the children or descendants of an AST node and return the cursors to Haskell as a vector. This eliminated the need for visitor functions that call repeatedly into Haskell from C, which was a major performance bottleneck.

Other performance improvements came from more efficient marshaling using Storable and Vector, and inlining of frequently-called functions. (Some of the most frequently called were rewritten in Haskell to avoid all FFI overhead.)

Finally, the eliminating the visitor callbacks made it possible to cleanly replace the ClangApp monad with the ClangT monad transformer. Code using the new API is much more idiomatic and can run on top of arbitrary monads. Although this doesn't directly improve performance, it makes it much easier to implement application-level optimizations like caching that are much more convenient when built on top of the State or Reader monads.

sethfowler commented 10 years ago

This has been cooking for a while now, and I wanted to work on it in my own repo until it was baked. I think things are now at the point where I'm happy with the performance and the general API is in a good place.

The next step is to finish the remaining tasks listed in other issues (add docs, implement the rest of the functions from LLVM 3.4 which is now out, etc.) and get a release ready. I'll probably go ahead and work on those things directly in this repo.

sethfowler commented 10 years ago

Looks like this caused a build failure, which I'll investigate later tonight. It builds fine on OS X/clang, so I'm guessing the failure involves Linux or gcc.

sethfowler commented 10 years ago

The build failure is due to lack of support for stdalign.h on the build machine. We can live without this, but I'd rather have the option to use it, so I'll see if I can detect this feature (which is part of C11).

sethfowler commented 10 years ago

None of my machines have a compiler that defines __STDC_VERSION__ to indicate C11 support. I could work around this, but I don't want to check in code I can't test. There's not much point since we don't use the alignment values right now anyway. For now, I've simply hardcoded those values and removed all usage of the stdalign.h header.

sethfowler commented 10 years ago

The build failed again, this time due to a lack of accurate constraints on the hashable package dependency. (There are major API changes between hashable 1.1.x and hashable 1.2.x.) My newest push should fix that problem.

sethfowler commented 10 years ago

Alright, the build is green again!