leafNodeCache.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_EF_LEAF_NODE_CACHE_H
8 #define PXR_EXEC_EF_LEAF_NODE_CACHE_H
9 
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/ef/api.h"
17 
18 #include "pxr/base/tf/hashmap.h"
21 #include "pxr/exec/vdf/types.h"
22 
23 #include <atomic>
24 #include <vector>
25 
26 PXR_NAMESPACE_OPEN_SCOPE
27 
38 {
39 public:
42  EF_API
44 
52  size_t GetVersion() const {
53  return _version.load(std::memory_order_relaxed);
54  }
55 
58  EF_API
60  const VdfMaskedOutputVector &outputs,
61  bool updateIncrementally);
62 
65  EF_API
66  const std::vector<const VdfNode *> &FindNodes(
67  const VdfMaskedOutputVector &outputs,
68  bool updateIncrementally);
69 
75  EF_API
76  const std::vector<const VdfNode *> &FindNodes(
77  const VdfMaskedOutputVector &outputs,
78  const TfBits &outputsMask);
79 
82  EF_API
83  void Clear();
84 
90  EF_API
91  void WillDeleteConnection(const VdfConnection &connection);
92 
98  EF_API
99  void DidConnect(const VdfConnection &connection);
100 
101 private:
102  // Holds the sets of leaf nodes, one for each output in the request, along
103  // with cached, combined sets of leaf nodes given a mask of the requested
104  // outputs. The sets contain indices into the leaf node indexer.
105  struct _VectorizedCacheEntry {
106  std::vector<TfBits> leafNodes;
107  TfHashMap<TfBits, TfBits, TfBits::FastHash> combinedLeafNodes;
108  };
109 
110  // Stores an array of leaf nodes, and outputs connected to these leaf nodes.
111  struct _SparseCacheEntry {
112  std::vector<const VdfNode *> nodes;
113  VdfOutputToMaskMap outputs;
114  };
115 
116  // Clears the vectorized and sparse caches along with the traverser used to
117  // populate those caches, if their internal state has been flagged as being
118  // invalid.
119  void _ClearCachesIfInvalid();
120 
121  // Combine separate sets of leaf nodes into a single set.
122  TfBits _CombineLeafNodes(
123  const TfBits &outputsMask,
124  const std::vector<TfBits> &leafNodes) const;
125 
126  // Populates the vectorized cache by doing a vectorized traversal.
127  _VectorizedCacheEntry *_PopulateVectorizedEntry(
128  const VdfMaskedOutputVector &outputs);
129 
130  // Populates the sparse cache by using the results from a previous,
131  // vectorized traversal.
132  _SparseCacheEntry *_PopulateSparseEntry(
133  const VdfMaskedOutputVector &outputs,
134  const TfBits &leafNodes);
135 
136  // The version of the cache. Incremented with every edit.
137  std::atomic<size_t> _version;
138 
139  // Indicates that the internal state pertaining to vectorized and sparse
140  // caches is invalid and that those caches be cleared.
141  std::atomic<bool> _cachesAreInvalid;
142 
143  // The dependency cache used for fast lookups of input-to-output
144  // dependencies.
145  EfDependencyCache _dependencyCache;
146 
147  // The leaf node indexer associates each leaf node with a unique index.
148  Ef_LeafNodeIndexer _indexer;
149 
150  // A cache of requested outputs to leaf node dependencies for each
151  // individual output in the request.
152  using _VectorizedCache = TfHashMap<
154  _VectorizedCacheEntry,
156  _VectorizedCache _vectorizedCache;
157 
158  // A cache of requested outputs to leaf nodes and leaf node connected
159  // outputs.
160  using _SparseCache = TfHashMap<
162  _SparseCacheEntry,
164  _SparseCache _sparseCache;
165 
166  // The traverser used to populate the vectorized cache.
168 };
169 
170 PXR_NAMESPACE_CLOSE_SCOPE
171 
172 #endif
A class that fully represents a connection between two VdfNodes.
Definition: connection.h:29
size_t GetVersion() const
Returns the current edit version of the leaf node cache.
Definition: leafNodeCache.h:52
This cache is a thin wrapper around the EfDependencyCache.
Definition: leafNodeCache.h:37
std::vector< VdfMaskedOutput > VdfMaskedOutputVector
A vector of VdfMaskedOutputs.
EF_API void WillDeleteConnection(const VdfConnection &connection)
Call this to notify the cache of connections that have been deleted.
Fast bit array that keeps track of the number of bits set and can find the next set in a timely manne...
Definition: bits.h:48
Traverses a VdfNetwork in the input-to-output direction, while treating each output in the traversal ...
EF_API void Clear()
Clear the entire cache.
std::unordered_map< const VdfOutput *, VdfMask, TfHash > VdfOutputToMaskMap
A map from output pointer to mask.
Definition: types.h:104
The leaf node indexer tracks leaf nodes added and removed from the network, and associates each leaf ...
EF_API const VdfOutputToMaskMap & FindOutputs(const VdfMaskedOutputVector &outputs, bool updateIncrementally)
Find outputs dependent on the given outputs.
Hashing functor for VdfMaskedOutputVectors.
EF_API void DidConnect(const VdfConnection &connection)
Call this to notify the cache of newly added connections.
Caches output traversals by associating an input request with a set of stored output dependencies,...
EF_API EfLeafNodeCache()
Constructor.
EF_API const std::vector< const VdfNode * > & FindNodes(const VdfMaskedOutputVector &outputs, bool updateIncrementally)
Find leaf nodes dependent on the given outputs.