xref: /llvm-project/clang-tools-extra/clangd/support/FileCache.cpp (revision 2c675be9b232c1d0b5c55cbcb196e71036c681ea)
1d95db169SSam McCall //===--- FileCache.cpp ----------------------------------------------------===//
2d95db169SSam McCall //
3d95db169SSam McCall // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d95db169SSam McCall // See https://llvm.org/LICENSE.txt for license information.
5d95db169SSam McCall // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d95db169SSam McCall //
7d95db169SSam McCall //===----------------------------------------------------------------------===//
8d95db169SSam McCall 
9d95db169SSam McCall #include "support/FileCache.h"
104d006520SSam McCall #include "llvm/ADT/ScopeExit.h"
11*2c675be9SKazu Hirata #include <optional>
12d95db169SSam McCall 
13d95db169SSam McCall namespace clang {
14d95db169SSam McCall namespace clangd {
15d95db169SSam McCall 
16d95db169SSam McCall // Sentinel values for the Size cache key. In both cases, a successful stat of
17d95db169SSam McCall // the file will never result in the cached value being reused.
18d95db169SSam McCall 
19d95db169SSam McCall // The cached value does not reflect the current content on disk.
20d95db169SSam McCall static constexpr uint64_t CacheDiskMismatch =
21d95db169SSam McCall     std::numeric_limits<uint64_t>::max();
22d95db169SSam McCall // The cached value reflects that the file doesn't exist.
23d95db169SSam McCall static constexpr uint64_t FileNotFound = CacheDiskMismatch - 1;
24d95db169SSam McCall 
FileCache(llvm::StringRef Path)25d95db169SSam McCall FileCache::FileCache(llvm::StringRef Path)
26d95db169SSam McCall     : Path(Path), ValidTime(std::chrono::steady_clock::time_point::min()),
27d95db169SSam McCall       ModifiedTime(), Size(CacheDiskMismatch) {
28d95db169SSam McCall   assert(llvm::sys::path::is_absolute(Path));
29d95db169SSam McCall }
30d95db169SSam McCall 
read(const ThreadsafeFS & TFS,std::chrono::steady_clock::time_point FreshTime,llvm::function_ref<void (std::optional<llvm::StringRef>)> Parse,llvm::function_ref<void ()> Read) const31d95db169SSam McCall void FileCache::read(
32d95db169SSam McCall     const ThreadsafeFS &TFS, std::chrono::steady_clock::time_point FreshTime,
33*2c675be9SKazu Hirata     llvm::function_ref<void(std::optional<llvm::StringRef>)> Parse,
34d95db169SSam McCall     llvm::function_ref<void()> Read) const {
35d95db169SSam McCall 
36d95db169SSam McCall   std::lock_guard<std::mutex> Lock(Mu);
37d95db169SSam McCall   // We're going to update the cache and return whatever's in it.
38d95db169SSam McCall   auto Return = llvm::make_scope_exit(Read);
39d95db169SSam McCall 
40d95db169SSam McCall   // Return any sufficiently recent result without doing any further work.
41d95db169SSam McCall   if (ValidTime > FreshTime)
42d95db169SSam McCall     return;
43d95db169SSam McCall 
44d95db169SSam McCall   // Ensure we always bump ValidTime, so that FreshTime imposes a hard limit on
45d95db169SSam McCall   // how often we do IO.
46d95db169SSam McCall   auto BumpValidTime = llvm::make_scope_exit(
47d95db169SSam McCall       [&] { ValidTime = std::chrono::steady_clock::now(); });
48d95db169SSam McCall 
49d95db169SSam McCall   // stat is cheaper than opening the file. It's usually unchanged.
50d95db169SSam McCall   assert(llvm::sys::path::is_absolute(Path));
51059a23c0SKazu Hirata   auto FS = TFS.view(/*CWD=*/std::nullopt);
52d95db169SSam McCall   auto Stat = FS->status(Path);
53d95db169SSam McCall   if (!Stat || !Stat->isRegularFile()) {
54d95db169SSam McCall     if (Size != FileNotFound) // Allow "not found" value to be cached.
55059a23c0SKazu Hirata       Parse(std::nullopt);
56d95db169SSam McCall     // Ensure the cache key won't match any future stat().
57d95db169SSam McCall     Size = FileNotFound;
58d95db169SSam McCall     return;
59d95db169SSam McCall   }
60d95db169SSam McCall   // If the modified-time and size match, assume the content does too.
61d95db169SSam McCall   if (Size == Stat->getSize() &&
62d95db169SSam McCall       ModifiedTime == Stat->getLastModificationTime())
63d95db169SSam McCall     return;
64d95db169SSam McCall 
65d95db169SSam McCall   // OK, the file has actually changed. Update cache key, compute new value.
66d95db169SSam McCall   Size = Stat->getSize();
67d95db169SSam McCall   ModifiedTime = Stat->getLastModificationTime();
68d95db169SSam McCall   // Now read the file from disk.
69d95db169SSam McCall   if (auto Buf = FS->getBufferForFile(Path)) {
70d95db169SSam McCall     Parse(Buf->get()->getBuffer());
71d95db169SSam McCall     // Result is cacheable if the actual read size matches the new cache key.
72d95db169SSam McCall     // (We can't update the cache key, because we don't know the new mtime).
73d95db169SSam McCall     if (Buf->get()->getBufferSize() != Size)
74d95db169SSam McCall       Size = CacheDiskMismatch;
75d95db169SSam McCall   } else {
76d95db169SSam McCall     // File was unreadable. Keep the old value and try again next time.
77d95db169SSam McCall     Size = CacheDiskMismatch;
78d95db169SSam McCall   }
79d95db169SSam McCall }
80d95db169SSam McCall 
81d95db169SSam McCall } // namespace clangd
82d95db169SSam McCall } // namespace clang
83