clang-tools 19.0.0git
Go to the documentation of this file.
1//===--- FileCache.h - Revalidating cache of data from disk ------*- C++-*-===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
12#include "Path.h"
13#include "ThreadsafeFS.h"
14#include "llvm/Support/Chrono.h"
15#include <mutex>
16#include <optional>
18namespace clang {
19namespace clangd {
21/// Base class for threadsafe cache of data read from a file on disk.
23/// We want configuration files to be "live" as much as possible.
24/// Reading them every time is simplest, but caching solves a few problems:
25/// - reading and parsing is cheap but not free (and happens on hot paths)
26/// - we can ignore invalid data and use the old value (we may see truncated
27/// compile_commands.json from non-atomic writers)
28/// - we avoid reporting the same errors repeatedly
30/// We still read and parse the data synchronously on demand, but skip as much
31/// work as possible:
32/// - if not enough wall-time has elapsed, assume the data is still up-to-date
33/// - if we stat the file and it has the same mtime + size, don't read it
34/// - obviously we only have to parse when we re-read the file
35/// (Tracking OS change events is an alternative, but difficult to do portably.)
37/// Caches for particular data (e.g. compilation databases) should inherit and:
38/// - add mutable storage for the cached parsed data
39/// - add a public interface implemented on top of read()
40class FileCache {
42 // Path must be absolute.
45 // Updates the cached value if needed, then provides threadsafe access to it.
46 //
47 // Specifically:
48 // - Parse() may be called (if the cache was not up-to-date)
49 // The lock is held, so cache storage may be safely written.
50 // Parse(None) means the file doesn't exist.
51 // - Read() will always be called, to provide access to the value.
52 // The lock is again held, so the value can be copied or used.
53 //
54 // If the last Parse is newer than FreshTime, we don't check metadata.
55 // - time_point::min() means we only do IO if we never read the file before
56 // - time_point::max() means we always at least stat the file
57 // - steady_clock::now() + seconds(1) means we accept 1 second of staleness
58 void read(const ThreadsafeFS &TFS,
59 std::chrono::steady_clock::time_point FreshTime,
60 llvm::function_ref<void(std::optional<llvm::StringRef>)> Parse,
61 llvm::function_ref<void()> Read) const;
63 PathRef path() const { return Path; }
66 std::string Path;
67 // Members are mutable so read() can present a const interface.
68 // (It is threadsafe and approximates read-through to TFS).
69 mutable std::mutex Mu;
70 // Time when the cache was known valid (reflected disk state).
71 mutable std::chrono::steady_clock::time_point ValidTime;
72 // Filesystem metadata corresponding to the currently cached data.
73 mutable llvm::sys::TimePoint<> ModifiedTime;
74 mutable uint64_t Size;
77} // namespace clangd
78} // namespace clang
Base class for threadsafe cache of data read from a file on disk.
Definition: FileCache.h:40
PathRef path() const
Definition: FileCache.h:63
void 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) const
Definition: FileCache.cpp:31
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
Definition: ThreadsafeFS.h:26
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//