Blender V5.3
memory_cache_file_load.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <optional>
10
11#include "BLI_fileops.hh"
12#include "BLI_hash.hh"
13#include "BLI_map.hh"
15#include "BLI_mutex.hh"
16#include "BLI_task.hh"
17#include "BLI_vector.hh"
18#include "BLI_vector_set.hh"
19
20namespace blender::memory_cache {
21
25class LoadFileKey : public GenericKey {
26 private:
28 Vector<std::string> file_paths_;
33 std::shared_ptr<const GenericKey> loader_key_;
34
35 public:
36 LoadFileKey(Vector<std::string> file_paths, std::shared_ptr<const GenericKey> loader_key)
37 : file_paths_(std::move(file_paths)), loader_key_(std::move(loader_key))
38 {
39 }
40
42 {
43 return file_paths_;
44 }
45
46 uint64_t hash() const override
47 {
48 return get_default_hash(file_paths_, *loader_key_);
49 }
50
51 friend bool operator==(const LoadFileKey &a, const LoadFileKey &b)
52 {
53 return a.file_paths_ == b.file_paths_ && *a.loader_key_ == *b.loader_key_;
54 }
55
56 bool equal_to(const GenericKey &other) const override
57 {
58 if (const auto *other_typed = dynamic_cast<const LoadFileKey *>(&other)) {
59 return *this == *other_typed;
60 }
61 return false;
62 }
63
64 std::unique_ptr<GenericKey> to_storable() const override
65 {
66 /* Currently #LoadFileKey is always storable, i.e. it owns all the data it references. A
67 * potential future optimization could be to support just referencing the paths and loader key,
68 * but that causes some boilerplate now that is not worth it. */
69 return std::make_unique<LoadFileKey>(*this);
70 }
71};
72
73static std::optional<int64_t> get_file_modification_time(const StringRefNull path)
74{
75 BLI_stat_t stat;
76 if (BLI_stat(path.c_str(), &stat) == -1) {
77 return std::nullopt;
78 }
79 return stat.st_mtime;
80}
81
86
88{
89 static FileStatMap file_stat_map;
90 return file_stat_map;
91}
92
94{
95 FileStatMap &file_stat_map = get_file_stat_map();
96
97 /* Retrieve the file modification times before the lock because there is no need for the lock
98 * yet. While not guaranteed, retrieving the modification time is often optimized by the OS so
99 * that no actual access to the hard drive is necessary. */
100 Array<std::optional<int64_t>> new_times(file_paths.size());
101 for (const int i : file_paths.index_range()) {
102 new_times[i] = get_file_modification_time(file_paths[i]);
103 }
104
105 std::lock_guard lock{file_stat_map.mutex};
106
107 /* Find all paths that have changed on disk. */
108 VectorSet<StringRefNull> outdated_paths;
109 for (const int i : file_paths.index_range()) {
110 const StringRefNull path = file_paths[i];
111 const std::optional<int64_t> new_time = new_times[i];
112 std::optional<int64_t> &old_time = file_stat_map.map.lookup_or_add_as(path, new_time);
113 if (old_time != new_time) {
114 outdated_paths.add(path);
115 old_time = new_time;
116 }
117 }
118 /* If any referenced file was changed, invalidate the caches that use it. */
119 if (!outdated_paths.is_empty()) {
120 /* Isolate because a mutex is locked. */
122 /* Invalidation is done while the mutex is locked so that other threads won't see the old
123 * cached value anymore after we've detected that it's outdated. */
124 memory_cache::remove_if([&](const GenericKey &other_key) {
125 if (const auto *other_key_typed = dynamic_cast<const LoadFileKey *>(&other_key)) {
126 const Span<std::string> other_key_paths = other_key_typed->file_paths();
127 return std::any_of(
128 other_key_paths.begin(), other_key_paths.end(), [&](const StringRefNull path) {
129 return outdated_paths.contains(path);
130 });
131 }
132 return false;
133 });
134 });
135 }
136}
137
138std::shared_ptr<CachedValue> get_loaded_base(const GenericKey &loader_key,
139 Span<StringRefNull> file_paths,
140 FunctionRef<std::unique_ptr<CachedValue>()> load_fn)
141{
143 const LoadFileKey key{file_paths, loader_key.to_storable()};
144 return memory_cache::get_base(key, load_fn);
145}
146
147} // namespace blender::memory_cache
File and directory operations.
volatile int lock
virtual std::unique_ptr< GenericKey > to_storable() const =0
Value & lookup_or_add_as(ForwardKey &&key, ForwardValue &&...value)
Definition BLI_map.hh:605
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr const T * begin() const
Definition BLI_span.hh:224
constexpr const char * c_str() const
bool add(const Key &key)
bool equal_to(const GenericKey &other) const override
friend bool operator==(const LoadFileKey &a, const LoadFileKey &b)
LoadFileKey(Vector< std::string > file_paths, std::shared_ptr< const GenericKey > loader_key)
std::unique_ptr< GenericKey > to_storable() const override
std::shared_ptr< CachedValue > get_base(const GenericKey &key, FunctionRef< std::unique_ptr< CachedValue >()> compute_fn)
void remove_if(FunctionRef< bool(const GenericKey &)> predicate)
static FileStatMap & get_file_stat_map()
std::shared_ptr< CachedValue > get_loaded_base(const GenericKey &loader_key, Span< StringRefNull > file_paths, FunctionRef< std::unique_ptr< CachedValue >()> load_fn)
static std::optional< int64_t > get_file_modification_time(const StringRefNull path)
static void invalidate_outdated_caches_if_necessary(const Span< StringRefNull > file_paths)
auto isolate_task(const Function &function)
Definition BLI_task.hh:224
unsigned __int64 uint64_t
Definition BKE_text.h:91
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
std::mutex Mutex
Definition BLI_mutex.hh:51
struct stat BLI_stat_t
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
Map< std::string, std::optional< int64_t > > map