This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Extract per-dir CDB cache to its own threadsafe class. NFC
ClosedPublic

Authored by sammccall on Dec 1 2020, 3:50 AM.

Details

Summary

This is a step towards making compile_commands.json reloadable.

The idea is:

  • in addition to rare CDB loads we're soon going to have somewhat-rare CDB reloads and fairly-common stat() of files to validate the CDB
  • so stop doing all our work under a big global lock, instead using it to acquire per-directory structures with their own locks
  • each directory can be refreshed from disk every N seconds, like filecache
  • avoid locking these at all in the most common case: directory has no CDB

Diff Detail

Event Timeline

sammccall created this revision.Dec 1 2020, 3:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 1 2020, 3:50 AM
sammccall requested review of this revision.Dec 1 2020, 3:50 AM
sammccall added inline comments.Dec 1 2020, 3:57 AM
clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
89

All these members feel pretty messy, there's definitely some redundancy (e.g. CachePopulated is equivalent to CDB || FinalizedNoCDB at the moment).

Basically this is an intermediate state: CachePopulated will become the validity time of the cached value (which *is* meaningful when CDB is set).

I can try to polish this code in its own right, or optimize for a smooth transition to cache/expiry, but probably not both at once.

kadircet added inline comments.Dec 1 2020, 6:20 AM
clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
91

maybe rename this to DidBroadcast if we are not planning to add extra meaning to Dirty in the near future?

119

what if caller is not willing to broadcast?

259

won't this overwrite ShouldBroadcast ?

for example if we get a query with ShouldBroadcast set to true, but first searchdir doesn't have a CDB underneath, for all the consecutive calls this will be false.

271–273

stale comment.

clang-tools-extra/clangd/GlobalCompilationDatabase.h
88

it is kind of hard to figure out if this mutex is also meant for OnlyDirCache. maybe separate it with a line or put some explicit governed by annotations?

89

some comments about semantics of OnlyDirCache ? I suppose existing of it implies DirCaches won't be used at all ?

sammccall updated this revision to Diff 309474.Dec 4 2020, 12:38 AM
sammccall marked 6 inline comments as done.

Address review comments

sammccall added inline comments.Dec 4 2020, 12:46 AM
clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
91

Renamed to NeedsBroadcast.

(DidBroadcast isn't quite right once we can reload databases and thus need to broadcast again)

119

Good catch, thanks.

Changed the handling of ShouldBroadcast to always flow via NeedsBroadcast, which is set by load().

kadircet accepted this revision.Dec 9 2020, 1:37 AM

thanks, lgtm!

clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
79

nit: mark it const ?

117

nit: early exit

128

nit: maybe stash modification of control signals to load() rather than having them split between load() and get() (or alternatively just change the load to return a {shared,unique}_ptr while making it a free function and then perform all the signal modifications inside get)

This revision is now accepted and ready to land.Dec 9 2020, 1:37 AM
sammccall marked 2 inline comments as done.Dec 9 2020, 8:39 AM
sammccall added inline comments.
clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
79

Yeah, Done. I don't usually like const on members but all the other state is so complicated here and we definitely aren't movable and don't want to be.

128

Moved them all into get to isolate the complexity somewhat. We can in fact infer NeedsBroadcast from the old/new CDB pointer.

Unfortunately making load a free function falls apart in the next patch as load gets complicated:

  • it needs to own the state of "which cached file corresponds to the current CDB" as well as the CDB itself
  • it needs to access and update the file cache state itself
  • it has a one-off query of the CachePopulated flag to avoid reloading plugin-based CDBs

It's of course *possible* to make it a free function but the signature is such a mess that I don't think it buys anything.

This revision was landed with ongoing or failed builds.Dec 9 2020, 8:48 AM
This revision was automatically updated to reflect the committed changes.
sammccall marked an inline comment as done.
thakis added a subscriber: thakis.Dec 9 2020, 12:08 PM

Looks like this breaks tests on Windows: http://45.33.8.238/win/29513/step_9.txt