Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

irony-cdb-json/irony-server: Caches last used compilation database #499

Merged
merged 1 commit into from
Jan 25, 2019

Conversation

itollefsen
Copy link
Contributor

A compilation database can be very large and take a long time to read
and parse, which happens quite frequently (when opening a file for
instance).

This caches the last read/parsed compilation database based on its
filename and last modification time.

This could be extended to cache N number of databases instead of just
one, which would be useful when working on multiple projects
simultaneously.

Copy link
Owner

@Sarcasm Sarcasm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for taking so long to review the code.
I requested some changes before the merge, most notably to respect the fact that libclang CompilationDatabase is more than just JSONCompilationDatabase.

clang_CompilationDatabase_fromDirectory(buildDir.c_str(), &error);
bool dbLoaded;
CXCompilationDatabase db;
std::tie(dbLoaded, db) = cdbCache_.getCompilationDatabase(buildDir);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how better it is, but I feel like the caching could have the same API as the Clang API.

That is, it could have a signature such as:

CXCompilationDatabase
CompDBCache::fromDirectory(const char *BuildDir,
                           CXCompilationDatabase_Error *ErrorCode);

So the caching is just decoration/optimisation that can be used as a drop-in replacement.

The cache only deals with caching and not with writing S-Expr results.

@@ -133,6 +134,20 @@ class Irony {
void computeCxUnsaved();

private:
class CDBCache {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very fond of inner classes, I would prefer a top-level CompDB[Cache] class.

The class could mirror the libclang naming:

  • getCompilationDatabase could be renamed fromDirectory.

@@ -124,7 +125,7 @@ class Irony {
/// \endcode
///
void getCompileOptions(const std::string &buildDir,
const std::string &file) const;
const std::string &file);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I would drop the const here, instead I would make the cache variable mutable, caching is a valid use case for mutable IMHO.

clear();
}

std::tuple<bool, CXCompilationDatabase> Irony::CDBCache::getCompilationDatabase(const std::string &buildDir) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CompilationDatabase does not necessarily imply JSONCompilationDatabase.
Clang abstracts a CompilationDatabase, which can be a JSONCompilationDatabase, but it can also be something else.

So I would reorganize the code so that the logic is like this:

  • stat $build_dir/compile_commands.json
  • if stat worked
    • if it matches what's the cache variables (path, mtime, ...), return the cache result
    • else, call clang_fromDirectory, cache the result and return it
  • else, if stat did not work, maybe it's another kind of compilation database, just return the result from clang_fromDirectory, no caching

Using this logic does not hinder the normal behavior of libclang, but if we found a compile_commands.json, we are faster.

server/src/Irony.cpp Outdated Show resolved Hide resolved
@itollefsen
Copy link
Contributor Author

I read the feedback and then forgot all about it. It's been a busy winter.

Anyway, I've now tried to incorporate the changes you requested. Including splitting the cache class from the Irony class and putting it in its own translation unit(s).

As for other formats - yes, I had completely forgotten about them. Now remedied. Although I think it would be a good (future) improvement to add support for caching more formats (it currently only caches results for JSON compilation databases, as you noted).

Copy link
Owner

@Sarcasm Sarcasm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update!

I suggested a few changes which I believe could improve readability, but it looks good already, so thank you!

As for other formats - yes, I had completely forgotten about them. Now remedied. Although I think it would be a good (future) improvement to add support for caching more formats (it currently only caches results for JSON compilation databases, as you noted).

I know only about one, a compile_flags.txt similar to the .clang_complete irony mode already supports:

I don't think caching this file is relevant.

irony-cdb-json.el Outdated Show resolved Hide resolved
server/src/CompDBCache.cpp Outdated Show resolved Hide resolved
server/src/CompDBCache.cpp Show resolved Hide resolved
Copy link
Owner

@Sarcasm Sarcasm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm so sorry, one little very minor change and good to go!

irony-cdb-json.el Outdated Show resolved Hide resolved
A compilation database can be very large and take a long time to read
and parse, which happens quite frequently (when opening a file for
instance).

This caches the last read/parsed compilation database based on its
filename and last modification time.
@Sarcasm Sarcasm merged commit 93b91d7 into Sarcasm:master Jan 25, 2019
@Sarcasm
Copy link
Owner

Sarcasm commented Jan 25, 2019

Thank you for your patience and for the change itself, which is quite useful!

@itollefsen itollefsen deleted the cache-last-used-cdb branch January 25, 2019 12:47
sten0 added a commit to sten0/irony-mode that referenced this pull request Dec 10, 2021
Release 1.4.0

In this release:

- cache compilation database [SarcasmGH-499]
- add support for new libclang versions,
  which should now support newer versions automatically
- CMake modernization, require CMake >= 3
- improved compile options adjustement [SarcasmGH-542]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants