Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <txgraph.h>
6 : :
7 : : #include <cluster_linearize.h>
8 : : #include <random.h>
9 : : #include <util/bitset.h>
10 : : #include <util/check.h>
11 : : #include <util/feefrac.h>
12 : : #include <util/vector.h>
13 : :
14 : : #include <compare>
15 : : #include <functional>
16 : : #include <memory>
17 : : #include <set>
18 : : #include <span>
19 : : #include <unordered_set>
20 : : #include <utility>
21 : :
22 : : namespace {
23 : :
24 : : using namespace cluster_linearize;
25 : :
26 : : /** The maximum number of levels a TxGraph can have (0 = main, 1 = staging). */
27 : : static constexpr int MAX_LEVELS{2};
28 : :
29 : : // Forward declare the TxGraph implementation class.
30 : : class TxGraphImpl;
31 : :
32 : : /** Position of a DepGraphIndex within a Cluster::m_linearization. */
33 : : using LinearizationIndex = uint32_t;
34 : : /** Position of a Cluster within TxGraphImpl::ClusterSet::m_clusters. */
35 : : using ClusterSetIndex = uint32_t;
36 : :
37 : : /** Quality levels for cached cluster linearizations. */
38 : : enum class QualityLevel
39 : : {
40 : : /** This is a singleton cluster consisting of a transaction that individually exceeds the
41 : : * cluster size limit. It cannot be merged with anything. */
42 : : OVERSIZED_SINGLETON,
43 : : /** This cluster may have multiple disconnected components, which are all NEEDS_FIX. */
44 : : NEEDS_SPLIT_FIX,
45 : : /** This cluster may have multiple disconnected components, which are all NEEDS_RELINEARIZE. */
46 : : NEEDS_SPLIT,
47 : : /** This cluster may be non-topological. */
48 : : NEEDS_FIX,
49 : : /** This cluster has undergone changes that warrant re-linearization. */
50 : : NEEDS_RELINEARIZE,
51 : : /** The minimal level of linearization has been performed, but it is not known to be optimal. */
52 : : ACCEPTABLE,
53 : : /** The linearization is known to be optimal. */
54 : : OPTIMAL,
55 : : /** This cluster is not registered in any ClusterSet::m_clusters.
56 : : * This must be the last entry in QualityLevel as ClusterSet::m_clusters is sized using it. */
57 : : NONE,
58 : : };
59 : :
60 : : /** Information about a transaction inside TxGraphImpl::Trim. */
61 : 64217 : struct TrimTxData
62 : : {
63 : : // Fields populated by Cluster::AppendTrimData(). These are immutable after TrimTxData
64 : : // construction.
65 : : /** Chunk feerate for this transaction. */
66 : : FeePerWeight m_chunk_feerate;
67 : : /** GraphIndex of the transaction. */
68 : : TxGraph::GraphIndex m_index;
69 : : /** Size of the transaction. */
70 : : uint32_t m_tx_size;
71 : :
72 : : // Fields only used internally by TxGraphImpl::Trim():
73 : : /** Number of unmet dependencies this transaction has. -1 if the transaction is included. */
74 : : uint32_t m_deps_left;
75 : : /** Number of dependencies that apply to this transaction as child. */
76 : : uint32_t m_parent_count;
77 : : /** Where in deps_by_child those dependencies begin. */
78 : : uint32_t m_parent_offset;
79 : : /** Number of dependencies that apply to this transaction as parent. */
80 : : uint32_t m_children_count;
81 : : /** Where in deps_by_parent those dependencies begin. */
82 : : uint32_t m_children_offset;
83 : :
84 : : // Fields only used internally by TxGraphImpl::Trim()'s union-find implementation, and only for
85 : : // transactions that are definitely included or definitely rejected.
86 : : //
87 : : // As transactions get processed, they get organized into trees which form partitions
88 : : // representing the would-be clusters up to that point. The root of each tree is a
89 : : // representative for that partition. See
90 : : // https://en.wikipedia.org/wiki/Disjoint-set_data_structure.
91 : : //
92 : : /** Pointer to another TrimTxData, towards the root of the tree. If this is a root, m_uf_parent
93 : : * is equal to this itself. */
94 : : TrimTxData* m_uf_parent;
95 : : /** If this is a root, the total number of transactions in the partition. */
96 : : uint32_t m_uf_count;
97 : : /** If this is a root, the total size of transactions in the partition. */
98 : : uint64_t m_uf_size;
99 : : };
100 : :
101 : : /** A grouping of connected transactions inside a TxGraphImpl::ClusterSet. */
102 : : class Cluster
103 : : {
104 : : friend class TxGraphImpl;
105 : : friend class BlockBuilderImpl;
106 : :
107 : : protected:
108 : : using GraphIndex = TxGraph::GraphIndex;
109 : : using SetType = BitSet<MAX_CLUSTER_COUNT_LIMIT>;
110 : : /** The quality level of m_linearization. */
111 : : QualityLevel m_quality{QualityLevel::NONE};
112 : : /** Which position this Cluster has in TxGraphImpl::ClusterSet::m_clusters[m_quality]. */
113 : : ClusterSetIndex m_setindex{ClusterSetIndex(-1)};
114 : : /** Sequence number for this Cluster (for tie-breaking comparison between equal-chunk-feerate
115 : : transactions in distinct clusters). */
116 : : uint64_t m_sequence;
117 : :
118 : 128969 : explicit Cluster(uint64_t sequence) noexcept : m_sequence(sequence) {}
119 : :
120 : : public:
121 : : // Provide virtual destructor, for safe polymorphic usage inside std::unique_ptr.
122 : 0 : virtual ~Cluster() = default;
123 : :
124 : : // Cannot move or copy (would invalidate Cluster* in Locator and ClusterSet). */
125 : : Cluster(const Cluster&) = delete;
126 : : Cluster& operator=(const Cluster&) = delete;
127 : : Cluster(Cluster&&) = delete;
128 : : Cluster& operator=(Cluster&&) = delete;
129 : :
130 : : // Generic helper functions.
131 : :
132 : : /** Whether the linearization of this Cluster can be exposed. */
133 : 182231795 : bool IsAcceptable() const noexcept
134 : : {
135 : 182231795 : return m_quality == QualityLevel::ACCEPTABLE || m_quality == QualityLevel::OPTIMAL;
136 : : }
137 : : /** Whether the linearization of this Cluster is topological. */
138 : 234316 : bool IsTopological() const noexcept
139 : : {
140 : 234316 : return m_quality != QualityLevel::NEEDS_FIX && m_quality != QualityLevel::NEEDS_SPLIT_FIX;
141 : : }
142 : : /** Whether the linearization of this Cluster is optimal. */
143 : 7122444 : bool IsOptimal() const noexcept
144 : : {
145 : 7122444 : return m_quality == QualityLevel::OPTIMAL;
146 : : }
147 : : /** Whether this cluster is oversized. Note that no changes that can cause oversizedness are
148 : : * ever applied, so the only way a materialized Cluster object can be oversized is by being
149 : : * an individually oversized transaction singleton. */
150 : 14341030 : bool IsOversized() const noexcept { return m_quality == QualityLevel::OVERSIZED_SINGLETON; }
151 : : /** Whether this cluster requires splitting. */
152 : 181986688 : bool NeedsSplitting() const noexcept
153 : : {
154 : 181986688 : return m_quality == QualityLevel::NEEDS_SPLIT || m_quality == QualityLevel::NEEDS_SPLIT_FIX;
155 : : }
156 : :
157 : : /** Get the smallest number of transactions this Cluster is intended for. */
158 : : virtual DepGraphIndex GetMinIntendedTxCount() const noexcept = 0;
159 : : /** Get the maximum number of transactions this Cluster supports. */
160 : : virtual DepGraphIndex GetMaxTxCount() const noexcept = 0;
161 : : /** Total memory usage currently for this Cluster, including all its dynamic memory, plus Cluster
162 : : * structure itself, and ClusterSet::m_clusters entry. */
163 : : virtual size_t TotalMemoryUsage() const noexcept = 0;
164 : : /** Determine the range of DepGraphIndexes used by this Cluster. */
165 : : virtual DepGraphIndex GetDepGraphIndexRange() const noexcept = 0;
166 : : /** Get the number of transactions in this Cluster. */
167 : : virtual LinearizationIndex GetTxCount() const noexcept = 0;
168 : : /** Get the total size of the transactions in this Cluster. */
169 : : virtual uint64_t GetTotalTxSize() const noexcept = 0;
170 : : /** Given a DepGraphIndex into this Cluster, find the corresponding GraphIndex. */
171 : : virtual GraphIndex GetClusterEntry(DepGraphIndex index) const noexcept = 0;
172 : : /** Append a transaction with given GraphIndex at the end of this Cluster and its
173 : : * linearization. Return the DepGraphIndex it was placed at. */
174 : : virtual DepGraphIndex AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept = 0;
175 : : /** Add dependencies to a given child in this cluster. */
176 : : virtual void AddDependencies(SetType parents, DepGraphIndex child) noexcept = 0;
177 : : /** Invoke visit1_fn for each transaction in the cluster, in linearization order, then
178 : : * visit2_fn in the same order, then wipe this Cluster. */
179 : : virtual void ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept = 0;
180 : : /** Figure out what level this Cluster exists at in the graph. In most cases this is known by
181 : : * the caller already (see all "int level" arguments below), but not always. */
182 : : virtual int GetLevel(const TxGraphImpl& graph) const noexcept = 0;
183 : : /** Only called by TxGraphImpl::SwapIndexes. */
184 : : virtual void UpdateMapping(DepGraphIndex cluster_idx, GraphIndex graph_idx) noexcept = 0;
185 : : /** Push changes to Cluster and its linearization to the TxGraphImpl Entry objects. */
186 : : virtual void Updated(TxGraphImpl& graph, int level) noexcept = 0;
187 : : /** Create a copy of this Cluster in staging, returning a pointer to it (used by PullIn). */
188 : : virtual Cluster* CopyToStaging(TxGraphImpl& graph) const noexcept = 0;
189 : : /** Get the list of Clusters in main that conflict with this one (which is assumed to be in staging). */
190 : : virtual void GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept = 0;
191 : : /** Mark all the Entry objects belonging to this staging Cluster as missing. The Cluster must be
192 : : * deleted immediately after. */
193 : : virtual void MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept = 0;
194 : : /** Remove all transactions from a (non-empty) Cluster. */
195 : : virtual void Clear(TxGraphImpl& graph, int level) noexcept = 0;
196 : : /** Change a Cluster's level from 1 (staging) to 0 (main). */
197 : : virtual void MoveToMain(TxGraphImpl& graph) noexcept = 0;
198 : : /** Minimize this Cluster's memory usage. */
199 : : virtual void Compact() noexcept = 0;
200 : :
201 : : // Functions that implement the Cluster-specific side of internal TxGraphImpl mutations.
202 : :
203 : : /** Apply all removals from the front of to_remove that apply to this Cluster, popping them
204 : : * off. There must be at least one such entry. */
205 : : virtual void ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept = 0;
206 : : /** Split this cluster (must have a NEEDS_SPLIT* quality). Returns whether to delete this
207 : : * Cluster afterwards. */
208 : : [[nodiscard]] virtual bool Split(TxGraphImpl& graph, int level) noexcept = 0;
209 : : /** Move all transactions from cluster to *this (as separate components). */
210 : : virtual void Merge(TxGraphImpl& graph, int level, Cluster& cluster) noexcept = 0;
211 : : /** Given a span of (parent, child) pairs that all belong to this Cluster, apply them. */
212 : : virtual void ApplyDependencies(TxGraphImpl& graph, int level, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept = 0;
213 : : /** Improve the linearization of this Cluster. Returns how much work was performed and whether
214 : : * the Cluster's QualityLevel improved as a result. */
215 : : virtual std::pair<uint64_t, bool> Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept = 0;
216 : : /** For every chunk in the cluster, append its FeeFrac to ret. */
217 : : virtual void AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept = 0;
218 : : /** Add a TrimTxData entry (filling m_chunk_feerate, m_index, m_tx_size) for every
219 : : * transaction in the Cluster to ret. Implicit dependencies between consecutive transactions
220 : : * in the linearization are added to deps. Return the Cluster's total transaction size. */
221 : : virtual uint64_t AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept = 0;
222 : :
223 : : // Functions that implement the Cluster-specific side of public TxGraph functions.
224 : :
225 : : /** Process elements from the front of args that apply to this cluster, and append Refs for the
226 : : * union of their ancestors to output. */
227 : : virtual void GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept = 0;
228 : : /** Process elements from the front of args that apply to this cluster, and append Refs for the
229 : : * union of their descendants to output. */
230 : : virtual void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept = 0;
231 : : /** Populate range with refs for the transactions in this Cluster's linearization, from
232 : : * position start_pos until start_pos+range.size()-1, inclusive. Returns whether that
233 : : * range includes the last transaction in the linearization. */
234 : : virtual bool GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept = 0;
235 : : /** Get the individual transaction feerate of a Cluster element. */
236 : : virtual FeePerWeight GetIndividualFeerate(DepGraphIndex idx) noexcept = 0;
237 : : /** Modify the fee of a Cluster element. */
238 : : virtual void SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept = 0;
239 : :
240 : : // Debugging functions.
241 : :
242 : : virtual void SanityCheck(const TxGraphImpl& graph, int level) const = 0;
243 : : };
244 : :
245 : : /** An implementation of Cluster that uses a DepGraph and vectors, to support arbitrary numbers of
246 : : * transactions up to MAX_CLUSTER_COUNT_LIMIT. */
247 : : class GenericClusterImpl final : public Cluster
248 : : {
249 : : friend class TxGraphImpl;
250 : : /** The DepGraph for this cluster, holding all feerates, and ancestors/descendants. */
251 : : DepGraph<SetType> m_depgraph;
252 : : /** m_mapping[i] gives the GraphIndex for the position i transaction in m_depgraph. Values for
253 : : * positions i that do not exist in m_depgraph shouldn't ever be accessed and thus don't
254 : : * matter. m_mapping.size() equals m_depgraph.PositionRange(). */
255 : : std::vector<GraphIndex> m_mapping;
256 : : /** The current linearization of the cluster. m_linearization.size() equals
257 : : * m_depgraph.TxCount(). This is always kept topological. */
258 : : std::vector<DepGraphIndex> m_linearization;
259 : :
260 : : public:
261 : : /** The smallest number of transactions this Cluster implementation is intended for. */
262 : : static constexpr DepGraphIndex MIN_INTENDED_TX_COUNT{2};
263 : : /** The largest number of transactions this Cluster implementation supports. */
264 : : static constexpr DepGraphIndex MAX_TX_COUNT{SetType::Size()};
265 : :
266 : : GenericClusterImpl() noexcept = delete;
267 : : /** Construct an empty GenericClusterImpl. */
268 : : explicit GenericClusterImpl(uint64_t sequence) noexcept;
269 : :
270 : : size_t TotalMemoryUsage() const noexcept final;
271 : 53618 : constexpr DepGraphIndex GetMinIntendedTxCount() const noexcept final { return MIN_INTENDED_TX_COUNT; }
272 : 57451 : constexpr DepGraphIndex GetMaxTxCount() const noexcept final { return MAX_TX_COUNT; }
273 [ - + ]: 9 : DepGraphIndex GetDepGraphIndexRange() const noexcept final { return m_depgraph.PositionRange(); }
274 [ - + ]: 300495 : LinearizationIndex GetTxCount() const noexcept final { return m_linearization.size(); }
275 : : uint64_t GetTotalTxSize() const noexcept final;
276 : 225857 : GraphIndex GetClusterEntry(DepGraphIndex index) const noexcept final { return m_mapping[index]; }
277 : : DepGraphIndex AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept final;
278 : : void AddDependencies(SetType parents, DepGraphIndex child) noexcept final;
279 : : void ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept final;
280 : : int GetLevel(const TxGraphImpl& graph) const noexcept final;
281 : 639 : void UpdateMapping(DepGraphIndex cluster_idx, GraphIndex graph_idx) noexcept final { m_mapping[cluster_idx] = graph_idx; }
282 : : void Updated(TxGraphImpl& graph, int level) noexcept final;
283 : : Cluster* CopyToStaging(TxGraphImpl& graph) const noexcept final;
284 : : void GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept final;
285 : : void MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept final;
286 : : void Clear(TxGraphImpl& graph, int level) noexcept final;
287 : : void MoveToMain(TxGraphImpl& graph) noexcept final;
288 : : void Compact() noexcept final;
289 : : void ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept final;
290 : : [[nodiscard]] bool Split(TxGraphImpl& graph, int level) noexcept final;
291 : : void Merge(TxGraphImpl& graph, int level, Cluster& cluster) noexcept final;
292 : : void ApplyDependencies(TxGraphImpl& graph, int level, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept final;
293 : : std::pair<uint64_t, bool> Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept final;
294 : : void AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept final;
295 : : uint64_t AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept final;
296 : : void GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept final;
297 : : void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept final;
298 : : bool GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept final;
299 : : FeePerWeight GetIndividualFeerate(DepGraphIndex idx) noexcept final;
300 : : void SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept final;
301 : : void SanityCheck(const TxGraphImpl& graph, int level) const final;
302 : : };
303 : :
304 : : /** An implementation of Cluster that only supports 1 transaction. */
305 : 0 : class SingletonClusterImpl final : public Cluster
306 : : {
307 : : friend class TxGraphImpl;
308 : :
309 : : /** The feerate of the (singular) transaction in this Cluster. */
310 : : FeePerWeight m_feerate;
311 : : /** Constant to indicate that this Cluster is empty. */
312 : : static constexpr auto NO_GRAPH_INDEX = GraphIndex(-1);
313 : : /** The GraphIndex of the transaction. NO_GRAPH_INDEX if this Cluster is empty. */
314 : : GraphIndex m_graph_index = NO_GRAPH_INDEX;
315 : :
316 : : public:
317 : : /** The smallest number of transactions this Cluster implementation is intended for. */
318 : : static constexpr DepGraphIndex MIN_INTENDED_TX_COUNT{1};
319 : : /** The largest number of transactions this Cluster implementation supports. */
320 : : static constexpr DepGraphIndex MAX_TX_COUNT{1};
321 : :
322 : : SingletonClusterImpl() noexcept = delete;
323 : : /** Construct an empty SingletonClusterImpl. */
324 : 127028 : explicit SingletonClusterImpl(uint64_t sequence) noexcept : Cluster(sequence) {}
325 : :
326 : : size_t TotalMemoryUsage() const noexcept final;
327 : 7116886 : constexpr DepGraphIndex GetMinIntendedTxCount() const noexcept final { return MIN_INTENDED_TX_COUNT; }
328 : 7118501 : constexpr DepGraphIndex GetMaxTxCount() const noexcept final { return MAX_TX_COUNT; }
329 : 57731283 : LinearizationIndex GetTxCount() const noexcept final { return m_graph_index != NO_GRAPH_INDEX; }
330 : 7565 : DepGraphIndex GetDepGraphIndexRange() const noexcept final { return GetTxCount(); }
331 [ + + ]: 7256088 : uint64_t GetTotalTxSize() const noexcept final { return GetTxCount() ? m_feerate.size : 0; }
332 : 7116892 : GraphIndex GetClusterEntry(DepGraphIndex index) const noexcept final { Assume(index == 0); Assume(GetTxCount()); return m_graph_index; }
333 : : DepGraphIndex AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept final;
334 : : void AddDependencies(SetType parents, DepGraphIndex child) noexcept final;
335 : : void ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept final;
336 : : int GetLevel(const TxGraphImpl& graph) const noexcept final;
337 : 15633 : void UpdateMapping(DepGraphIndex cluster_idx, GraphIndex graph_idx) noexcept final { Assume(cluster_idx == 0); m_graph_index = graph_idx; }
338 : : void Updated(TxGraphImpl& graph, int level) noexcept final;
339 : : Cluster* CopyToStaging(TxGraphImpl& graph) const noexcept final;
340 : : void GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept final;
341 : : void MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept final;
342 : : void Clear(TxGraphImpl& graph, int level) noexcept final;
343 : : void MoveToMain(TxGraphImpl& graph) noexcept final;
344 : : void Compact() noexcept final;
345 : : void ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept final;
346 : : [[nodiscard]] bool Split(TxGraphImpl& graph, int level) noexcept final;
347 : : void Merge(TxGraphImpl& graph, int level, Cluster& cluster) noexcept final;
348 : : void ApplyDependencies(TxGraphImpl& graph, int level, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept final;
349 : : std::pair<uint64_t, bool> Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept final;
350 : : void AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept final;
351 : : uint64_t AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept final;
352 : : void GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept final;
353 : : void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept final;
354 : : bool GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept final;
355 : : FeePerWeight GetIndividualFeerate(DepGraphIndex idx) noexcept final;
356 : : void SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept final;
357 : : void SanityCheck(const TxGraphImpl& graph, int level) const final;
358 : : };
359 : :
360 : : /** The transaction graph, including staged changes.
361 : : *
362 : : * The overall design of the data structure consists of 3 interlinked representations:
363 : : * - The transactions (held as a vector of TxGraphImpl::Entry inside TxGraphImpl).
364 : : * - The clusters (Cluster objects in per-quality vectors inside TxGraphImpl::ClusterSet).
365 : : * - The Refs (TxGraph::Ref objects, held externally by users of the TxGraph class)
366 : : *
367 : : * The Clusters are kept in one or two ClusterSet objects, one for the "main" graph, and one for
368 : : * the proposed changes ("staging"). If a transaction occurs in both, they share the same Entry,
369 : : * but there will be a separate Cluster per graph.
370 : : *
371 : : * Clusters and Refs contain the index of the Entry objects they refer to, and the Entry objects
372 : : * refer back to the Clusters and Refs the corresponding transaction is contained in.
373 : : *
374 : : * While redundant, this permits moving all of them independently, without invalidating things
375 : : * or costly iteration to fix up everything:
376 : : * - Entry objects can be moved to fill holes left by removed transactions in the Entry vector
377 : : * (see TxGraphImpl::Compact).
378 : : * - Clusters can be rewritten continuously (removals can cause them to split, new dependencies
379 : : * can cause them to be merged).
380 : : * - Ref objects can be held outside the class, while permitting them to be moved around, and
381 : : * inherited from.
382 : : */
383 : : class TxGraphImpl final : public TxGraph
384 : : {
385 : : friend class Cluster;
386 : : friend class SingletonClusterImpl;
387 : : friend class GenericClusterImpl;
388 : : friend class BlockBuilderImpl;
389 : : private:
390 : : /** Internal RNG. */
391 : : FastRandomContext m_rng;
392 : : /** This TxGraphImpl's maximum cluster count limit. */
393 : : const DepGraphIndex m_max_cluster_count;
394 : : /** This TxGraphImpl's maximum cluster size limit. */
395 : : const uint64_t m_max_cluster_size;
396 : : /** The number of linearization improvement steps needed per cluster to be considered
397 : : * acceptable. */
398 : : const uint64_t m_acceptable_iters;
399 : :
400 : : /** Information about one group of Clusters to be merged. */
401 : : struct GroupEntry
402 : : {
403 : : /** Where the clusters to be merged start in m_group_clusters. */
404 : : uint32_t m_cluster_offset;
405 : : /** How many clusters to merge. */
406 : : uint32_t m_cluster_count;
407 : : /** Where the dependencies for this cluster group in m_deps_to_add start. */
408 : : uint32_t m_deps_offset;
409 : : /** How many dependencies to add. */
410 : : uint32_t m_deps_count;
411 : : };
412 : :
413 : : /** Information about all groups of Clusters to be merged. */
414 : 88406 : struct GroupData
415 : : {
416 : : /** The groups of Clusters to be merged. */
417 : : std::vector<GroupEntry> m_groups;
418 : : /** Which clusters are to be merged. GroupEntry::m_cluster_offset indexes into this. */
419 : : std::vector<Cluster*> m_group_clusters;
420 : : };
421 : :
422 : : /** The collection of all Clusters in main or staged. */
423 : : struct ClusterSet
424 : : {
425 : : /** The vectors of clusters, one vector per quality level. ClusterSetIndex indexes into each. */
426 : : std::array<std::vector<std::unique_ptr<Cluster>>, int(QualityLevel::NONE)> m_clusters;
427 : : /** Which removals have yet to be applied. */
428 : : std::vector<GraphIndex> m_to_remove;
429 : : /** Which dependencies are to be added ((parent,child) pairs). GroupData::m_deps_offset indexes
430 : : * into this. */
431 : : std::vector<std::pair<GraphIndex, GraphIndex>> m_deps_to_add;
432 : : /** Information about the merges to be performed, if known. */
433 : : std::optional<GroupData> m_group_data = GroupData{};
434 : : /** Which entries were removed in this ClusterSet (so they can be wiped on abort). This
435 : : * includes all entries which have an (R) removed locator at this level (staging only),
436 : : * plus optionally any transaction in m_unlinked. */
437 : : std::vector<GraphIndex> m_removed;
438 : : /** Total number of transactions in this graph (sum of all transaction counts in all
439 : : * Clusters, and for staging also those inherited from the main ClusterSet). */
440 : : GraphIndex m_txcount{0};
441 : : /** Total number of individually oversized transactions in the graph. */
442 : : GraphIndex m_txcount_oversized{0};
443 : : /** Whether this graph is oversized (if known). */
444 : : std::optional<bool> m_oversized{false};
445 : : /** The combined TotalMemoryUsage of all clusters in this level (only Clusters that
446 : : * are materialized; in staging, implicit Clusters from main are not counted), */
447 : : size_t m_cluster_usage{0};
448 : :
449 : 61589 : ClusterSet() noexcept = default;
450 : : };
451 : :
452 : : /** The main ClusterSet. */
453 : : ClusterSet m_main_clusterset;
454 : : /** The staging ClusterSet, if any. */
455 : : std::optional<ClusterSet> m_staging_clusterset;
456 : : /** Next sequence number to assign to created Clusters. */
457 : : uint64_t m_next_sequence_counter{0};
458 : :
459 : : /** Information about a chunk in the main graph. */
460 : : struct ChunkData
461 : : {
462 : : /** The Entry which is the last transaction of the chunk. */
463 : : mutable GraphIndex m_graph_index;
464 : : /** How many transactions the chunk contains (-1 = singleton tail of cluster). */
465 : : LinearizationIndex m_chunk_count;
466 : :
467 : 174893 : ChunkData(GraphIndex graph_index, LinearizationIndex chunk_count) noexcept :
468 : 174893 : m_graph_index{graph_index}, m_chunk_count{chunk_count} {}
469 : : };
470 : :
471 : : /** Compare two Cluster* by their m_sequence value (while supporting nullptr). */
472 : 46376637 : static std::strong_ordering CompareClusters(Cluster* a, Cluster* b) noexcept
473 : : {
474 : : // The nullptr pointer compares before everything else.
475 [ - + ]: 46376637 : if (a == nullptr || b == nullptr) {
476 [ # # # # ]: 0 : return (a != nullptr) <=> (b != nullptr);
477 : : }
478 : : // If neither pointer is nullptr, compare the Clusters' sequence numbers.
479 [ + + ]: 46376637 : Assume(a == b || a->m_sequence != b->m_sequence);
480 [ + + + + ]: 46376637 : return a->m_sequence <=> b->m_sequence;
481 : : }
482 : :
483 : : /** Compare two entries (which must both exist within the main graph). */
484 : 89465931 : std::strong_ordering CompareMainTransactions(GraphIndex a, GraphIndex b) const noexcept
485 : : {
486 [ - + + - : 89465931 : Assume(a < m_entries.size() && b < m_entries.size());
+ + ]
487 [ + + ]: 89465931 : const auto& entry_a = m_entries[a];
488 : 89465931 : const auto& entry_b = m_entries[b];
489 : : // Compare chunk feerates, and return result if it differs.
490 : 89465931 : auto feerate_cmp = FeeRateCompare(entry_b.m_main_chunk_feerate, entry_a.m_main_chunk_feerate);
491 [ + + ]: 89465931 : if (feerate_cmp < 0) return std::strong_ordering::less;
492 [ + + ]: 62253803 : if (feerate_cmp > 0) return std::strong_ordering::greater;
493 : : // Compare Cluster m_sequence as tie-break for equal chunk feerates.
494 : 47636761 : const auto& locator_a = entry_a.m_locator[0];
495 : 47636761 : const auto& locator_b = entry_b.m_locator[0];
496 [ + + ]: 47636761 : Assume(locator_a.IsPresent() && locator_b.IsPresent());
497 [ + + ]: 47636761 : if (locator_a.cluster != locator_b.cluster) {
498 : 46109260 : return CompareClusters(locator_a.cluster, locator_b.cluster);
499 : : }
500 : : // As final tie-break, compare position within cluster linearization.
501 [ + - + + ]: 1527501 : return entry_a.m_main_lin_index <=> entry_b.m_main_lin_index;
502 : : }
503 : :
504 : : /** Comparator for ChunkData objects in mining order. */
505 : : class ChunkOrder
506 : : {
507 : : const TxGraphImpl* const m_graph;
508 : : public:
509 : 1253 : explicit ChunkOrder(const TxGraphImpl* graph) : m_graph(graph) {}
510 : :
511 : 2197245 : bool operator()(const ChunkData& a, const ChunkData& b) const noexcept
512 : : {
513 : 2197245 : return m_graph->CompareMainTransactions(a.m_graph_index, b.m_graph_index) < 0;
514 : : }
515 : : };
516 : :
517 : : /** Definition for the mining index type. */
518 : : using ChunkIndex = std::set<ChunkData, ChunkOrder>;
519 : :
520 : : /** Index of ChunkData objects, indexing the last transaction in each chunk in the main
521 : : * graph. */
522 : : ChunkIndex m_main_chunkindex;
523 : : /** Number of index-observing objects in existence (BlockBuilderImpls). */
524 : : size_t m_main_chunkindex_observers{0};
525 : : /** Cache of discarded ChunkIndex node handles to reuse, avoiding additional allocation. */
526 : : std::vector<ChunkIndex::node_type> m_main_chunkindex_discarded;
527 : :
528 : : /** A Locator that describes whether, where, and in which Cluster an Entry appears.
529 : : * Every Entry has MAX_LEVELS locators, as it may appear in one Cluster per level.
530 : : *
531 : : * Each level of a Locator is in one of three states:
532 : : *
533 : : * - (P)resent: actually occurs in a Cluster at that level.
534 : : *
535 : : * - (M)issing:
536 : : * - In the main graph: the transaction does not exist in main.
537 : : * - In the staging graph: the transaction's existence is the same as in main. If it doesn't
538 : : * exist in main, (M) in staging means it does not exist there
539 : : * either. If it does exist in main, (M) in staging means the
540 : : * cluster it is in has not been modified in staging, and thus the
541 : : * transaction implicitly exists in staging too (without explicit
542 : : * Cluster object; see PullIn() to create it in staging too).
543 : : *
544 : : * - (R)emoved: only possible in staging; it means the transaction exists in main, but is
545 : : * removed in staging.
546 : : *
547 : : * The following combinations are possible:
548 : : * - (M,M): the transaction doesn't exist in either graph.
549 : : * - (P,M): the transaction exists in both, but only exists explicitly in a Cluster object in
550 : : * main. Its existence in staging is inherited from main.
551 : : * - (P,P): the transaction exists in both, and is materialized in both. Thus, the clusters
552 : : * and/or their linearizations may be different in main and staging.
553 : : * - (M,P): the transaction is added in staging, and does not exist in main.
554 : : * - (P,R): the transaction exists in main, but is removed in staging.
555 : : *
556 : : * When staging does not exist, only (M,M) and (P,M) are possible.
557 : : */
558 : : struct Locator
559 : : {
560 : : /** Which Cluster the Entry appears in (nullptr = missing). */
561 : : Cluster* cluster{nullptr};
562 : : /** Where in the Cluster it appears (if cluster == nullptr: 0 = missing, -1 = removed). */
563 : : DepGraphIndex index{0};
564 : :
565 : : /** Mark this Locator as missing (= same as lower level, or non-existing if level 0). */
566 : 2124 : void SetMissing() noexcept { cluster = nullptr; index = 0; }
567 : : /** Mark this Locator as removed (not allowed in level 0). */
568 : 2124 : void SetRemoved() noexcept { cluster = nullptr; index = DepGraphIndex(-1); }
569 : : /** Mark this Locator as present, in the specified Cluster. */
570 : 330259 : void SetPresent(Cluster* c, DepGraphIndex i) noexcept { cluster = c; index = i; }
571 : : /** Check if this Locator is missing. */
572 [ - + + + ]: 7376831 : bool IsMissing() const noexcept { return cluster == nullptr && index == 0; }
573 : : /** Check if this Locator is removed. */
574 [ + - - + : 7344306 : bool IsRemoved() const noexcept { return cluster == nullptr && index == DepGraphIndex(-1); }
- + ]
575 : : /** Check if this Locator is present (in some Cluster). */
576 [ - - + - ]: 47636761 : bool IsPresent() const noexcept { return cluster != nullptr; }
577 : : };
578 : :
579 : : /** Internal information about each transaction in a TxGraphImpl. */
580 : 125274 : struct Entry
581 : : {
582 : : /** Pointer to the corresponding Ref object if any, or nullptr if unlinked. */
583 : : Ref* m_ref{nullptr};
584 : : /** Iterator to the corresponding ChunkData, if any, and m_main_chunkindex.end() otherwise.
585 : : * This is initialized on construction of the Entry, in AddTransaction. */
586 : : ChunkIndex::iterator m_main_chunkindex_iterator;
587 : : /** Which Cluster and position therein this Entry appears in. ([0] = main, [1] = staged). */
588 : : Locator m_locator[MAX_LEVELS];
589 : : /** The chunk feerate of this transaction in main (if present in m_locator[0]). */
590 : : FeePerWeight m_main_chunk_feerate;
591 : : /** The position this transaction has in the main linearization (if present). */
592 : : LinearizationIndex m_main_lin_index;
593 : : };
594 : :
595 : : /** The set of all transactions (in all levels combined). GraphIndex values index into this. */
596 : : std::vector<Entry> m_entries;
597 : :
598 : : /** Set of Entries which have no linked Ref anymore. */
599 : : std::vector<GraphIndex> m_unlinked;
600 : :
601 : : public:
602 : : /** Construct a new TxGraphImpl with the specified limits. */
603 : 1253 : explicit TxGraphImpl(DepGraphIndex max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept :
604 : 1253 : m_max_cluster_count(max_cluster_count),
605 : 1253 : m_max_cluster_size(max_cluster_size),
606 : 1253 : m_acceptable_iters(acceptable_iters),
607 : 1253 : m_main_chunkindex(ChunkOrder(this))
608 : : {
609 : 1253 : Assume(max_cluster_count >= 1);
610 : 1253 : Assume(max_cluster_count <= MAX_CLUSTER_COUNT_LIMIT);
611 : 1253 : }
612 : :
613 : : /** Destructor. */
614 : : ~TxGraphImpl() noexcept;
615 : :
616 : : // Cannot move or copy (would invalidate TxGraphImpl* in Ref, MiningOrder, EvictionOrder).
617 : : TxGraphImpl(const TxGraphImpl&) = delete;
618 : : TxGraphImpl& operator=(const TxGraphImpl&) = delete;
619 : : TxGraphImpl(TxGraphImpl&&) = delete;
620 : : TxGraphImpl& operator=(TxGraphImpl&&) = delete;
621 : :
622 : : // Simple helper functions.
623 : :
624 : : /** Swap the Entry referred to by a and the one referred to by b. */
625 : : void SwapIndexes(GraphIndex a, GraphIndex b) noexcept;
626 : : /** If idx exists in the specified level ClusterSet (explicitly, or in the level below and not
627 : : * removed), return the Cluster it is in. Otherwise, return nullptr. */
628 : 715744 : Cluster* FindCluster(GraphIndex idx, int level) const noexcept { return FindClusterAndLevel(idx, level).first; }
629 : : /** Like FindCluster, but also return what level the match was found in (-1 if not found). */
630 : : std::pair<Cluster*, int> FindClusterAndLevel(GraphIndex idx, int level) const noexcept;
631 : : /** Extract a Cluster from its ClusterSet, and set its quality to QualityLevel::NONE. */
632 : : std::unique_ptr<Cluster> ExtractCluster(int level, QualityLevel quality, ClusterSetIndex setindex) noexcept;
633 : : /** Delete a Cluster. */
634 : : void DeleteCluster(Cluster& cluster, int level) noexcept;
635 : : /** Insert a Cluster into its ClusterSet. */
636 : : ClusterSetIndex InsertCluster(int level, std::unique_ptr<Cluster>&& cluster, QualityLevel quality) noexcept;
637 : : /** Change the QualityLevel of a Cluster (identified by old_quality and old_index). */
638 : : void SetClusterQuality(int level, QualityLevel old_quality, ClusterSetIndex old_index, QualityLevel new_quality) noexcept;
639 : : /** Get the index of the top level ClusterSet (staging if it exists, main otherwise). */
640 [ + + ]: 2679305 : int GetTopLevel() const noexcept { return m_staging_clusterset.has_value(); }
641 : : /** Get the specified level (staging if it exists and level is TOP, main otherwise). */
642 [ - - + + : 62876 : int GetSpecifiedLevel(Level level) const noexcept { return level == Level::TOP && m_staging_clusterset.has_value(); }
+ - - - -
- - - - -
- - + - ]
643 : : /** Get a reference to the ClusterSet at the specified level (which must exist). */
644 : : ClusterSet& GetClusterSet(int level) noexcept;
645 : : const ClusterSet& GetClusterSet(int level) const noexcept;
646 : : /** Make a transaction not exist at a specified level. It must currently exist there.
647 : : * oversized_tx indicates whether the transaction is an individually-oversized one
648 : : * (OVERSIZED_SINGLETON). */
649 : : void ClearLocator(int level, GraphIndex index, bool oversized_tx) noexcept;
650 : : /** Find which Clusters in main conflict with ones in staging. */
651 : : std::vector<Cluster*> GetConflicts() const noexcept;
652 : : /** Clear an Entry's ChunkData. */
653 : : void ClearChunkData(Entry& entry) noexcept;
654 : : /** Give an Entry a ChunkData object. */
655 : : void CreateChunkData(GraphIndex idx, LinearizationIndex chunk_count) noexcept;
656 : : /** Create an empty GenericClusterImpl object. */
657 : 1941 : std::unique_ptr<GenericClusterImpl> CreateEmptyGenericCluster() noexcept
658 : : {
659 : 1941 : return std::make_unique<GenericClusterImpl>(m_next_sequence_counter++);
660 : : }
661 : : /** Create an empty SingletonClusterImpl object. */
662 : 127028 : std::unique_ptr<SingletonClusterImpl> CreateEmptySingletonCluster() noexcept
663 : : {
664 : 127028 : return std::make_unique<SingletonClusterImpl>(m_next_sequence_counter++);
665 : : }
666 : : /** Create an empty Cluster of the appropriate implementation for the specified (maximum) tx
667 : : * count. */
668 : 127272 : std::unique_ptr<Cluster> CreateEmptyCluster(DepGraphIndex tx_count) noexcept
669 : : {
670 [ + + ]: 127272 : if (tx_count >= SingletonClusterImpl::MIN_INTENDED_TX_COUNT && tx_count <= SingletonClusterImpl::MAX_TX_COUNT) {
671 : 125658 : return CreateEmptySingletonCluster();
672 : : }
673 [ + - ]: 1614 : if (tx_count >= GenericClusterImpl::MIN_INTENDED_TX_COUNT && tx_count <= GenericClusterImpl::MAX_TX_COUNT) {
674 [ - + ]: 1614 : return CreateEmptyGenericCluster();
675 : : }
676 : 0 : assert(false);
677 : : return {};
678 : : }
679 : :
680 : : // Functions for handling Refs.
681 : :
682 : : /** Only called by Ref's move constructor/assignment to update Ref locations. */
683 : 190824 : void UpdateRef(GraphIndex idx, Ref& new_location) noexcept final
684 : : {
685 : 190824 : auto& entry = m_entries[idx];
686 : 190824 : Assume(entry.m_ref != nullptr);
687 : 190824 : entry.m_ref = &new_location;
688 : 190824 : }
689 : :
690 : : /** Only called by Ref::~Ref to unlink Refs, and Ref's move assignment. */
691 : 61263 : void UnlinkRef(GraphIndex idx) noexcept final
692 : : {
693 [ - + ]: 61263 : auto& entry = m_entries[idx];
694 [ - + ]: 61263 : Assume(entry.m_ref != nullptr);
695 [ - + ]: 61263 : Assume(m_main_chunkindex_observers == 0 || !entry.m_locator[0].IsPresent());
696 : 61263 : entry.m_ref = nullptr;
697 : : // Mark the transaction as to be removed in all levels where it explicitly or implicitly
698 : : // exists.
699 : 61263 : bool exists_anywhere{false};
700 : 61263 : bool exists{false};
701 [ + + ]: 122526 : for (int level = 0; level <= GetTopLevel(); ++level) {
702 [ + + ]: 61263 : if (entry.m_locator[level].IsPresent()) {
703 : : exists_anywhere = true;
704 : : exists = true;
705 [ + - ]: 12816 : } else if (entry.m_locator[level].IsRemoved()) {
706 : : exists = false;
707 : : }
708 [ - + ]: 12816 : if (exists) {
709 : 48447 : auto& clusterset = GetClusterSet(level);
710 : 48447 : clusterset.m_to_remove.push_back(idx);
711 : : // Force recomputation of grouping data.
712 [ + + ]: 48447 : clusterset.m_group_data = std::nullopt;
713 : : // Do not wipe the oversized state of main if staging exists. The reason for this
714 : : // is that the alternative would mean that cluster merges may need to be applied to
715 : : // a formerly-oversized main graph while staging exists (to satisfy chunk feerate
716 : : // queries into main, for example), and such merges could conflict with pulls of
717 : : // some of their constituents into staging.
718 [ + - + - ]: 109710 : if (level == GetTopLevel() && clusterset.m_oversized == true) {
719 : 0 : clusterset.m_oversized = std::nullopt;
720 : : }
721 : : }
722 : : }
723 : 61263 : m_unlinked.push_back(idx);
724 [ + + ]: 61263 : if (!exists_anywhere) Compact();
725 : 61263 : }
726 : :
727 : : // Functions related to various normalization/application steps.
728 : : /** Get rid of unlinked Entry objects in m_entries, if possible (this changes the GraphIndex
729 : : * values for remaining Entry objects, so this only does something when no to-be-applied
730 : : * operations or staged removals referring to GraphIndexes remain). */
731 : : void Compact() noexcept;
732 : : /** If cluster is not in staging, copy it there, and return a pointer to it.
733 : : * Staging must exist, and this modifies the locators of its
734 : : * transactions from inherited (P,M) to explicit (P,P). */
735 : : Cluster* PullIn(Cluster* cluster, int level) noexcept;
736 : : /** Apply all removals queued up in m_to_remove to the relevant Clusters (which get a
737 : : * NEEDS_SPLIT* QualityLevel) up to the specified level. */
738 : : void ApplyRemovals(int up_to_level) noexcept;
739 : : /** Split an individual cluster. */
740 : : void Split(Cluster& cluster, int level) noexcept;
741 : : /** Split all clusters that need splitting up to the specified level. */
742 : : void SplitAll(int up_to_level) noexcept;
743 : : /** Populate m_group_data based on m_deps_to_add in the specified level. */
744 : : void GroupClusters(int level) noexcept;
745 : : /** Merge the specified clusters. */
746 : : void Merge(std::span<Cluster*> to_merge, int level) noexcept;
747 : : /** Apply all m_deps_to_add to the relevant Clusters in the specified level. */
748 : : void ApplyDependencies(int level) noexcept;
749 : : /** Make a specified Cluster have quality ACCEPTABLE or OPTIMAL. */
750 : : void MakeAcceptable(Cluster& cluster, int level) noexcept;
751 : : /** Make all Clusters at the specified level have quality ACCEPTABLE or OPTIMAL. */
752 : : void MakeAllAcceptable(int level) noexcept;
753 : :
754 : : // Implementations for the public TxGraph interface.
755 : :
756 : : Ref AddTransaction(const FeePerWeight& feerate) noexcept final;
757 : : void RemoveTransaction(const Ref& arg) noexcept final;
758 : : void AddDependency(const Ref& parent, const Ref& child) noexcept final;
759 : : void SetTransactionFee(const Ref&, int64_t fee) noexcept final;
760 : :
761 : : bool DoWork(uint64_t iters) noexcept final;
762 : :
763 : : void StartStaging() noexcept final;
764 : : void CommitStaging() noexcept final;
765 : : void AbortStaging() noexcept final;
766 : 60336 : bool HaveStaging() const noexcept final { return m_staging_clusterset.has_value(); }
767 : :
768 : : bool Exists(const Ref& arg, Level level) noexcept final;
769 : : FeePerWeight GetMainChunkFeerate(const Ref& arg) noexcept final;
770 : : FeePerWeight GetIndividualFeerate(const Ref& arg) noexcept final;
771 : : std::vector<Ref*> GetCluster(const Ref& arg, Level level) noexcept final;
772 : : std::vector<Ref*> GetAncestors(const Ref& arg, Level level) noexcept final;
773 : : std::vector<Ref*> GetDescendants(const Ref& arg, Level level) noexcept final;
774 : : std::vector<Ref*> GetAncestorsUnion(std::span<const Ref* const> args, Level level) noexcept final;
775 : : std::vector<Ref*> GetDescendantsUnion(std::span<const Ref* const> args, Level level) noexcept final;
776 : : GraphIndex GetTransactionCount(Level level) noexcept final;
777 : : bool IsOversized(Level level) noexcept final;
778 : : std::strong_ordering CompareMainOrder(const Ref& a, const Ref& b) noexcept final;
779 : : GraphIndex CountDistinctClusters(std::span<const Ref* const> refs, Level level) noexcept final;
780 : : std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>> GetMainStagingDiagrams() noexcept final;
781 : : std::vector<Ref*> Trim() noexcept final;
782 : :
783 : : std::unique_ptr<BlockBuilder> GetBlockBuilder() noexcept final;
784 : : std::pair<std::vector<Ref*>, FeePerWeight> GetWorstMainChunk() noexcept final;
785 : :
786 : : size_t GetMainMemoryUsage() noexcept final;
787 : :
788 : : void SanityCheck() const final;
789 : : };
790 : :
791 : 181761805 : TxGraphImpl::ClusterSet& TxGraphImpl::GetClusterSet(int level) noexcept
792 : : {
793 [ + + ]: 181761805 : if (level == 0) return m_main_clusterset;
794 : 362028 : Assume(level == 1);
795 : 362028 : Assume(m_staging_clusterset.has_value());
796 : 362028 : return *m_staging_clusterset;
797 : : }
798 : :
799 : 204025 : const TxGraphImpl::ClusterSet& TxGraphImpl::GetClusterSet(int level) const noexcept
800 : : {
801 [ + + ]: 204025 : if (level == 0) return m_main_clusterset;
802 : 50913 : Assume(level == 1);
803 : 50913 : Assume(m_staging_clusterset.has_value());
804 : 50913 : return *m_staging_clusterset;
805 : : }
806 : :
807 : : /** Implementation of the TxGraph::BlockBuilder interface. */
808 : : class BlockBuilderImpl final : public TxGraph::BlockBuilder
809 : : {
810 : : /** Which TxGraphImpl this object is doing block building for. It will have its
811 : : * m_main_chunkindex_observers incremented as long as this BlockBuilderImpl exists. */
812 : : TxGraphImpl* const m_graph;
813 : : /** Cluster sequence numbers which we're not including further transactions from. */
814 : : std::unordered_set<uint64_t> m_excluded_clusters;
815 : : /** Iterator to the current chunk in the chunk index. end() if nothing further remains. */
816 : : TxGraphImpl::ChunkIndex::const_iterator m_cur_iter;
817 : : /** Which cluster the current chunk belongs to, so we can exclude further transactions from it
818 : : * when that chunk is skipped. */
819 : : Cluster* m_cur_cluster;
820 : : /** Whether we know that m_cur_iter points to the last chunk of m_cur_cluster. */
821 : : bool m_known_end_of_cluster;
822 : :
823 : : // Move m_cur_iter / m_cur_cluster to the next acceptable chunk.
824 : : void Next() noexcept;
825 : :
826 : : public:
827 : : /** Construct a new BlockBuilderImpl to build blocks for the provided graph. */
828 : : BlockBuilderImpl(TxGraphImpl& graph) noexcept;
829 : :
830 : : // Implement the public interface.
831 : : ~BlockBuilderImpl() final;
832 : : std::optional<std::pair<std::vector<TxGraph::Ref*>, FeePerWeight>> GetCurrentChunk() noexcept final;
833 : : void Include() noexcept final;
834 : : void Skip() noexcept final;
835 : : };
836 : :
837 : 310630 : void TxGraphImpl::ClearChunkData(Entry& entry) noexcept
838 : : {
839 [ + + ]: 310630 : if (entry.m_main_chunkindex_iterator != m_main_chunkindex.end()) {
840 : 113291 : Assume(m_main_chunkindex_observers == 0);
841 : : // If the Entry has a non-empty m_main_chunkindex_iterator, extract it, and move the handle
842 : : // to the cache of discarded chunkindex entries.
843 : 113291 : m_main_chunkindex_discarded.emplace_back(m_main_chunkindex.extract(entry.m_main_chunkindex_iterator));
844 : 113291 : entry.m_main_chunkindex_iterator = m_main_chunkindex.end();
845 : : }
846 : 310630 : }
847 : :
848 : 180867 : void TxGraphImpl::CreateChunkData(GraphIndex idx, LinearizationIndex chunk_count) noexcept
849 : : {
850 [ + + ]: 180867 : auto& entry = m_entries[idx];
851 [ + + ]: 180867 : if (!m_main_chunkindex_discarded.empty()) {
852 : : // Reuse an discarded node handle.
853 : 5974 : auto& node = m_main_chunkindex_discarded.back().value();
854 : 5974 : node.m_graph_index = idx;
855 : 5974 : node.m_chunk_count = chunk_count;
856 : 5974 : auto insert_result = m_main_chunkindex.insert(std::move(m_main_chunkindex_discarded.back()));
857 : 5974 : Assume(insert_result.inserted);
858 : 5974 : entry.m_main_chunkindex_iterator = insert_result.position;
859 : 5974 : m_main_chunkindex_discarded.pop_back();
860 : 5974 : } else {
861 : : // Construct a new entry.
862 : 174893 : auto emplace_result = m_main_chunkindex.emplace(idx, chunk_count);
863 : 174893 : Assume(emplace_result.second);
864 : 174893 : entry.m_main_chunkindex_iterator = emplace_result.first;
865 : : }
866 : 180867 : }
867 : :
868 : 67908 : size_t GenericClusterImpl::TotalMemoryUsage() const noexcept
869 : : {
870 : 67908 : return // Dynamic memory allocated in this Cluster.
871 [ - + - + ]: 135816 : memusage::DynamicUsage(m_mapping) + memusage::DynamicUsage(m_linearization) +
872 : : // Dynamic memory usage inside m_depgraph.
873 [ - + ]: 67908 : m_depgraph.DynamicMemoryUsage() +
874 : : // Memory usage of the allocated Cluster itself.
875 : 67908 : memusage::MallocUsage(sizeof(GenericClusterImpl)) +
876 : : // Memory usage of the ClusterSet::m_clusters entry.
877 : 67908 : sizeof(std::unique_ptr<Cluster>);
878 : : }
879 : :
880 : 7343883 : size_t SingletonClusterImpl::TotalMemoryUsage() const noexcept
881 : : {
882 : 7343883 : return // Memory usage of the allocated SingletonClusterImpl itself.
883 : 7343883 : memusage::MallocUsage(sizeof(SingletonClusterImpl)) +
884 : : // Memory usage of the ClusterSet::m_clusters entry.
885 : 7343883 : sizeof(std::unique_ptr<Cluster>);
886 : : }
887 : :
888 : 58361 : uint64_t GenericClusterImpl::GetTotalTxSize() const noexcept
889 : : {
890 : 58361 : uint64_t ret{0};
891 [ + + ]: 353495 : for (auto i : m_linearization) {
892 : 295134 : ret += m_depgraph.FeeRate(i).size;
893 : : }
894 : 58361 : return ret;
895 : : }
896 : :
897 : 35 : DepGraphIndex GenericClusterImpl::AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept
898 : : {
899 : 35 : Assume(graph_idx != GraphIndex(-1));
900 : 35 : auto ret = m_depgraph.AddTransaction(feerate);
901 : 35 : m_mapping.push_back(graph_idx);
902 : 35 : m_linearization.push_back(ret);
903 : 35 : return ret;
904 : : }
905 : :
906 : 125658 : DepGraphIndex SingletonClusterImpl::AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept
907 : : {
908 : 125658 : Assume(!GetTxCount());
909 : 125658 : m_graph_index = graph_idx;
910 : 125658 : m_feerate = feerate;
911 : 125658 : return 0;
912 : : }
913 : :
914 : 35 : void GenericClusterImpl::AddDependencies(SetType parents, DepGraphIndex child) noexcept
915 : : {
916 : 35 : m_depgraph.AddDependencies(parents, child);
917 : 35 : }
918 : :
919 : 384 : void SingletonClusterImpl::AddDependencies(SetType parents, DepGraphIndex child) noexcept
920 : : {
921 : : // Singletons cannot have any dependencies.
922 [ - + ]: 384 : Assume(child == 0);
923 [ - + ]: 384 : Assume(parents == SetType{} || parents == SetType::Fill(0));
924 : 384 : }
925 : :
926 : 9 : void GenericClusterImpl::ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept
927 : : {
928 [ + + ]: 46 : for (auto pos : m_linearization) {
929 : 37 : visit1_fn(pos, m_mapping[pos], FeePerWeight::FromFeeFrac(m_depgraph.FeeRate(pos)));
930 : : }
931 [ + + ]: 46 : for (auto pos : m_linearization) {
932 : 37 : visit2_fn(pos, m_mapping[pos], m_depgraph.GetReducedParents(pos));
933 : : }
934 : : // Purge this Cluster, now that everything has been moved.
935 : 9 : m_depgraph = DepGraph<SetType>{};
936 [ + - ]: 9 : m_linearization.clear();
937 [ + - ]: 9 : m_mapping.clear();
938 : 9 : }
939 : :
940 : 7565 : void SingletonClusterImpl::ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept
941 : : {
942 [ + - ]: 7565 : if (GetTxCount()) {
943 : 7565 : visit1_fn(0, m_graph_index, m_feerate);
944 : 7565 : visit2_fn(0, m_graph_index, SetType{});
945 : 7565 : m_graph_index = NO_GRAPH_INDEX;
946 : : }
947 : 7565 : }
948 : :
949 : 53638 : int GenericClusterImpl::GetLevel(const TxGraphImpl& graph) const noexcept
950 : : {
951 : : // GetLevel() does not work for empty Clusters.
952 [ + - ]: 53638 : if (!Assume(!m_linearization.empty())) return -1;
953 : :
954 : : // Pick an arbitrary Entry that occurs in this Cluster.
955 : 53638 : const auto& entry = graph.m_entries[m_mapping[m_linearization.front()]];
956 : : // See if there is a level whose Locator matches this Cluster, if so return that level.
957 [ + - ]: 53658 : for (int level = 0; level < MAX_LEVELS; ++level) {
958 [ + + ]: 53658 : if (entry.m_locator[level].cluster == this) return level;
959 : : }
960 : : // Given that we started with an Entry that occurs in this Cluster, one of its Locators must
961 : : // point back to it.
962 : 0 : assert(false);
963 : : return -1;
964 : : }
965 : :
966 : 7181382 : int SingletonClusterImpl::GetLevel(const TxGraphImpl& graph) const noexcept
967 : : {
968 : : // GetLevel() does not work for empty Clusters.
969 [ + - ]: 7181382 : if (!Assume(GetTxCount())) return -1;
970 : :
971 : : // Get the Entry in this Cluster.
972 : 7181382 : const auto& entry = graph.m_entries[m_graph_index];
973 : : // See if there is a level whose Locator matches this Cluster, if so return that level.
974 [ + - ]: 7181658 : for (int level = 0; level < MAX_LEVELS; ++level) {
975 [ + + ]: 7181658 : if (entry.m_locator[level].cluster == this) return level;
976 : : }
977 : : // Given that we started with an Entry that occurs in this Cluster, one of its Locators must
978 : : // point back to it.
979 : 0 : assert(false);
980 : : return -1;
981 : : }
982 : :
983 : 48737 : void TxGraphImpl::ClearLocator(int level, GraphIndex idx, bool oversized_tx) noexcept
984 : : {
985 [ + + ]: 48737 : auto& entry = m_entries[idx];
986 : 48737 : auto& clusterset = GetClusterSet(level);
987 [ + + ]: 48737 : Assume(entry.m_locator[level].IsPresent());
988 : : // Change the locator from Present to Missing or Removed.
989 [ + + - + ]: 48737 : if (level == 0 || !entry.m_locator[level - 1].IsPresent()) {
990 : 46613 : entry.m_locator[level].SetMissing();
991 : : } else {
992 : 2124 : entry.m_locator[level].SetRemoved();
993 : 2124 : clusterset.m_removed.push_back(idx);
994 : : }
995 : : // Update the transaction count.
996 : 48737 : --clusterset.m_txcount;
997 : 48737 : clusterset.m_txcount_oversized -= oversized_tx;
998 : : // If clearing main, adjust the status of Locators of this transaction in staging, if it exists.
999 [ + + + + ]: 48737 : if (level == 0 && GetTopLevel() == 1) {
1000 [ + + ]: 1967 : if (entry.m_locator[1].IsRemoved()) {
1001 : 1536 : entry.m_locator[1].SetMissing();
1002 [ - + ]: 431 : } else if (!entry.m_locator[1].IsPresent()) {
1003 : 0 : --m_staging_clusterset->m_txcount;
1004 : 0 : m_staging_clusterset->m_txcount_oversized -= oversized_tx;
1005 : : }
1006 : : }
1007 [ + + ]: 48737 : if (level == 0) ClearChunkData(entry);
1008 : 48737 : }
1009 : :
1010 : 12876 : void GenericClusterImpl::Updated(TxGraphImpl& graph, int level) noexcept
1011 : : {
1012 : : // Update all the Locators for this Cluster's Entry objects.
1013 [ + + ]: 158537 : for (DepGraphIndex idx : m_linearization) {
1014 [ + + ]: 145661 : auto& entry = graph.m_entries[m_mapping[idx]];
1015 : : // Discard any potential ChunkData prior to modifying the Cluster (as that could
1016 : : // invalidate its ordering).
1017 [ + + ]: 145661 : if (level == 0) graph.ClearChunkData(entry);
1018 : 145661 : entry.m_locator[level].SetPresent(this, idx);
1019 : : }
1020 : : // If this is for the main graph (level = 0), and the Cluster's quality is ACCEPTABLE or
1021 : : // OPTIMAL, compute its chunking and store its information in the Entry's m_main_lin_index
1022 : : // and m_main_chunk_feerate. These fields are only accessed after making the entire graph
1023 : : // ACCEPTABLE, so it is pointless to compute these if we haven't reached that quality level
1024 : : // yet.
1025 [ + + + + ]: 12876 : if (level == 0 && IsAcceptable()) {
1026 [ - + ]: 5522 : auto chunking = ChunkLinearizationInfo(m_depgraph, m_linearization);
1027 : 5522 : LinearizationIndex lin_idx{0};
1028 : : // Iterate over the chunks.
1029 [ - + + + ]: 72036 : for (unsigned chunk_idx = 0; chunk_idx < chunking.size(); ++chunk_idx) {
1030 : 66514 : auto& chunk = chunking[chunk_idx];
1031 : 66514 : auto chunk_count = chunk.transactions.Count();
1032 : 66514 : Assume(chunk_count > 0);
1033 : : // Iterate over the transactions in the linearization, which must match those in chunk.
1034 : 73742 : while (true) {
1035 [ + + ]: 73742 : DepGraphIndex idx = m_linearization[lin_idx];
1036 : 73742 : GraphIndex graph_idx = m_mapping[idx];
1037 : 73742 : auto& entry = graph.m_entries[graph_idx];
1038 : 73742 : entry.m_main_lin_index = lin_idx++;
1039 [ + + ]: 73742 : entry.m_main_chunk_feerate = FeePerWeight::FromFeeFrac(chunk.feerate);
1040 [ + + ]: 73742 : Assume(chunk.transactions[idx]);
1041 [ + + ]: 73742 : chunk.transactions.Reset(idx);
1042 [ + + ]: 73742 : if (chunk.transactions.None()) {
1043 : : // Last transaction in the chunk.
1044 [ + + - + : 66514 : if (chunk_count == 1 && chunk_idx + 1 == chunking.size()) {
+ + ]
1045 : : // If this is the final chunk of the cluster, and it contains just a single
1046 : : // transaction (which will always be true for the very common singleton
1047 : : // clusters), store the special value -1 as chunk count.
1048 : : chunk_count = LinearizationIndex(-1);
1049 : : }
1050 : 66514 : graph.CreateChunkData(graph_idx, chunk_count);
1051 : 66514 : break;
1052 : : }
1053 : : }
1054 : : }
1055 : 5522 : }
1056 : 12876 : }
1057 : :
1058 : 176996 : void SingletonClusterImpl::Updated(TxGraphImpl& graph, int level) noexcept
1059 : : {
1060 : : // Don't do anything if this is empty.
1061 [ + - ]: 176996 : if (GetTxCount() == 0) return;
1062 : :
1063 [ + + ]: 176996 : auto& entry = graph.m_entries[m_graph_index];
1064 : : // Discard any potential ChunkData prior to modifying the Cluster (as that could
1065 : : // invalidate its ordering).
1066 [ + + ]: 176996 : if (level == 0) graph.ClearChunkData(entry);
1067 : 176996 : entry.m_locator[level].SetPresent(this, 0);
1068 : : // If this is for the main graph (level = 0), compute its chunking and store its information in
1069 : : // the Entry's m_main_lin_index and m_main_chunk_feerate.
1070 [ + + + + ]: 176996 : if (level == 0 && IsAcceptable()) {
1071 : 114353 : entry.m_main_lin_index = 0;
1072 : 114353 : entry.m_main_chunk_feerate = m_feerate;
1073 : : // Always use the special LinearizationIndex(-1), indicating singleton chunk at end of
1074 : : // Cluster, here.
1075 : 114353 : graph.CreateChunkData(m_graph_index, LinearizationIndex(-1));
1076 : : }
1077 : : }
1078 : :
1079 : 127 : void GenericClusterImpl::GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept
1080 : : {
1081 [ + + ]: 1144 : for (auto i : m_linearization) {
1082 [ + + ]: 1017 : auto& entry = graph.m_entries[m_mapping[i]];
1083 : : // For every transaction Entry in this Cluster, if it also exists in a lower-level Cluster,
1084 : : // then that Cluster conflicts.
1085 [ + + ]: 1017 : if (entry.m_locator[0].IsPresent()) {
1086 : 864 : out.push_back(entry.m_locator[0].cluster);
1087 : : }
1088 : : }
1089 : 127 : }
1090 : :
1091 : 50974 : void SingletonClusterImpl::GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept
1092 : : {
1093 : : // Empty clusters have no conflicts.
1094 [ + - ]: 50974 : if (GetTxCount() == 0) return;
1095 : :
1096 [ + + ]: 50974 : auto& entry = graph.m_entries[m_graph_index];
1097 : : // If the transaction in this Cluster also exists in a lower-level Cluster, then that Cluster
1098 : : // conflicts.
1099 [ + + ]: 50974 : if (entry.m_locator[0].IsPresent()) {
1100 : 130 : out.push_back(entry.m_locator[0].cluster);
1101 : : }
1102 : : }
1103 : :
1104 : 50913 : std::vector<Cluster*> TxGraphImpl::GetConflicts() const noexcept
1105 : : {
1106 : 50913 : Assume(GetTopLevel() == 1);
1107 : 50913 : auto& clusterset = GetClusterSet(1);
1108 : 50913 : std::vector<Cluster*> ret;
1109 : : // All main Clusters containing transactions in m_removed (so (P,R) ones) are conflicts.
1110 [ + + ]: 54563 : for (auto i : clusterset.m_removed) {
1111 [ + - ]: 3650 : auto& entry = m_entries[i];
1112 [ + - ]: 3650 : if (entry.m_locator[0].IsPresent()) {
1113 : 3650 : ret.push_back(entry.m_locator[0].cluster);
1114 : : }
1115 : : }
1116 : : // Then go over all Clusters at this level, and find their conflicts (the (P,P) ones).
1117 [ + + ]: 407304 : for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
1118 : 356391 : auto& clusters = clusterset.m_clusters[quality];
1119 [ + + ]: 407492 : for (const auto& cluster : clusters) {
1120 : 51101 : cluster->GetConflicts(*this, ret);
1121 : : }
1122 : : }
1123 : : // Deduplicate the result (the same Cluster may appear multiple times).
1124 : 50913 : std::sort(ret.begin(), ret.end(), [](Cluster* a, Cluster* b) noexcept { return CompareClusters(a, b) < 0; });
1125 : 50913 : ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
1126 : 50913 : return ret;
1127 : : }
1128 : :
1129 : 327 : Cluster* GenericClusterImpl::CopyToStaging(TxGraphImpl& graph) const noexcept
1130 : : {
1131 : : // Construct an empty Cluster.
1132 : 327 : auto ret = graph.CreateEmptyGenericCluster();
1133 : 327 : auto ptr = ret.get();
1134 : : // Copy depgraph, mapping, and linearization.
1135 : 327 : ptr->m_depgraph = m_depgraph;
1136 : 327 : ptr->m_mapping = m_mapping;
1137 : 327 : ptr->m_linearization = m_linearization;
1138 : : // Insert the new Cluster into the graph.
1139 : 327 : graph.InsertCluster(/*level=*/1, std::move(ret), m_quality);
1140 : : // Update its Locators.
1141 : 327 : ptr->Updated(graph, /*level=*/1);
1142 : : // Update memory usage.
1143 : 327 : graph.GetClusterSet(/*level=*/1).m_cluster_usage += ptr->TotalMemoryUsage();
1144 [ - + ]: 327 : return ptr;
1145 : 327 : }
1146 : :
1147 : 1370 : Cluster* SingletonClusterImpl::CopyToStaging(TxGraphImpl& graph) const noexcept
1148 : : {
1149 : : // Construct an empty Cluster.
1150 : 1370 : auto ret = graph.CreateEmptySingletonCluster();
1151 : 1370 : auto ptr = ret.get();
1152 : : // Copy data.
1153 : 1370 : ptr->m_graph_index = m_graph_index;
1154 : 1370 : ptr->m_feerate = m_feerate;
1155 : : // Insert the new Cluster into the graph.
1156 : 1370 : graph.InsertCluster(/*level=*/1, std::move(ret), m_quality);
1157 : : // Update its Locators.
1158 : 1370 : ptr->Updated(graph, /*level=*/1);
1159 : : // Update memory usage.
1160 : 1370 : graph.GetClusterSet(/*level=*/1).m_cluster_usage += ptr->TotalMemoryUsage();
1161 : 2740 : return ptr;
1162 : 1370 : }
1163 : :
1164 : 1456 : void GenericClusterImpl::ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept
1165 : : {
1166 : : // Iterate over the prefix of to_remove that applies to this cluster.
1167 : 1456 : Assume(!to_remove.empty());
1168 : 1456 : SetType todo;
1169 : 1456 : graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1170 : 6082 : do {
1171 [ - + ]: 6082 : GraphIndex idx = to_remove.front();
1172 [ - + + + ]: 6082 : Assume(idx < graph.m_entries.size());
1173 [ + + ]: 6082 : auto& entry = graph.m_entries[idx];
1174 : 6082 : auto& locator = entry.m_locator[level];
1175 : : // Stop once we hit an entry that applies to another Cluster.
1176 [ + + ]: 6082 : if (locator.cluster != this) break;
1177 : : // - Remember it in a set of to-remove DepGraphIndexes.
1178 [ + + ]: 5033 : todo.Set(locator.index);
1179 : : // - Remove from m_mapping. This isn't strictly necessary as unused positions in m_mapping
1180 : : // are just never accessed, but set it to -1 here to increase the ability to detect a bug
1181 : : // that causes it to be accessed regardless.
1182 [ + + ]: 5033 : m_mapping[locator.index] = GraphIndex(-1);
1183 : : // - Remove its linearization index from the Entry (if in main).
1184 [ + + ]: 5033 : if (level == 0) {
1185 : 4276 : entry.m_main_lin_index = LinearizationIndex(-1);
1186 : : }
1187 : : // - Mark it as missing/removed in the Entry's locator.
1188 : 5033 : graph.ClearLocator(level, idx, m_quality == QualityLevel::OVERSIZED_SINGLETON);
1189 [ + + ]: 5033 : to_remove = to_remove.subspan(1);
1190 [ + + ]: 5033 : } while(!to_remove.empty());
1191 : :
1192 : 1456 : Assume(todo.Any());
1193 : : // Wipe from the Cluster's DepGraph (this is O(n) regardless of the number of entries
1194 : : // removed, so we benefit from batching all the removals).
1195 : 1456 : m_depgraph.RemoveTransactions(todo);
1196 [ - + ]: 1456 : m_mapping.resize(m_depgraph.PositionRange());
1197 : :
1198 : : // Filter removed transactions out of m_linearization.
1199 : 1456 : m_linearization.erase(std::remove_if(m_linearization.begin(), m_linearization.end(),
1200 : 5751 : [&](auto pos) { return todo[pos]; }),
1201 : 1456 : m_linearization.end());
1202 : :
1203 : 1456 : Compact();
1204 : 1456 : graph.GetClusterSet(level).m_cluster_usage += TotalMemoryUsage();
1205 [ + + ]: 1456 : auto new_quality = IsTopological() ? QualityLevel::NEEDS_SPLIT : QualityLevel::NEEDS_SPLIT_FIX;
1206 : 1456 : graph.SetClusterQuality(level, m_quality, m_setindex, new_quality);
1207 : 1456 : Updated(graph, level);
1208 : 1456 : }
1209 : :
1210 : 41737 : void SingletonClusterImpl::ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept
1211 : : {
1212 : : // We can only remove the one transaction this Cluster has.
1213 : 41737 : Assume(!to_remove.empty());
1214 : 41737 : Assume(GetTxCount());
1215 : 41737 : Assume(to_remove.front() == m_graph_index);
1216 : : // Pop all copies of m_graph_index from the front of to_remove (at least one, but there may be
1217 : : // multiple).
1218 : 41737 : do {
1219 [ + + ]: 41737 : to_remove = to_remove.subspan(1);
1220 [ + + + - : 79950 : } while (!to_remove.empty() && to_remove.front() == m_graph_index);
- + ]
1221 : : // Clear this cluster.
1222 : 41737 : graph.ClearLocator(level, m_graph_index, m_quality == QualityLevel::OVERSIZED_SINGLETON);
1223 : 41737 : m_graph_index = NO_GRAPH_INDEX;
1224 : 41737 : graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::NEEDS_SPLIT);
1225 : : // No need to account for m_cluster_usage changes here, as SingletonClusterImpl has constant
1226 : : // memory usage.
1227 : 41737 : }
1228 : :
1229 : 224 : void GenericClusterImpl::Clear(TxGraphImpl& graph, int level) noexcept
1230 : : {
1231 [ - + ]: 224 : Assume(GetTxCount());
1232 : 224 : graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1233 [ + + ]: 1197 : for (auto i : m_linearization) {
1234 : 973 : graph.ClearLocator(level, m_mapping[i], m_quality == QualityLevel::OVERSIZED_SINGLETON);
1235 : : }
1236 : 224 : m_depgraph = {};
1237 [ + - ]: 224 : m_linearization.clear();
1238 [ + - ]: 224 : m_mapping.clear();
1239 : 224 : }
1240 : :
1241 : 994 : void SingletonClusterImpl::Clear(TxGraphImpl& graph, int level) noexcept
1242 : : {
1243 : 994 : Assume(GetTxCount());
1244 : 994 : graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1245 : 994 : graph.ClearLocator(level, m_graph_index, m_quality == QualityLevel::OVERSIZED_SINGLETON);
1246 : 994 : m_graph_index = NO_GRAPH_INDEX;
1247 : 994 : }
1248 : :
1249 : 51 : void GenericClusterImpl::MoveToMain(TxGraphImpl& graph) noexcept
1250 : : {
1251 [ + + ]: 492 : for (auto i : m_linearization) {
1252 : 441 : GraphIndex idx = m_mapping[i];
1253 : 441 : auto& entry = graph.m_entries[idx];
1254 : 441 : entry.m_locator[1].SetMissing();
1255 : : }
1256 : 51 : auto quality = m_quality;
1257 : : // Subtract memory usage from staging and add it to main.
1258 : 51 : graph.GetClusterSet(/*level=*/1).m_cluster_usage -= TotalMemoryUsage();
1259 : 51 : graph.GetClusterSet(/*level=*/0).m_cluster_usage += TotalMemoryUsage();
1260 : : // Remove cluster itself from staging and add it to main.
1261 : 51 : auto cluster = graph.ExtractCluster(1, quality, m_setindex);
1262 : 51 : graph.InsertCluster(/*level=*/0, std::move(cluster), quality);
1263 : 51 : Updated(graph, /*level=*/0);
1264 : 51 : }
1265 : :
1266 : 49681 : void SingletonClusterImpl::MoveToMain(TxGraphImpl& graph) noexcept
1267 : : {
1268 [ + - ]: 49681 : if (GetTxCount()) {
1269 : 49681 : auto& entry = graph.m_entries[m_graph_index];
1270 : 49681 : entry.m_locator[1].SetMissing();
1271 : : }
1272 : 49681 : auto quality = m_quality;
1273 : 49681 : graph.GetClusterSet(/*level=*/1).m_cluster_usage -= TotalMemoryUsage();
1274 : 49681 : auto cluster = graph.ExtractCluster(/*level=*/1, quality, m_setindex);
1275 : 49681 : graph.InsertCluster(/*level=*/0, std::move(cluster), quality);
1276 : 49681 : graph.GetClusterSet(/*level=*/0).m_cluster_usage += TotalMemoryUsage();
1277 : 49681 : Updated(graph, /*level=*/0);
1278 : 49681 : }
1279 : :
1280 : 6903 : void GenericClusterImpl::Compact() noexcept
1281 : : {
1282 : 6903 : m_linearization.shrink_to_fit();
1283 : 6903 : m_mapping.shrink_to_fit();
1284 : 6903 : m_depgraph.Compact();
1285 : 6903 : }
1286 : :
1287 : 384 : void SingletonClusterImpl::Compact() noexcept
1288 : : {
1289 : : // Nothing to compact; SingletonClusterImpl is constant size.
1290 : 384 : }
1291 : :
1292 : 403 : void GenericClusterImpl::AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept
1293 : : {
1294 [ - + ]: 403 : auto chunk_feerates = ChunkLinearization(m_depgraph, m_linearization);
1295 [ - + - + ]: 403 : ret.reserve(ret.size() + chunk_feerates.size());
1296 : 403 : ret.insert(ret.end(), chunk_feerates.begin(), chunk_feerates.end());
1297 : 403 : }
1298 : :
1299 : 2653 : void SingletonClusterImpl::AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept
1300 : : {
1301 [ + - ]: 2653 : if (GetTxCount()) {
1302 : 2653 : ret.push_back(m_feerate);
1303 : : }
1304 : 2653 : }
1305 : :
1306 : 0 : uint64_t GenericClusterImpl::AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept
1307 : : {
1308 [ # # ]: 0 : Assume(IsAcceptable());
1309 [ # # ]: 0 : auto linchunking = ChunkLinearizationInfo(m_depgraph, m_linearization);
1310 : 0 : LinearizationIndex pos{0};
1311 : 0 : uint64_t size{0};
1312 : 0 : auto prev_index = GraphIndex(-1);
1313 : : // Iterate over the chunks of this cluster's linearization.
1314 [ # # ]: 0 : for (const auto& [chunk, chunk_feerate] : linchunking) {
1315 : : // Iterate over the transactions of that chunk, in linearization order.
1316 : 0 : auto chunk_tx_count = chunk.Count();
1317 [ # # ]: 0 : for (unsigned j = 0; j < chunk_tx_count; ++j) {
1318 : 0 : auto cluster_idx = m_linearization[pos];
1319 : : // The transaction must appear in the chunk.
1320 : 0 : Assume(chunk[cluster_idx]);
1321 : : // Construct a new element in ret.
1322 : 0 : auto& entry = ret.emplace_back();
1323 [ # # ]: 0 : entry.m_chunk_feerate = FeePerWeight::FromFeeFrac(chunk_feerate);
1324 [ # # ]: 0 : entry.m_index = m_mapping[cluster_idx];
1325 : : // If this is not the first transaction of the cluster linearization, it has an
1326 : : // implicit dependency on its predecessor.
1327 [ # # ]: 0 : if (pos != 0) deps.emplace_back(prev_index, entry.m_index);
1328 : 0 : prev_index = entry.m_index;
1329 : 0 : entry.m_tx_size = m_depgraph.FeeRate(cluster_idx).size;
1330 : 0 : size += entry.m_tx_size;
1331 : 0 : ++pos;
1332 : : }
1333 : : }
1334 : 0 : return size;
1335 : 0 : }
1336 : :
1337 : 64217 : uint64_t SingletonClusterImpl::AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept
1338 : : {
1339 [ + - ]: 64217 : if (!GetTxCount()) return 0;
1340 : 64217 : auto& entry = ret.emplace_back();
1341 : 64217 : entry.m_chunk_feerate = m_feerate;
1342 : 64217 : entry.m_index = m_graph_index;
1343 : 64217 : entry.m_tx_size = m_feerate.size;
1344 : 64217 : return m_feerate.size;
1345 : : }
1346 : :
1347 : 1456 : bool GenericClusterImpl::Split(TxGraphImpl& graph, int level) noexcept
1348 : : {
1349 : : // This function can only be called when the Cluster needs splitting.
1350 [ + + ]: 1456 : Assume(NeedsSplitting());
1351 : : // Determine the new quality the split-off Clusters will have.
1352 [ + + ]: 1456 : QualityLevel new_quality = IsTopological() ? QualityLevel::NEEDS_RELINEARIZE : QualityLevel::NEEDS_FIX;
1353 : : /** Which positions are still left in this Cluster. */
1354 : 1456 : auto todo = m_depgraph.Positions();
1355 : : /** Mapping from transaction positions in this Cluster to the Cluster where it ends up, and
1356 : : * its position therein. */
1357 [ - + ]: 1456 : std::vector<std::pair<Cluster*, DepGraphIndex>> remap(m_depgraph.PositionRange());
1358 : 1456 : std::vector<Cluster*> new_clusters;
1359 : 1456 : bool first{true};
1360 : : // Iterate over the connected components of this Cluster's m_depgraph.
1361 [ + + ]: 1850 : while (todo.Any()) {
1362 : 414 : auto component = m_depgraph.FindConnectedComponent(todo);
1363 [ + + ]: 414 : auto component_size = component.Count();
1364 [ + + ]: 414 : auto split_quality = component_size <= 1 ? QualityLevel::OPTIMAL : new_quality;
1365 [ + + + + : 630 : if (first && component == todo && SetType::Fill(component_size) == component && component_size >= MIN_INTENDED_TX_COUNT) {
+ + ]
1366 : : // The existing Cluster is an entire component, without holes. Leave it be, but update
1367 : : // its quality. If there are holes, we continue, so that the Cluster is reconstructed
1368 : : // without holes, reducing memory usage. If the component's size is below the intended
1369 : : // transaction count for this Cluster implementation, continue so that it can get
1370 : : // converted.
1371 : 20 : Assume(todo == m_depgraph.Positions());
1372 : 20 : graph.SetClusterQuality(level, m_quality, m_setindex, split_quality);
1373 : : // If this made the quality ACCEPTABLE or OPTIMAL, we need to compute and cache its
1374 : : // chunking.
1375 : 20 : Updated(graph, level);
1376 : 20 : return false;
1377 : : }
1378 : 394 : first = false;
1379 : : // Construct a new Cluster to hold the found component.
1380 : 394 : auto new_cluster = graph.CreateEmptyCluster(component_size);
1381 : 394 : new_clusters.push_back(new_cluster.get());
1382 : : // Remember that all the component's transactions go to this new Cluster. The positions
1383 : : // will be determined below, so use -1 for now.
1384 [ + - + + ]: 1207 : for (auto i : component) {
1385 : 419 : remap[i] = {new_cluster.get(), DepGraphIndex(-1)};
1386 : : }
1387 : 394 : graph.InsertCluster(level, std::move(new_cluster), split_quality);
1388 : 394 : todo -= component;
1389 : 394 : }
1390 : : // We have to split the Cluster up. Remove accounting for the existing one first.
1391 : 1436 : graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1392 : : // Redistribute the transactions.
1393 [ + + ]: 1855 : for (auto i : m_linearization) {
1394 : : /** The cluster which transaction originally in position i is moved to. */
1395 : 419 : Cluster* new_cluster = remap[i].first;
1396 : : // Copy the transaction to the new cluster's depgraph, and remember the position.
1397 : 419 : remap[i].second = new_cluster->AppendTransaction(m_mapping[i], FeePerWeight::FromFeeFrac(m_depgraph.FeeRate(i)));
1398 : : }
1399 : : // Redistribute the dependencies.
1400 [ + + ]: 1855 : for (auto i : m_linearization) {
1401 : : /** The cluster transaction in position i is moved to. */
1402 : 419 : Cluster* new_cluster = remap[i].first;
1403 : : // Copy its parents, translating positions.
1404 : 419 : SetType new_parents;
1405 [ + + + + ]: 467 : for (auto par : m_depgraph.GetReducedParents(i)) new_parents.Set(remap[par].second);
1406 : 419 : new_cluster->AddDependencies(new_parents, remap[i].second);
1407 : : }
1408 : : // Update all the Locators of moved transactions, and memory usage.
1409 [ + + ]: 1830 : for (Cluster* new_cluster : new_clusters) {
1410 : 394 : new_cluster->Updated(graph, level);
1411 : 394 : new_cluster->Compact();
1412 : 394 : graph.GetClusterSet(level).m_cluster_usage += new_cluster->TotalMemoryUsage();
1413 : : }
1414 : : // Wipe this Cluster, and return that it needs to be deleted.
1415 : 1436 : m_depgraph = DepGraph<SetType>{};
1416 [ + + ]: 1436 : m_mapping.clear();
1417 [ + + ]: 1660 : m_linearization.clear();
1418 : : return true;
1419 : 1456 : }
1420 : :
1421 : 41718 : bool SingletonClusterImpl::Split(TxGraphImpl& graph, int level) noexcept
1422 : : {
1423 : 41718 : Assume(NeedsSplitting());
1424 : 41718 : Assume(!GetTxCount());
1425 : 41718 : graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1426 : 41718 : return true;
1427 : : }
1428 : :
1429 : 7574 : void GenericClusterImpl::Merge(TxGraphImpl& graph, int level, Cluster& other) noexcept
1430 : : {
1431 : : /** Vector to store the positions in this Cluster for each position in other. */
1432 : 7574 : std::vector<DepGraphIndex> remap(other.GetDepGraphIndexRange());
1433 : : // Iterate over all transactions in the other Cluster (the one being absorbed).
1434 : 7574 : other.ExtractTransactions([&](DepGraphIndex pos, GraphIndex idx, FeePerWeight feerate) noexcept {
1435 : : // Copy the transaction into this Cluster, and remember its position.
1436 : 7602 : auto new_pos = m_depgraph.AddTransaction(feerate);
1437 : : // Since this cluster must have been made hole-free before being merged into, all added
1438 : : // transactions should appear at the end.
1439 [ - + ]: 7602 : Assume(new_pos == m_mapping.size());
1440 : 7602 : remap[pos] = new_pos;
1441 : 7602 : m_mapping.push_back(idx);
1442 : 7602 : m_linearization.push_back(new_pos);
1443 : 15148 : }, [&](DepGraphIndex pos, GraphIndex idx, SetType other_parents) noexcept {
1444 : : // Copy the transaction's dependencies, translating them using remap.
1445 : 7602 : SetType parents;
1446 [ + + + + ]: 7658 : for (auto par : other_parents) {
1447 : 28 : parents.Set(remap[par]);
1448 : : }
1449 : 7602 : m_depgraph.AddDependencies(parents, remap[pos]);
1450 : : // Update the transaction's Locator. There is no need to call Updated() to update chunk
1451 : : // feerates, as Updated() will be invoked by Cluster::ApplyDependencies on the resulting
1452 : : // merged Cluster later anyway.
1453 [ + + ]: 7602 : auto& entry = graph.m_entries[idx];
1454 : : // Discard any potential ChunkData prior to modifying the Cluster (as that could
1455 : : // invalidate its ordering).
1456 [ + + ]: 7602 : if (level == 0) graph.ClearChunkData(entry);
1457 : 7602 : entry.m_locator[level].SetPresent(this, remap[pos]);
1458 : 7602 : });
1459 : 7574 : }
1460 : :
1461 : 0 : void SingletonClusterImpl::Merge(TxGraphImpl&, int, Cluster&) noexcept
1462 : : {
1463 : : // Nothing can be merged into a singleton; it should have been converted to GenericClusterImpl first.
1464 : 0 : Assume(false);
1465 : 0 : }
1466 : :
1467 : 5449 : void GenericClusterImpl::ApplyDependencies(TxGraphImpl& graph, int level, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept
1468 : : {
1469 : : // Sort the list of dependencies to apply by child, so those can be applied in batch.
1470 [ - - - - : 34132 : std::sort(to_apply.begin(), to_apply.end(), [](auto& a, auto& b) { return a.second < b.second; });
+ + + + +
+ + + + +
+ + + + +
+ - - +
+ ]
1471 : : // Iterate over groups of to-be-added dependencies with the same child.
1472 : 5449 : auto it = to_apply.begin();
1473 [ + + ]: 11256 : while (it != to_apply.end()) {
1474 : 5807 : auto& first_child = graph.m_entries[it->second].m_locator[level];
1475 : 5807 : const auto child_idx = first_child.index;
1476 : : // Iterate over all to-be-added dependencies within that same child, gather the relevant
1477 : : // parents.
1478 : 5807 : SetType parents;
1479 [ + + ]: 15976 : while (it != to_apply.end()) {
1480 [ + + ]: 10527 : auto& child = graph.m_entries[it->second].m_locator[level];
1481 : 10527 : auto& parent = graph.m_entries[it->first].m_locator[level];
1482 [ + + ]: 10527 : Assume(child.cluster == this && parent.cluster == this);
1483 [ + + ]: 10527 : if (child.index != child_idx) break;
1484 : 10169 : parents.Set(parent.index);
1485 : 10169 : ++it;
1486 : : }
1487 : : // Push all dependencies to the underlying DepGraph. Note that this is O(N) in the size of
1488 : : // the cluster, regardless of the number of parents being added, so batching them together
1489 : : // has a performance benefit.
1490 : 5807 : m_depgraph.AddDependencies(parents, child_idx);
1491 : : }
1492 : :
1493 : : // Finally set the cluster to NEEDS_FIX, so its linearization is fixed the next time it is
1494 : : // attempted to be made ACCEPTABLE.
1495 : 5449 : Assume(!NeedsSplitting());
1496 : 5449 : Assume(!IsOversized());
1497 : 5449 : graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::NEEDS_FIX);
1498 : :
1499 : : // Finally push the changes to graph.m_entries.
1500 : 5449 : Updated(graph, level);
1501 : 5449 : }
1502 : :
1503 : 0 : void SingletonClusterImpl::ApplyDependencies(TxGraphImpl&, int, std::span<std::pair<GraphIndex, GraphIndex>>) noexcept
1504 : : {
1505 : : // Nothing can actually be applied.
1506 : 0 : Assume(false);
1507 : 0 : }
1508 : :
1509 : 2506 : TxGraphImpl::~TxGraphImpl() noexcept
1510 : : {
1511 : : // If Refs outlive the TxGraphImpl they refer to, unlink them, so that their destructor does not
1512 : : // try to reach into a non-existing TxGraphImpl anymore.
1513 [ + + ]: 69091 : for (auto& entry : m_entries) {
1514 [ + + ]: 67838 : if (entry.m_ref != nullptr) {
1515 : 64011 : GetRefGraph(*entry.m_ref) = nullptr;
1516 : : }
1517 : : }
1518 : 2506 : }
1519 : :
1520 : 155899 : std::unique_ptr<Cluster> TxGraphImpl::ExtractCluster(int level, QualityLevel quality, ClusterSetIndex setindex) noexcept
1521 : : {
1522 [ - + ]: 155899 : Assume(quality != QualityLevel::NONE);
1523 : :
1524 : 155899 : auto& clusterset = GetClusterSet(level);
1525 [ - + ]: 155899 : auto& quality_clusters = clusterset.m_clusters[int(quality)];
1526 [ - + - + ]: 155899 : Assume(setindex < quality_clusters.size());
1527 : :
1528 : : // Extract the Cluster-owning unique_ptr.
1529 [ - + ]: 155899 : std::unique_ptr<Cluster> ret = std::move(quality_clusters[setindex]);
1530 [ - + ]: 155899 : ret->m_quality = QualityLevel::NONE;
1531 : 155899 : ret->m_setindex = ClusterSetIndex(-1);
1532 : :
1533 : : // Clean up space in quality_cluster.
1534 [ - + ]: 155899 : auto max_setindex = quality_clusters.size() - 1;
1535 [ + + ]: 155899 : if (setindex != max_setindex) {
1536 : : // If the cluster was not the last element of quality_clusters, move that to take its place.
1537 : 33372 : quality_clusters.back()->m_setindex = setindex;
1538 : 33372 : quality_clusters[setindex] = std::move(quality_clusters.back());
1539 : : }
1540 : : // The last element of quality_clusters is now unused; drop it.
1541 : 155899 : quality_clusters.pop_back();
1542 : :
1543 : 155899 : return ret;
1544 : : }
1545 : :
1546 : 232922 : ClusterSetIndex TxGraphImpl::InsertCluster(int level, std::unique_ptr<Cluster>&& cluster, QualityLevel quality) noexcept
1547 : : {
1548 : : // Cannot insert with quality level NONE (as that would mean not inserted).
1549 [ - + ]: 232922 : Assume(quality != QualityLevel::NONE);
1550 : : // The passed-in Cluster must not currently be in the TxGraphImpl.
1551 [ - + ]: 232922 : Assume(cluster->m_quality == QualityLevel::NONE);
1552 : :
1553 : : // Append it at the end of the relevant TxGraphImpl::m_cluster.
1554 : 232922 : auto& clusterset = GetClusterSet(level);
1555 [ - + ]: 232922 : auto& quality_clusters = clusterset.m_clusters[int(quality)];
1556 [ - + ]: 232922 : ClusterSetIndex ret = quality_clusters.size();
1557 : 232922 : cluster->m_quality = quality;
1558 : 232922 : cluster->m_setindex = ret;
1559 : 232922 : quality_clusters.push_back(std::move(cluster));
1560 : 232922 : return ret;
1561 : : }
1562 : :
1563 : 54221 : void TxGraphImpl::SetClusterQuality(int level, QualityLevel old_quality, ClusterSetIndex old_index, QualityLevel new_quality) noexcept
1564 : : {
1565 [ + - ]: 54221 : Assume(new_quality != QualityLevel::NONE);
1566 : :
1567 : : // Don't do anything if the quality did not change.
1568 [ + - ]: 54221 : if (old_quality == new_quality) return;
1569 : : // Extract the cluster from where it currently resides.
1570 : 54221 : auto cluster_ptr = ExtractCluster(level, old_quality, old_index);
1571 : : // And re-insert it where it belongs.
1572 : 54221 : InsertCluster(level, std::move(cluster_ptr), new_quality);
1573 : 54221 : }
1574 : :
1575 : 51946 : void TxGraphImpl::DeleteCluster(Cluster& cluster, int level) noexcept
1576 : : {
1577 : : // Extract the cluster from where it currently resides.
1578 : 51946 : auto cluster_ptr = ExtractCluster(level, cluster.m_quality, cluster.m_setindex);
1579 : : // And throw it away.
1580 [ + - ]: 51946 : cluster_ptr.reset();
1581 : 51946 : }
1582 : :
1583 : 1374644 : std::pair<Cluster*, int> TxGraphImpl::FindClusterAndLevel(GraphIndex idx, int level) const noexcept
1584 : : {
1585 [ + - ]: 1374644 : Assume(level >= 0 && level <= GetTopLevel());
1586 : 1374644 : auto& entry = m_entries[idx];
1587 : : // Search the entry's locators from top to bottom.
1588 [ + + ]: 1408705 : for (int l = level; l >= 0; --l) {
1589 : : // If the locator is missing, dig deeper; it may exist at a lower level and therefore be
1590 : : // implicitly existing at this level too.
1591 [ + + ]: 1441495 : if (entry.m_locator[l].IsMissing()) continue;
1592 : : // If the locator has the entry marked as explicitly removed, stop.
1593 [ + + ]: 1373373 : if (entry.m_locator[l].IsRemoved()) break;
1594 : : // Otherwise, we have found the topmost ClusterSet that contains this entry.
1595 : 1373368 : return {entry.m_locator[l].cluster, l};
1596 : : }
1597 : : // If no non-empty locator was found, or an explicitly removed was hit, return nothing.
1598 : 1276 : return {nullptr, -1};
1599 : : }
1600 : :
1601 : 2423 : Cluster* TxGraphImpl::PullIn(Cluster* cluster, int level) noexcept
1602 : : {
1603 [ + + ]: 2423 : int to_level = GetTopLevel();
1604 [ + + ]: 2423 : Assume(to_level == 1);
1605 : 2423 : Assume(level <= to_level);
1606 : : // Copy the Cluster from main to staging, if it's not already there.
1607 [ + + ]: 2423 : if (level == 0) {
1608 : : // Make the Cluster Acceptable before copying. This isn't strictly necessary, but doing it
1609 : : // now avoids doing double work later.
1610 : 1697 : MakeAcceptable(*cluster, level);
1611 : 1697 : cluster = cluster->CopyToStaging(*this);
1612 : : }
1613 : 2423 : return cluster;
1614 : : }
1615 : :
1616 : 654227 : void TxGraphImpl::ApplyRemovals(int up_to_level) noexcept
1617 : : {
1618 [ + - ]: 654227 : Assume(up_to_level >= 0 && up_to_level <= GetTopLevel());
1619 [ + + ]: 1325201 : for (int level = 0; level <= up_to_level; ++level) {
1620 : 670974 : auto& clusterset = GetClusterSet(level);
1621 : 670974 : auto& to_remove = clusterset.m_to_remove;
1622 : : // Skip if there is nothing to remove in this level.
1623 [ + + ]: 670974 : if (to_remove.empty()) continue;
1624 : : // Pull in all Clusters that are not in staging.
1625 [ + + ]: 3931 : if (level == 1) {
1626 [ + + ]: 3424 : for (GraphIndex index : to_remove) {
1627 [ + - ]: 2124 : auto [cluster, cluster_level] = FindClusterAndLevel(index, level);
1628 [ + - ]: 2124 : if (cluster != nullptr) PullIn(cluster, cluster_level);
1629 : : }
1630 : : }
1631 : : // Group the set of to-be-removed entries by Cluster::m_sequence.
1632 : 3931 : std::sort(to_remove.begin(), to_remove.end(), [&](GraphIndex a, GraphIndex b) noexcept {
1633 : 243456 : Cluster* cluster_a = m_entries[a].m_locator[level].cluster;
1634 : 243456 : Cluster* cluster_b = m_entries[b].m_locator[level].cluster;
1635 : 243456 : return CompareClusters(cluster_a, cluster_b) < 0;
1636 : : });
1637 : : // Process per Cluster.
1638 [ - + ]: 3931 : std::span to_remove_span{to_remove};
1639 [ + + ]: 47124 : while (!to_remove_span.empty()) {
1640 [ + - ]: 43193 : Cluster* cluster = m_entries[to_remove_span.front()].m_locator[level].cluster;
1641 [ + - ]: 43193 : if (cluster != nullptr) {
1642 : : // If the first to_remove_span entry's Cluster exists, hand to_remove_span to it, so it
1643 : : // can pop off whatever applies to it.
1644 : 43193 : cluster->ApplyRemovals(*this, level, to_remove_span);
1645 : : } else {
1646 : : // Otherwise, skip this already-removed entry. This may happen when
1647 : : // RemoveTransaction was called twice on the same Ref, for example.
1648 : 0 : to_remove_span = to_remove_span.subspan(1);
1649 : : }
1650 : : }
1651 [ + - ]: 674905 : to_remove.clear();
1652 : : }
1653 : 654227 : Compact();
1654 : 654227 : }
1655 : :
1656 : 17137 : void TxGraphImpl::SwapIndexes(GraphIndex a, GraphIndex b) noexcept
1657 : : {
1658 [ - + ]: 17137 : Assume(a < m_entries.size());
1659 : 17137 : Assume(b < m_entries.size());
1660 : : // Swap the Entry objects.
1661 : 17137 : std::swap(m_entries[a], m_entries[b]);
1662 : : // Iterate over both objects.
1663 [ + + ]: 51411 : for (int i = 0; i < 2; ++i) {
1664 [ + + ]: 34274 : GraphIndex idx = i ? b : a;
1665 [ + + ]: 34274 : Entry& entry = m_entries[idx];
1666 : : // Update linked Ref, if any exists.
1667 [ + + ]: 34274 : if (entry.m_ref) GetRefIndex(*entry.m_ref) = idx;
1668 : : // Update linked chunk index entries, if any exist.
1669 [ + + ]: 34274 : if (entry.m_main_chunkindex_iterator != m_main_chunkindex.end()) {
1670 : 15940 : entry.m_main_chunkindex_iterator->m_graph_index = idx;
1671 : : }
1672 : : // Update the locators for both levels. The rest of the Entry information will not change,
1673 : : // so no need to invoke Cluster::Updated().
1674 [ + + ]: 102822 : for (int level = 0; level < MAX_LEVELS; ++level) {
1675 : 68548 : Locator& locator = entry.m_locator[level];
1676 [ + + ]: 68548 : if (locator.IsPresent()) {
1677 : 16272 : locator.cluster->UpdateMapping(locator.index, idx);
1678 : : }
1679 : : }
1680 : : }
1681 : 17137 : }
1682 : :
1683 : 743410 : void TxGraphImpl::Compact() noexcept
1684 : : {
1685 : : // We cannot compact while any to-be-applied operations or staged removals remain as we'd need
1686 : : // to rewrite them. It is easier to delay the compaction until they have been applied.
1687 [ + + ]: 743410 : if (!m_main_clusterset.m_deps_to_add.empty()) return;
1688 [ + + ]: 737997 : if (!m_main_clusterset.m_to_remove.empty()) return;
1689 [ + + ]: 737992 : Assume(m_main_clusterset.m_removed.empty()); // non-staging m_removed is always empty
1690 [ + + ]: 737992 : if (m_staging_clusterset.has_value()) {
1691 [ + + ]: 24955 : if (!m_staging_clusterset->m_deps_to_add.empty()) return;
1692 [ + - ]: 3818 : if (!m_staging_clusterset->m_to_remove.empty()) return;
1693 [ - + ]: 3818 : if (!m_staging_clusterset->m_removed.empty()) return;
1694 : : }
1695 : :
1696 : : // Release memory used by discarded ChunkData index entries.
1697 : 713037 : ClearShrink(m_main_chunkindex_discarded);
1698 : :
1699 : : // Sort the GraphIndexes that need to be cleaned up. They are sorted in reverse, so the last
1700 : : // ones get processed first. This means earlier-processed GraphIndexes will not cause moving of
1701 : : // later-processed ones during the "swap with end of m_entries" step below (which might
1702 : : // invalidate them).
1703 : 713037 : std::sort(m_unlinked.begin(), m_unlinked.end(), std::greater{});
1704 : :
1705 : 713037 : auto last = GraphIndex(-1);
1706 [ + + ]: 770473 : for (GraphIndex idx : m_unlinked) {
1707 : : // m_unlinked should never contain the same GraphIndex twice (the code below would fail
1708 : : // if so, because GraphIndexes get invalidated by removing them).
1709 : 57436 : Assume(idx != last);
1710 : 57436 : last = idx;
1711 : :
1712 : : // Make sure the entry is unlinked.
1713 : 57436 : Entry& entry = m_entries[idx];
1714 : 57436 : Assume(entry.m_ref == nullptr);
1715 : : // Make sure the entry does not occur in the graph.
1716 [ + + ]: 172308 : for (int level = 0; level < MAX_LEVELS; ++level) {
1717 : 114872 : Assume(!entry.m_locator[level].IsPresent());
1718 : : }
1719 : :
1720 : : // Move the entry to the end.
1721 [ - + + + ]: 57436 : if (idx != m_entries.size() - 1) SwapIndexes(idx, m_entries.size() - 1);
1722 : : // Drop the entry for idx, now that it is at the end.
1723 : 57436 : m_entries.pop_back();
1724 : : }
1725 [ + + ]: 713037 : m_unlinked.clear();
1726 : : }
1727 : :
1728 : 43174 : void TxGraphImpl::Split(Cluster& cluster, int level) noexcept
1729 : : {
1730 : : // To split a Cluster, first make sure all removals are applied (as we might need to split
1731 : : // again afterwards otherwise).
1732 : 43174 : ApplyRemovals(level);
1733 : 43174 : bool del = cluster.Split(*this, level);
1734 [ + + ]: 43174 : if (del) {
1735 : : // Cluster::Split reports whether the Cluster is to be deleted.
1736 : 43154 : DeleteCluster(cluster, level);
1737 : : }
1738 : 43174 : }
1739 : :
1740 : 603821 : void TxGraphImpl::SplitAll(int up_to_level) noexcept
1741 : : {
1742 [ + - ]: 603821 : Assume(up_to_level >= 0 && up_to_level <= GetTopLevel());
1743 : : // Before splitting all Cluster, first make sure all removals are applied.
1744 : 603821 : ApplyRemovals(up_to_level);
1745 [ + + ]: 1215774 : for (int level = 0; level <= up_to_level; ++level) {
1746 [ + + ]: 1835859 : for (auto quality : {QualityLevel::NEEDS_SPLIT_FIX, QualityLevel::NEEDS_SPLIT}) {
1747 : 1223906 : auto& queue = GetClusterSet(level).m_clusters[int(quality)];
1748 [ + + ]: 1267080 : while (!queue.empty()) {
1749 : 43174 : Split(*queue.back().get(), level);
1750 : : }
1751 : : }
1752 : : }
1753 : 603821 : }
1754 : :
1755 : 89020301 : void TxGraphImpl::GroupClusters(int level) noexcept
1756 : : {
1757 : 89020301 : auto& clusterset = GetClusterSet(level);
1758 : : // If the groupings have been computed already, nothing is left to be done.
1759 [ + + ]: 89020301 : if (clusterset.m_group_data.has_value()) return;
1760 : :
1761 : : // Before computing which Clusters need to be merged together, first apply all removals and
1762 : : // split the Clusters into connected components. If we would group first, we might end up
1763 : : // with inefficient and/or oversized Clusters which just end up being split again anyway.
1764 : 10786 : SplitAll(level);
1765 : :
1766 : : /** Annotated clusters: an entry for each Cluster, together with the sequence number for the
1767 : : * representative for the partition it is in (initially its own, later that of the
1768 : : * to-be-merged group). */
1769 : 10786 : std::vector<std::pair<Cluster*, uint64_t>> an_clusters;
1770 : : /** Annotated dependencies: an entry for each m_deps_to_add entry (excluding ones that apply
1771 : : * to removed transactions), together with the sequence number of the representative root of
1772 : : * Clusters it applies to (initially that of the child Cluster, later that of the
1773 : : * to-be-merged group). */
1774 : 10786 : std::vector<std::pair<std::pair<GraphIndex, GraphIndex>, uint64_t>> an_deps;
1775 : :
1776 : : // Construct an an_clusters entry for every oversized cluster, including ones from levels below,
1777 : : // as they may be inherited in this one.
1778 [ + + ]: 29704 : for (int level_iter = 0; level_iter <= level; ++level_iter) {
1779 [ + + ]: 18924 : for (auto& cluster : GetClusterSet(level_iter).m_clusters[int(QualityLevel::OVERSIZED_SINGLETON)]) {
1780 : 6 : auto graph_idx = cluster->GetClusterEntry(0);
1781 : 6 : auto cur_cluster = FindCluster(graph_idx, level);
1782 [ - + ]: 6 : if (cur_cluster == nullptr) continue;
1783 : 6 : an_clusters.emplace_back(cur_cluster, cur_cluster->m_sequence);
1784 : : }
1785 : : }
1786 : :
1787 : : // Construct a an_clusters entry for every parent and child in the to-be-applied dependencies,
1788 : : // and an an_deps entry for each dependency to be applied.
1789 [ - + ]: 10786 : an_deps.reserve(clusterset.m_deps_to_add.size());
1790 [ + + + + ]: 150836 : for (const auto& [par, chl] : clusterset.m_deps_to_add) {
1791 : 140050 : auto par_cluster = FindCluster(par, level);
1792 : 140050 : auto chl_cluster = FindCluster(chl, level);
1793 : : // Skip dependencies for which the parent or child transaction is removed.
1794 [ + + - + ]: 140050 : if (par_cluster == nullptr || chl_cluster == nullptr) continue;
1795 : 140045 : an_clusters.emplace_back(par_cluster, par_cluster->m_sequence);
1796 : : // Do not include a duplicate when parent and child are identical, as it'll be removed
1797 : : // below anyway.
1798 [ + + ]: 140045 : if (chl_cluster != par_cluster) an_clusters.emplace_back(chl_cluster, chl_cluster->m_sequence);
1799 : : // Add entry to an_deps, using the child sequence number.
1800 : 140045 : an_deps.emplace_back(std::pair{par, chl}, chl_cluster->m_sequence);
1801 : : }
1802 : : // Sort and deduplicate an_clusters, so we end up with a sorted list of all involved Clusters
1803 : : // to which dependencies apply, or which are oversized.
1804 [ + + + + : 5519722 : std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
+ + + + +
+ + + + +
+ + + + +
+ - - +
+ ]
1805 : 10786 : an_clusters.erase(std::unique(an_clusters.begin(), an_clusters.end()), an_clusters.end());
1806 : : // Sort an_deps by applying the same order to the involved child cluster.
1807 [ - - - - : 2352195 : std::sort(an_deps.begin(), an_deps.end(), [&](auto& a, auto& b) noexcept { return a.second < b.second; });
+ + + + +
+ + + + +
+ + + + +
+ - - +
+ ]
1808 : :
1809 : : // Run the union-find algorithm to find partitions of the input Clusters which need to be
1810 : : // grouped together. See https://en.wikipedia.org/wiki/Disjoint-set_data_structure.
1811 : 10786 : {
1812 : : /** Each PartitionData entry contains information about a single input Cluster. */
1813 : 10786 : struct PartitionData
1814 : : {
1815 : : /** The sequence number of the cluster this holds information for. */
1816 : : uint64_t sequence;
1817 : : /** All PartitionData entries belonging to the same partition are organized in a tree.
1818 : : * Each element points to its parent, or to itself if it is the root. The root is then
1819 : : * a representative for the entire tree, and can be found by walking upwards from any
1820 : : * element. */
1821 : : PartitionData* parent;
1822 : : /** (only if this is a root, so when parent == this) An upper bound on the height of
1823 : : * tree for this partition. */
1824 : : unsigned rank;
1825 : : };
1826 : : /** Information about each input Cluster. Sorted by Cluster::m_sequence. */
1827 : 10786 : std::vector<PartitionData> partition_data;
1828 : :
1829 : : /** Given a Cluster, find its corresponding PartitionData. */
1830 : 282596 : auto locate_fn = [&](uint64_t sequence) noexcept -> PartitionData* {
1831 : 271810 : auto it = std::lower_bound(partition_data.begin(), partition_data.end(), sequence,
1832 [ + + ]: 4088363 : [](auto& a, uint64_t seq) noexcept { return a.sequence < seq; });
1833 : 271810 : Assume(it != partition_data.end());
1834 : 271810 : Assume(it->sequence == sequence);
1835 : 271810 : return &*it;
1836 : 10786 : };
1837 : :
1838 : : /** Given a PartitionData, find the root of the tree it is in (its representative). */
1839 : 293245 : static constexpr auto find_root_fn = [](PartitionData* data) noexcept -> PartitionData* {
1840 [ + + + + ]: 483323 : while (data->parent != data) {
1841 : : // Replace pointers to parents with pointers to grandparents.
1842 : : // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Finding_set_representatives.
1843 : 322864 : auto par = data->parent;
1844 : 322864 : data->parent = par->parent;
1845 : 322864 : data = par;
1846 : : }
1847 : 282459 : return data;
1848 : : };
1849 : :
1850 : : /** Given two PartitionDatas, union the partitions they are in, and return their
1851 : : * representative. */
1852 : 149299 : static constexpr auto union_fn = [](PartitionData* arg1, PartitionData* arg2) noexcept {
1853 : : // Find the roots of the trees, and bail out if they are already equal (which would
1854 : : // mean they are in the same partition already).
1855 [ + + ]: 399026 : auto rep1 = find_root_fn(arg1);
1856 : 138513 : auto rep2 = find_root_fn(arg2);
1857 [ + + ]: 138513 : if (rep1 == rep2) return rep1;
1858 : : // Pick the lower-rank root to become a child of the higher-rank one.
1859 : : // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Union_by_rank.
1860 [ + + ]: 135791 : if (rep1->rank < rep2->rank) std::swap(rep1, rep2);
1861 : 135791 : rep2->parent = rep1;
1862 : 135791 : rep1->rank += (rep1->rank == rep2->rank);
1863 : 135791 : return rep1;
1864 : : };
1865 : :
1866 : : // Start by initializing every Cluster as its own singleton partition.
1867 [ - + ]: 10786 : partition_data.resize(an_clusters.size());
1868 [ - + + + ]: 154732 : for (size_t i = 0; i < an_clusters.size(); ++i) {
1869 : 143946 : partition_data[i].sequence = an_clusters[i].first->m_sequence;
1870 : 143946 : partition_data[i].parent = &partition_data[i];
1871 : 143946 : partition_data[i].rank = 0;
1872 : : }
1873 : :
1874 : : // Run through all parent/child pairs in an_deps, and union the partitions their Clusters
1875 : : // are in.
1876 : 10786 : Cluster* last_chl_cluster{nullptr};
1877 : 10786 : PartitionData* last_partition{nullptr};
1878 [ + + + + ]: 150831 : for (const auto& [dep, _] : an_deps) {
1879 [ + + ]: 140045 : auto [par, chl] = dep;
1880 : 140045 : auto par_cluster = FindCluster(par, level);
1881 : 140045 : auto chl_cluster = FindCluster(chl, level);
1882 [ + + ]: 140045 : Assume(chl_cluster != nullptr && par_cluster != nullptr);
1883 : : // Nothing to do if parent and child are in the same Cluster.
1884 [ + + ]: 140045 : if (par_cluster == chl_cluster) continue;
1885 [ + + ]: 138513 : Assume(par != chl);
1886 [ + + ]: 138513 : if (chl_cluster == last_chl_cluster) {
1887 : : // If the child Clusters is the same as the previous iteration, union with the
1888 : : // tree they were in, avoiding the need for another lookup. Note that an_deps
1889 : : // is sorted by child Cluster, so batches with the same child are expected.
1890 : 5216 : last_partition = union_fn(locate_fn(par_cluster->m_sequence), last_partition);
1891 : : } else {
1892 : 133297 : last_chl_cluster = chl_cluster;
1893 : 133297 : last_partition = union_fn(locate_fn(par_cluster->m_sequence), locate_fn(chl_cluster->m_sequence));
1894 : : }
1895 : : }
1896 : :
1897 : : // Update the sequence numbers in an_clusters and an_deps to be those of the partition
1898 : : // representative.
1899 : 10786 : auto deps_it = an_deps.begin();
1900 [ - + + + ]: 154732 : for (size_t i = 0; i < partition_data.size(); ++i) {
1901 : 143946 : auto& data = partition_data[i];
1902 : : // Find the sequence of the representative of the partition Cluster i is in, and store
1903 : : // it with the Cluster.
1904 : 143946 : auto rep_seq = find_root_fn(&data)->sequence;
1905 : 143946 : an_clusters[i].second = rep_seq;
1906 : : // Find all dependencies whose child Cluster is Cluster i, and annotate them with rep.
1907 [ + + ]: 283991 : while (deps_it != an_deps.end()) {
1908 [ + + ]: 276477 : auto [par, chl] = deps_it->first;
1909 : 276477 : auto chl_cluster = FindCluster(chl, level);
1910 [ + + ]: 276477 : Assume(chl_cluster != nullptr);
1911 [ + + ]: 276477 : if (chl_cluster->m_sequence > data.sequence) break;
1912 : 140045 : deps_it->second = rep_seq;
1913 : 140045 : ++deps_it;
1914 : : }
1915 : : }
1916 : 10786 : }
1917 : :
1918 : : // Sort both an_clusters and an_deps by sequence number of the representative of the
1919 : : // partition they are in, grouping all those applying to the same partition together.
1920 [ - - - - : 1805923 : std::sort(an_deps.begin(), an_deps.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
+ + + + +
+ + + + +
+ + + + +
+ - - +
+ ]
1921 [ + + + + : 1798283 : std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
+ + + + +
+ + + + +
+ + + + +
+ - - +
+ ]
1922 : :
1923 : : // Translate the resulting cluster groups to the m_group_data structure, and the dependencies
1924 : : // back to m_deps_to_add.
1925 : 10786 : clusterset.m_group_data = GroupData{};
1926 [ - + ]: 10786 : clusterset.m_group_data->m_group_clusters.reserve(an_clusters.size());
1927 [ + + ]: 10786 : clusterset.m_deps_to_add.clear();
1928 [ - + ]: 10786 : clusterset.m_deps_to_add.reserve(an_deps.size());
1929 : 10786 : clusterset.m_oversized = false;
1930 : 10786 : auto an_deps_it = an_deps.begin();
1931 : 10786 : auto an_clusters_it = an_clusters.begin();
1932 [ + + ]: 18941 : while (an_clusters_it != an_clusters.end()) {
1933 : : // Process all clusters/dependencies belonging to the partition with representative rep.
1934 : 8155 : auto rep = an_clusters_it->second;
1935 : : // Create and initialize a new GroupData entry for the partition.
1936 : 8155 : auto& new_entry = clusterset.m_group_data->m_groups.emplace_back();
1937 [ - + ]: 8155 : new_entry.m_cluster_offset = clusterset.m_group_data->m_group_clusters.size();
1938 : 8155 : new_entry.m_cluster_count = 0;
1939 [ - + ]: 8155 : new_entry.m_deps_offset = clusterset.m_deps_to_add.size();
1940 : 8155 : new_entry.m_deps_count = 0;
1941 : 8155 : uint32_t total_count{0};
1942 : 8155 : uint64_t total_size{0};
1943 : : // Add all its clusters to it (copying those from an_clusters to m_group_clusters).
1944 [ + + + + ]: 152101 : while (an_clusters_it != an_clusters.end() && an_clusters_it->second == rep) {
1945 : 143946 : clusterset.m_group_data->m_group_clusters.push_back(an_clusters_it->first);
1946 : 143946 : total_count += an_clusters_it->first->GetTxCount();
1947 : 143946 : total_size += an_clusters_it->first->GetTotalTxSize();
1948 : 143946 : ++an_clusters_it;
1949 : 143946 : ++new_entry.m_cluster_count;
1950 : : }
1951 : : // Add all its dependencies to it (copying those back from an_deps to m_deps_to_add).
1952 [ + + + + ]: 148200 : while (an_deps_it != an_deps.end() && an_deps_it->second == rep) {
1953 : 140045 : clusterset.m_deps_to_add.push_back(an_deps_it->first);
1954 : 140045 : ++an_deps_it;
1955 : 140045 : ++new_entry.m_deps_count;
1956 : : }
1957 : : // Detect oversizedness.
1958 [ + + + + ]: 8155 : if (total_count > m_max_cluster_count || total_size > m_max_cluster_size) {
1959 : 149 : clusterset.m_oversized = true;
1960 : : }
1961 : : }
1962 : 10786 : Assume(an_deps_it == an_deps.end());
1963 : 10786 : Assume(an_clusters_it == an_clusters.end());
1964 : 10786 : Compact();
1965 : 10786 : }
1966 : :
1967 : 5449 : void TxGraphImpl::Merge(std::span<Cluster*> to_merge, int level) noexcept
1968 : : {
1969 [ + + ]: 5449 : Assume(!to_merge.empty());
1970 : : // Nothing to do if a group consists of just a single Cluster.
1971 [ + + ]: 5449 : if (to_merge.size() == 1) return;
1972 : :
1973 : : // Move the largest Cluster to the front of to_merge. As all transactions in other to-be-merged
1974 : : // Clusters will be moved to that one, putting the largest one first minimizes the number of
1975 : : // moves.
1976 : 5437 : size_t max_size_pos{0};
1977 : 5437 : DepGraphIndex max_size = to_merge[max_size_pos]->GetTxCount();
1978 : 5437 : GetClusterSet(level).m_cluster_usage -= to_merge[max_size_pos]->TotalMemoryUsage();
1979 : 5437 : DepGraphIndex total_size = max_size;
1980 [ + + ]: 11407 : for (size_t i = 1; i < to_merge.size(); ++i) {
1981 : 5970 : GetClusterSet(level).m_cluster_usage -= to_merge[i]->TotalMemoryUsage();
1982 : 5970 : DepGraphIndex size = to_merge[i]->GetTxCount();
1983 : 5970 : total_size += size;
1984 [ + + ]: 5970 : if (size > max_size) {
1985 : 56 : max_size_pos = i;
1986 : 56 : max_size = size;
1987 : : }
1988 : : }
1989 [ + + ]: 5437 : if (max_size_pos != 0) std::swap(to_merge[0], to_merge[max_size_pos]);
1990 : :
1991 : 5437 : size_t start_idx = 1;
1992 : 5437 : Cluster* into_cluster = to_merge[0];
1993 [ + + ]: 5437 : if (total_size > into_cluster->GetMaxTxCount()) {
1994 : : // The into_merge cluster is too small to fit all transactions being merged. Construct a
1995 : : // a new Cluster using an implementation that matches the total size, and merge everything
1996 : : // in there.
1997 : 1604 : auto new_cluster = CreateEmptyCluster(total_size);
1998 : 1604 : into_cluster = new_cluster.get();
1999 : 1604 : InsertCluster(level, std::move(new_cluster), QualityLevel::OPTIMAL);
2000 : 1604 : start_idx = 0;
2001 : 1604 : }
2002 : :
2003 : : // Merge all further Clusters in the group into the result (first one, or new one), and delete
2004 : : // them.
2005 [ + + ]: 13011 : for (size_t i = start_idx; i < to_merge.size(); ++i) {
2006 : 7574 : into_cluster->Merge(*this, level, *to_merge[i]);
2007 : 7574 : DeleteCluster(*to_merge[i], level);
2008 : : }
2009 : 5437 : into_cluster->Compact();
2010 : 5437 : GetClusterSet(level).m_cluster_usage += into_cluster->TotalMemoryUsage();
2011 : : }
2012 : :
2013 : 89013349 : void TxGraphImpl::ApplyDependencies(int level) noexcept
2014 : : {
2015 : 89013349 : auto& clusterset = GetClusterSet(level);
2016 : : // Do not bother computing groups if we already know the result will be oversized.
2017 [ + - ]: 89013349 : if (clusterset.m_oversized == true) return;
2018 : : // Compute the groups of to-be-merged Clusters (which also applies all removals, and splits).
2019 : 89013349 : GroupClusters(level);
2020 [ + + ]: 89013349 : Assume(clusterset.m_group_data.has_value());
2021 : : // Nothing to do if there are no dependencies to be added.
2022 [ + + ]: 89013349 : if (clusterset.m_deps_to_add.empty()) return;
2023 : : // Dependencies cannot be applied if it would result in oversized clusters.
2024 [ + - ]: 5245 : if (clusterset.m_oversized == true) return;
2025 : :
2026 : : // For each group of to-be-merged Clusters.
2027 [ + + ]: 10694 : for (const auto& group_entry : clusterset.m_group_data->m_groups) {
2028 [ - + ]: 5449 : auto cluster_span = std::span{clusterset.m_group_data->m_group_clusters}
2029 [ + + ]: 5449 : .subspan(group_entry.m_cluster_offset, group_entry.m_cluster_count);
2030 : : // Pull in all the Clusters that contain dependencies.
2031 [ + + ]: 5449 : if (level == 1) {
2032 [ + + ]: 375 : for (Cluster*& cluster : cluster_span) {
2033 : 299 : cluster = PullIn(cluster, cluster->GetLevel(*this));
2034 : : }
2035 : : }
2036 : : // Invoke Merge() to merge them into a single Cluster.
2037 : 5449 : Merge(cluster_span, level);
2038 : : // Actually apply all to-be-added dependencies (all parents and children from this grouping
2039 : : // belong to the same Cluster at this point because of the merging above).
2040 [ - + ]: 5449 : auto deps_span = std::span{clusterset.m_deps_to_add}
2041 : 5449 : .subspan(group_entry.m_deps_offset, group_entry.m_deps_count);
2042 : 5449 : Assume(!deps_span.empty());
2043 : 5449 : const auto& loc = m_entries[deps_span[0].second].m_locator[level];
2044 : 5449 : Assume(loc.IsPresent());
2045 : 5449 : loc.cluster->ApplyDependencies(*this, level, deps_span);
2046 : : }
2047 : :
2048 : : // Wipe the list of to-be-added dependencies now that they are applied.
2049 [ + - ]: 5245 : clusterset.m_deps_to_add.clear();
2050 : 5245 : Compact();
2051 : : // Also no further Cluster mergings are needed (note that we clear, but don't set to
2052 : : // std::nullopt, as that would imply the groupings are unknown).
2053 : 10490 : clusterset.m_group_data = GroupData{};
2054 : : }
2055 : :
2056 : 5547 : std::pair<uint64_t, bool> GenericClusterImpl::Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept
2057 : : {
2058 : : // We can only relinearize Clusters that do not need splitting.
2059 [ - + ]: 5547 : Assume(!NeedsSplitting());
2060 : : // No work is required for Clusters which are already optimally linearized.
2061 [ - + ]: 5547 : if (IsOptimal()) return {0, false};
2062 : : // Invoke the actual linearization algorithm (passing in the existing one).
2063 : 5547 : uint64_t rng_seed = graph.m_rng.rand64();
2064 [ - + - + ]: 5547 : auto [linearization, optimal, cost] = Linearize(m_depgraph, max_iters, rng_seed, m_linearization, /*is_topological=*/IsTopological());
2065 : : // Postlinearize to undo some of the non-determinism caused by randomizing the linearization.
2066 : : // This also guarantees that all chunks are connected (which is not guaranteed by SFL
2067 : : // currently, even when optimal).
2068 [ - + ]: 5547 : PostLinearize(m_depgraph, linearization);
2069 : : // Update the linearization.
2070 : 5547 : m_linearization = std::move(linearization);
2071 : : // Update the Cluster's quality.
2072 : 5547 : bool improved = false;
2073 [ + + ]: 5547 : if (optimal) {
2074 : 5464 : graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::OPTIMAL);
2075 : 5464 : improved = true;
2076 [ + - + - ]: 83 : } else if (max_iters >= graph.m_acceptable_iters && !IsAcceptable()) {
2077 : 83 : graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::ACCEPTABLE);
2078 : 83 : improved = true;
2079 [ # # ]: 0 : } else if (!IsTopological()) {
2080 : 0 : graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::NEEDS_RELINEARIZE);
2081 : 0 : improved = true;
2082 : : }
2083 : : // Update the Entry objects.
2084 : 5547 : Updated(graph, level);
2085 : 5547 : return {cost, improved};
2086 : 5547 : }
2087 : :
2088 : 0 : std::pair<uint64_t, bool> SingletonClusterImpl::Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept
2089 : : {
2090 : : // All singletons are optimal, oversized, or need splitting. Each of these precludes
2091 : : // Relinearize from being called.
2092 : 0 : assert(false);
2093 : : return {0, false};
2094 : : }
2095 : :
2096 : 174762544 : void TxGraphImpl::MakeAcceptable(Cluster& cluster, int level) noexcept
2097 : : {
2098 : : // Relinearize the Cluster if needed.
2099 [ + - + + : 174762544 : if (!cluster.NeedsSplitting() && !cluster.IsAcceptable() && !cluster.IsOversized()) {
+ + ]
2100 : 85 : cluster.Relinearize(*this, level, m_acceptable_iters);
2101 : : }
2102 : 174762544 : }
2103 : :
2104 : 196441 : void TxGraphImpl::MakeAllAcceptable(int level) noexcept
2105 : : {
2106 : 196441 : ApplyDependencies(level);
2107 : 196441 : auto& clusterset = GetClusterSet(level);
2108 [ + - ]: 196441 : if (clusterset.m_oversized == true) return;
2109 [ + + ]: 589323 : for (auto quality : {QualityLevel::NEEDS_FIX, QualityLevel::NEEDS_RELINEARIZE}) {
2110 : 392882 : auto& queue = clusterset.m_clusters[int(quality)];
2111 [ + + ]: 392959 : while (!queue.empty()) {
2112 : 77 : MakeAcceptable(*queue.back().get(), level);
2113 : : }
2114 : : }
2115 : : }
2116 : :
2117 : 1941 : GenericClusterImpl::GenericClusterImpl(uint64_t sequence) noexcept : Cluster{sequence} {}
2118 : :
2119 : 125274 : TxGraph::Ref TxGraphImpl::AddTransaction(const FeePerWeight& feerate) noexcept
2120 : : {
2121 [ - + - + ]: 125274 : Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
2122 : 125274 : Assume(feerate.size > 0);
2123 : : // Construct a new Ref.
2124 : 125274 : Ref ret;
2125 : : // Construct a new Entry, and link it with the Ref.
2126 [ - + ]: 125274 : auto idx = m_entries.size();
2127 : 125274 : m_entries.emplace_back();
2128 : 125274 : auto& entry = m_entries.back();
2129 : 125274 : entry.m_main_chunkindex_iterator = m_main_chunkindex.end();
2130 : 125274 : entry.m_ref = &ret;
2131 : 125274 : GetRefGraph(ret) = this;
2132 : 125274 : GetRefIndex(ret) = idx;
2133 : : // Construct a new singleton Cluster (which is necessarily optimally linearized).
2134 : 125274 : bool oversized = uint64_t(feerate.size) > m_max_cluster_size;
2135 : 125274 : auto cluster = CreateEmptyCluster(1);
2136 : 125274 : cluster->AppendTransaction(idx, feerate);
2137 [ + + ]: 125274 : auto cluster_ptr = cluster.get();
2138 [ + + ]: 125274 : int level = GetTopLevel();
2139 : 125274 : auto& clusterset = GetClusterSet(level);
2140 [ + + ]: 250518 : InsertCluster(level, std::move(cluster), oversized ? QualityLevel::OVERSIZED_SINGLETON : QualityLevel::OPTIMAL);
2141 : 125274 : cluster_ptr->Updated(*this, level);
2142 : 125274 : clusterset.m_cluster_usage += cluster_ptr->TotalMemoryUsage();
2143 : 125274 : ++clusterset.m_txcount;
2144 : : // Deal with individually oversized transactions.
2145 [ + + ]: 125274 : if (oversized) {
2146 : 30 : ++clusterset.m_txcount_oversized;
2147 : 30 : clusterset.m_oversized = true;
2148 [ + + ]: 30 : clusterset.m_group_data = std::nullopt;
2149 : : }
2150 : : // Return the Ref.
2151 : 250548 : return ret;
2152 : 125274 : }
2153 : :
2154 : 2132 : void TxGraphImpl::RemoveTransaction(const Ref& arg) noexcept
2155 : : {
2156 : : // Don't do anything if the Ref is empty (which may be indicative of the transaction already
2157 : : // having been removed).
2158 [ + - ]: 2132 : if (GetRefGraph(arg) == nullptr) return;
2159 [ - + ]: 2132 : Assume(GetRefGraph(arg) == this);
2160 [ - + + - ]: 2132 : Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
2161 : : // Find the Cluster the transaction is in, and stop if it isn't in any.
2162 [ + - ]: 2132 : int level = GetTopLevel();
2163 [ + - ]: 2132 : auto cluster = FindCluster(GetRefIndex(arg), level);
2164 [ + - ]: 2132 : if (cluster == nullptr) return;
2165 : : // Remember that the transaction is to be removed.
2166 : 2132 : auto& clusterset = GetClusterSet(level);
2167 : 2132 : clusterset.m_to_remove.push_back(GetRefIndex(arg));
2168 : : // Wipe m_group_data (as it will need to be recomputed).
2169 [ + + ]: 2132 : clusterset.m_group_data.reset();
2170 [ + - ]: 2132 : if (clusterset.m_oversized == true) clusterset.m_oversized = std::nullopt;
2171 : : }
2172 : :
2173 : 77069 : void TxGraphImpl::AddDependency(const Ref& parent, const Ref& child) noexcept
2174 : : {
2175 : : // Don't do anything if either Ref is empty (which may be indicative of it having already been
2176 : : // removed).
2177 [ + - + - ]: 77069 : if (GetRefGraph(parent) == nullptr || GetRefGraph(child) == nullptr) return;
2178 [ - + ]: 77069 : Assume(GetRefGraph(parent) == this && GetRefGraph(child) == this);
2179 [ - + + - ]: 77069 : Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
2180 : : // Don't do anything if this is a dependency on self.
2181 [ + - ]: 77069 : if (GetRefIndex(parent) == GetRefIndex(child)) return;
2182 : : // Find the Cluster the parent and child transaction are in, and stop if either appears to be
2183 : : // already removed.
2184 [ + - ]: 77069 : int level = GetTopLevel();
2185 : 77069 : auto par_cluster = FindCluster(GetRefIndex(parent), level);
2186 [ + - ]: 77069 : if (par_cluster == nullptr) return;
2187 : 77069 : auto chl_cluster = FindCluster(GetRefIndex(child), level);
2188 [ + - ]: 77069 : if (chl_cluster == nullptr) return;
2189 : : // Remember that this dependency is to be applied.
2190 : 77069 : auto& clusterset = GetClusterSet(level);
2191 : 77069 : clusterset.m_deps_to_add.emplace_back(GetRefIndex(parent), GetRefIndex(child));
2192 : : // Wipe m_group_data (as it will need to be recomputed).
2193 [ + + ]: 77069 : clusterset.m_group_data.reset();
2194 [ + + ]: 84017 : if (clusterset.m_oversized == false) clusterset.m_oversized = std::nullopt;
2195 : : }
2196 : :
2197 : 300 : bool TxGraphImpl::Exists(const Ref& arg, Level level_select) noexcept
2198 : : {
2199 [ + - ]: 300 : if (GetRefGraph(arg) == nullptr) return false;
2200 [ + - ]: 300 : Assume(GetRefGraph(arg) == this);
2201 [ + - ]: 300 : size_t level = GetSpecifiedLevel(level_select);
2202 : : // Make sure the transaction isn't scheduled for removal.
2203 : 300 : ApplyRemovals(level);
2204 : 300 : auto cluster = FindCluster(GetRefIndex(arg), level);
2205 : 300 : return cluster != nullptr;
2206 : : }
2207 : :
2208 : 47666 : void GenericClusterImpl::GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
2209 : : {
2210 : : /** The union of all ancestors to be returned. */
2211 : 47666 : SetType ancestors_union;
2212 : : // Process elements from the front of args, as long as they apply.
2213 [ + + ]: 95332 : while (!args.empty()) {
2214 [ + - ]: 47666 : if (args.front().first != this) break;
2215 : 47666 : ancestors_union |= m_depgraph.Ancestors(args.front().second);
2216 : 47666 : args = args.subspan(1);
2217 : : }
2218 [ + - ]: 47666 : Assume(ancestors_union.Any());
2219 : : // Translate all ancestors (in arbitrary order) to Refs (if they have any), and return them.
2220 [ + - + + ]: 352267 : for (auto idx : ancestors_union) {
2221 : 256935 : const auto& entry = graph.m_entries[m_mapping[idx]];
2222 : 256935 : Assume(entry.m_ref != nullptr);
2223 : 256935 : output.push_back(entry.m_ref);
2224 : : }
2225 : 47666 : }
2226 : :
2227 : 122230 : void SingletonClusterImpl::GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
2228 : : {
2229 : 122230 : Assume(GetTxCount());
2230 [ + + ]: 244460 : while (!args.empty()) {
2231 [ + - ]: 122230 : if (args.front().first != this) break;
2232 : 122230 : args = args.subspan(1);
2233 : : }
2234 : 122230 : const auto& entry = graph.m_entries[m_graph_index];
2235 : 122230 : Assume(entry.m_ref != nullptr);
2236 : 122230 : output.push_back(entry.m_ref);
2237 : 122230 : }
2238 : :
2239 : 46341 : void GenericClusterImpl::GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
2240 : : {
2241 : : /** The union of all descendants to be returned. */
2242 : 46341 : SetType descendants_union;
2243 : : // Process elements from the front of args, as long as they apply.
2244 [ + + ]: 92682 : while (!args.empty()) {
2245 [ + + ]: 46343 : if (args.front().first != this) break;
2246 : 46341 : descendants_union |= m_depgraph.Descendants(args.front().second);
2247 : 46341 : args = args.subspan(1);
2248 : : }
2249 [ + - ]: 46341 : Assume(descendants_union.Any());
2250 : : // Translate all descendants (in arbitrary order) to Refs (if they have any), and return them.
2251 [ + - + + ]: 487703 : for (auto idx : descendants_union) {
2252 : 395021 : const auto& entry = graph.m_entries[m_mapping[idx]];
2253 : 395021 : Assume(entry.m_ref != nullptr);
2254 : 395021 : output.push_back(entry.m_ref);
2255 : : }
2256 : 46341 : }
2257 : :
2258 : 43019 : void SingletonClusterImpl::GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
2259 : : {
2260 : : // In a singleton cluster, the ancestors or descendants are always just the entire cluster.
2261 : 43019 : GetAncestorRefs(graph, args, output);
2262 : 43019 : }
2263 : :
2264 : 191954 : bool GenericClusterImpl::GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept
2265 : : {
2266 : : // Translate the transactions in the Cluster (in linearization order, starting at start_pos in
2267 : : // the linearization) to Refs, and fill them in range.
2268 [ + + ]: 466502 : for (auto& ref : range) {
2269 [ - + ]: 274548 : Assume(start_pos < m_linearization.size());
2270 : 274548 : const auto& entry = graph.m_entries[m_mapping[m_linearization[start_pos++]]];
2271 : 274548 : Assume(entry.m_ref != nullptr);
2272 : 274548 : ref = entry.m_ref;
2273 : : }
2274 : : // Return whether start_pos has advanced to the end of the Cluster.
2275 [ - + ]: 191954 : return start_pos == m_linearization.size();
2276 : : }
2277 : :
2278 : 74742 : bool SingletonClusterImpl::GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept
2279 : : {
2280 : 74742 : Assume(!range.empty());
2281 : 74742 : Assume(GetTxCount());
2282 : 74742 : Assume(start_pos == 0);
2283 : 74742 : const auto& entry = graph.m_entries[m_graph_index];
2284 : 74742 : Assume(entry.m_ref != nullptr);
2285 : 74742 : range[0] = entry.m_ref;
2286 : 74742 : return true;
2287 : : }
2288 : :
2289 : 0 : FeePerWeight GenericClusterImpl::GetIndividualFeerate(DepGraphIndex idx) noexcept
2290 : : {
2291 : 0 : return FeePerWeight::FromFeeFrac(m_depgraph.FeeRate(idx));
2292 : : }
2293 : :
2294 : 0 : FeePerWeight SingletonClusterImpl::GetIndividualFeerate(DepGraphIndex idx) noexcept
2295 : : {
2296 : 0 : Assume(GetTxCount());
2297 : 0 : Assume(idx == 0);
2298 : 0 : return m_feerate;
2299 : : }
2300 : :
2301 : 25 : void GenericClusterImpl::MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept
2302 : : {
2303 : : // Mark all transactions of a Cluster missing, needed when aborting staging, so that the
2304 : : // corresponding Locators don't retain references into aborted Clusters.
2305 [ + + ]: 160 : for (auto ci : m_linearization) {
2306 : 135 : GraphIndex idx = m_mapping[ci];
2307 : 135 : auto& entry = graph.m_entries[idx];
2308 : 135 : entry.m_locator[1].SetMissing();
2309 : : }
2310 : 25 : }
2311 : :
2312 : 11269 : void SingletonClusterImpl::MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept
2313 : : {
2314 [ + - ]: 11269 : if (GetTxCount()) {
2315 : 11269 : auto& entry = graph.m_entries[m_graph_index];
2316 : 11269 : entry.m_locator[1].SetMissing();
2317 : : }
2318 : 11269 : }
2319 : :
2320 : 128140 : std::vector<TxGraph::Ref*> TxGraphImpl::GetAncestors(const Ref& arg, Level level_select) noexcept
2321 : : {
2322 : : // Return the empty vector if the Ref is empty.
2323 [ - + ]: 128140 : if (GetRefGraph(arg) == nullptr) return {};
2324 [ - + ]: 128140 : Assume(GetRefGraph(arg) == this);
2325 : : // Apply all removals and dependencies, as the result might be incorrect otherwise.
2326 [ - + ]: 128140 : size_t level = GetSpecifiedLevel(level_select);
2327 : 128140 : ApplyDependencies(level);
2328 : : // Ancestry cannot be known if unapplied dependencies remain.
2329 [ + + ]: 128140 : Assume(GetClusterSet(level).m_deps_to_add.empty());
2330 : : // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
2331 [ + + ]: 128140 : auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(arg), level);
2332 [ + + ]: 128140 : if (cluster == nullptr) return {};
2333 : : // Dispatch to the Cluster.
2334 : 126877 : std::pair<Cluster*, DepGraphIndex> match = {cluster, m_entries[GetRefIndex(arg)].m_locator[cluster_level].index};
2335 : 126877 : auto matches = std::span(&match, 1);
2336 : 126877 : std::vector<TxGraph::Ref*> ret;
2337 : 126877 : cluster->GetAncestorRefs(*this, matches, ret);
2338 : 126877 : return ret;
2339 : 126877 : }
2340 : :
2341 : 89271 : std::vector<TxGraph::Ref*> TxGraphImpl::GetDescendants(const Ref& arg, Level level_select) noexcept
2342 : : {
2343 : : // Return the empty vector if the Ref is empty.
2344 [ - + ]: 89271 : if (GetRefGraph(arg) == nullptr) return {};
2345 [ - + ]: 89271 : Assume(GetRefGraph(arg) == this);
2346 : : // Apply all removals and dependencies, as the result might be incorrect otherwise.
2347 [ - + ]: 89271 : size_t level = GetSpecifiedLevel(level_select);
2348 : 89271 : ApplyDependencies(level);
2349 : : // Ancestry cannot be known if unapplied dependencies remain.
2350 [ - + ]: 89271 : Assume(GetClusterSet(level).m_deps_to_add.empty());
2351 : : // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
2352 [ - + ]: 89271 : auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(arg), level);
2353 [ - + ]: 89271 : if (cluster == nullptr) return {};
2354 : : // Dispatch to the Cluster.
2355 : 89271 : std::pair<Cluster*, DepGraphIndex> match = {cluster, m_entries[GetRefIndex(arg)].m_locator[cluster_level].index};
2356 : 89271 : auto matches = std::span(&match, 1);
2357 : 89271 : std::vector<TxGraph::Ref*> ret;
2358 : 89271 : cluster->GetDescendantRefs(*this, matches, ret);
2359 : 89271 : return ret;
2360 : 89271 : }
2361 : :
2362 : 0 : std::vector<TxGraph::Ref*> TxGraphImpl::GetAncestorsUnion(std::span<const Ref* const> args, Level level_select) noexcept
2363 : : {
2364 : : // Apply all dependencies, as the result might be incorrect otherwise.
2365 [ # # ]: 0 : size_t level = GetSpecifiedLevel(level_select);
2366 : 0 : ApplyDependencies(level);
2367 : : // Ancestry cannot be known if unapplied dependencies remain.
2368 : 0 : Assume(GetClusterSet(level).m_deps_to_add.empty());
2369 : :
2370 : : // Translate args to matches.
2371 : 0 : std::vector<std::pair<Cluster*, DepGraphIndex>> matches;
2372 : 0 : matches.reserve(args.size());
2373 [ # # ]: 0 : for (auto arg : args) {
2374 [ # # ]: 0 : Assume(arg);
2375 : : // Skip empty Refs.
2376 [ # # ]: 0 : if (GetRefGraph(*arg) == nullptr) continue;
2377 [ # # ]: 0 : Assume(GetRefGraph(*arg) == this);
2378 : : // Find the Cluster the argument is in, and skip if none is found.
2379 [ # # ]: 0 : auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(*arg), level);
2380 [ # # ]: 0 : if (cluster == nullptr) continue;
2381 : : // Append to matches.
2382 : 0 : matches.emplace_back(cluster, m_entries[GetRefIndex(*arg)].m_locator[cluster_level].index);
2383 : : }
2384 : : // Group by Cluster.
2385 : 0 : std::sort(matches.begin(), matches.end(), [](auto& a, auto& b) noexcept { return CompareClusters(a.first, b.first) < 0; });
2386 : : // Dispatch to the Clusters.
2387 [ # # ]: 0 : std::span match_span(matches);
2388 : 0 : std::vector<TxGraph::Ref*> ret;
2389 [ # # ]: 0 : while (!match_span.empty()) {
2390 : 0 : match_span.front().first->GetAncestorRefs(*this, match_span, ret);
2391 : : }
2392 : 0 : return ret;
2393 : 0 : }
2394 : :
2395 : 22506 : std::vector<TxGraph::Ref*> TxGraphImpl::GetDescendantsUnion(std::span<const Ref* const> args, Level level_select) noexcept
2396 : : {
2397 : : // Apply all dependencies, as the result might be incorrect otherwise.
2398 [ - + ]: 22506 : size_t level = GetSpecifiedLevel(level_select);
2399 : 22506 : ApplyDependencies(level);
2400 : : // Ancestry cannot be known if unapplied dependencies remain.
2401 : 22506 : Assume(GetClusterSet(level).m_deps_to_add.empty());
2402 : :
2403 : : // Translate args to matches.
2404 : 22506 : std::vector<std::pair<Cluster*, DepGraphIndex>> matches;
2405 : 22506 : matches.reserve(args.size());
2406 [ + + ]: 22595 : for (auto arg : args) {
2407 [ - + ]: 89 : Assume(arg);
2408 : : // Skip empty Refs.
2409 [ - + ]: 89 : if (GetRefGraph(*arg) == nullptr) continue;
2410 [ - + ]: 89 : Assume(GetRefGraph(*arg) == this);
2411 : : // Find the Cluster the argument is in, and skip if none is found.
2412 [ - + ]: 89 : auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(*arg), level);
2413 [ - + ]: 89 : if (cluster == nullptr) continue;
2414 : : // Append to matches.
2415 : 89 : matches.emplace_back(cluster, m_entries[GetRefIndex(*arg)].m_locator[cluster_level].index);
2416 : : }
2417 : : // Group by Cluster.
2418 : 22506 : std::sort(matches.begin(), matches.end(), [](auto& a, auto& b) noexcept { return CompareClusters(a.first, b.first) < 0; });
2419 : : // Dispatch to the Clusters.
2420 [ - + ]: 22506 : std::span match_span(matches);
2421 : 22506 : std::vector<TxGraph::Ref*> ret;
2422 [ + + ]: 22595 : while (!match_span.empty()) {
2423 : 89 : match_span.front().first->GetDescendantRefs(*this, match_span, ret);
2424 : : }
2425 : 22506 : return ret;
2426 : 22506 : }
2427 : :
2428 : 98562 : std::vector<TxGraph::Ref*> TxGraphImpl::GetCluster(const Ref& arg, Level level_select) noexcept
2429 : : {
2430 : : // Return the empty vector if the Ref is empty (which may be indicative of the transaction
2431 : : // having been removed already.
2432 [ - + ]: 98562 : if (GetRefGraph(arg) == nullptr) return {};
2433 [ - + ]: 98562 : Assume(GetRefGraph(arg) == this);
2434 : : // Apply all removals and dependencies, as the result might be incorrect otherwise.
2435 [ - + ]: 98562 : size_t level = GetSpecifiedLevel(level_select);
2436 : 98562 : ApplyDependencies(level);
2437 : : // Cluster linearization cannot be known if unapplied dependencies remain.
2438 [ - + ]: 98562 : Assume(GetClusterSet(level).m_deps_to_add.empty());
2439 : : // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
2440 [ - + ]: 98562 : auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(arg), level);
2441 [ - + ]: 98562 : if (cluster == nullptr) return {};
2442 : : // Make sure the Cluster has an acceptable quality level, and then dispatch to it.
2443 : 98562 : MakeAcceptable(*cluster, cluster_level);
2444 [ - + ]: 98562 : std::vector<TxGraph::Ref*> ret(cluster->GetTxCount());
2445 [ - + ]: 98562 : cluster->GetClusterRefs(*this, ret, 0);
2446 : 98562 : return ret;
2447 : 98562 : }
2448 : :
2449 : 7 : TxGraph::GraphIndex TxGraphImpl::GetTransactionCount(Level level_select) noexcept
2450 : : {
2451 [ + - ]: 7 : size_t level = GetSpecifiedLevel(level_select);
2452 : 7 : ApplyRemovals(level);
2453 : 7 : return GetClusterSet(level).m_txcount;
2454 : : }
2455 : :
2456 : 0 : FeePerWeight TxGraphImpl::GetIndividualFeerate(const Ref& arg) noexcept
2457 : : {
2458 : : // Return the empty FeePerWeight if the passed Ref is empty.
2459 [ # # ]: 0 : if (GetRefGraph(arg) == nullptr) return {};
2460 : 0 : Assume(GetRefGraph(arg) == this);
2461 : : // Find the cluster the argument is in (the level does not matter as individual feerates will
2462 : : // be identical if it occurs in both), and return the empty FeePerWeight if it isn't in any.
2463 : 0 : Cluster* cluster{nullptr};
2464 : 0 : int level;
2465 [ # # ]: 0 : for (level = 0; level <= GetTopLevel(); ++level) {
2466 : : // Apply removals, so that we can correctly report FeePerWeight{} for non-existing
2467 : : // transactions.
2468 : 0 : ApplyRemovals(level);
2469 [ # # ]: 0 : if (m_entries[GetRefIndex(arg)].m_locator[level].IsPresent()) {
2470 : : cluster = m_entries[GetRefIndex(arg)].m_locator[level].cluster;
2471 : : break;
2472 : : }
2473 : : }
2474 [ # # ]: 0 : if (cluster == nullptr) return {};
2475 : : // Dispatch to the Cluster.
2476 : 0 : return cluster->GetIndividualFeerate(m_entries[GetRefIndex(arg)].m_locator[level].index);
2477 : : }
2478 : :
2479 : 60619 : FeePerWeight TxGraphImpl::GetMainChunkFeerate(const Ref& arg) noexcept
2480 : : {
2481 : : // Return the empty FeePerWeight if the passed Ref is empty.
2482 [ - + ]: 60619 : if (GetRefGraph(arg) == nullptr) return {};
2483 : 60619 : Assume(GetRefGraph(arg) == this);
2484 : : // Apply all removals and dependencies, as the result might be inaccurate otherwise.
2485 : 60619 : ApplyDependencies(/*level=*/0);
2486 : : // Chunk feerates cannot be accurately known if unapplied dependencies remain.
2487 [ - + ]: 60619 : Assume(m_main_clusterset.m_deps_to_add.empty());
2488 : : // Find the cluster the argument is in, and return the empty FeePerWeight if it isn't in any.
2489 [ - + ]: 60619 : auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(arg), /*level=*/0);
2490 [ - + ]: 60619 : if (cluster == nullptr) return {};
2491 : : // Make sure the Cluster has an acceptable quality level, and then return the transaction's
2492 : : // chunk feerate.
2493 : 60619 : MakeAcceptable(*cluster, cluster_level);
2494 : 60619 : const auto& entry = m_entries[GetRefIndex(arg)];
2495 : 60619 : return entry.m_main_chunk_feerate;
2496 : : }
2497 : :
2498 : 217011 : bool TxGraphImpl::IsOversized(Level level_select) noexcept
2499 : : {
2500 [ + + ]: 217011 : size_t level = GetSpecifiedLevel(level_select);
2501 : 217011 : auto& clusterset = GetClusterSet(level);
2502 [ + + ]: 217011 : if (clusterset.m_oversized.has_value()) {
2503 : : // Return cached value if known.
2504 : 210086 : return *clusterset.m_oversized;
2505 : : }
2506 : 6925 : ApplyRemovals(level);
2507 [ - + ]: 6925 : if (clusterset.m_txcount_oversized > 0) {
2508 : 0 : clusterset.m_oversized = true;
2509 : : } else {
2510 : : // Find which Clusters will need to be merged together, as that is where the oversize
2511 : : // property is assessed.
2512 : 6925 : GroupClusters(level);
2513 : : }
2514 : 6925 : Assume(clusterset.m_oversized.has_value());
2515 : 6925 : return *clusterset.m_oversized;
2516 : : }
2517 : :
2518 : 60336 : void TxGraphImpl::StartStaging() noexcept
2519 : : {
2520 : : // Staging cannot already exist.
2521 : 60336 : Assume(!m_staging_clusterset.has_value());
2522 : : // Apply all remaining dependencies in main before creating a staging graph. Once staging
2523 : : // exists, we cannot merge Clusters anymore (because of interference with Clusters being
2524 : : // pulled into staging), so to make sure all inspectors are available (if not oversized), do
2525 : : // all merging work now. Call SplitAll() first, so that even if ApplyDependencies does not do
2526 : : // any thing due to knowing the result is oversized, splitting is still performed.
2527 : 60336 : SplitAll(0);
2528 : 60336 : ApplyDependencies(0);
2529 : : // Construct the staging ClusterSet.
2530 : 60336 : m_staging_clusterset.emplace();
2531 : : // Copy statistics, precomputed data, and to-be-applied dependencies (only if oversized) to
2532 : : // the new graph. To-be-applied removals will always be empty at this point.
2533 : 60336 : m_staging_clusterset->m_txcount = m_main_clusterset.m_txcount;
2534 : 60336 : m_staging_clusterset->m_txcount_oversized = m_main_clusterset.m_txcount_oversized;
2535 : 60336 : m_staging_clusterset->m_deps_to_add = m_main_clusterset.m_deps_to_add;
2536 : 60336 : m_staging_clusterset->m_group_data = m_main_clusterset.m_group_data;
2537 : 60336 : m_staging_clusterset->m_oversized = m_main_clusterset.m_oversized;
2538 : 60336 : Assume(m_staging_clusterset->m_oversized.has_value());
2539 : 60336 : m_staging_clusterset->m_cluster_usage = 0;
2540 : 60336 : }
2541 : :
2542 : 10713 : void TxGraphImpl::AbortStaging() noexcept
2543 : : {
2544 : : // Staging must exist.
2545 : 10713 : Assume(m_staging_clusterset.has_value());
2546 : : // Mark all removed transactions as Missing (so the staging locator for these transactions
2547 : : // can be reused if another staging is created).
2548 [ + + ]: 11301 : for (auto idx : m_staging_clusterset->m_removed) {
2549 : 588 : m_entries[idx].m_locator[1].SetMissing();
2550 : : }
2551 : : // Do the same with the non-removed transactions in staging Clusters.
2552 [ + + ]: 85704 : for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
2553 [ + + ]: 86285 : for (auto& cluster : m_staging_clusterset->m_clusters[quality]) {
2554 : 11294 : cluster->MakeStagingTransactionsMissing(*this);
2555 : : }
2556 : : }
2557 : : // Destroy the staging ClusterSet.
2558 : 10713 : m_staging_clusterset.reset();
2559 : 10713 : Compact();
2560 [ - + ]: 10713 : if (!m_main_clusterset.m_group_data.has_value()) {
2561 : : // In case m_oversized in main was kept after a Ref destruction while staging exists, we
2562 : : // need to re-evaluate m_oversized now.
2563 [ # # # # ]: 0 : if (m_main_clusterset.m_to_remove.empty() && m_main_clusterset.m_txcount_oversized > 0) {
2564 : : // It is possible that a Ref destruction caused a removal in main while staging existed.
2565 : : // In this case, m_txcount_oversized may be inaccurate.
2566 : 0 : m_main_clusterset.m_oversized = true;
2567 : : } else {
2568 [ # # ]: 0 : m_main_clusterset.m_oversized = std::nullopt;
2569 : : }
2570 : : }
2571 : 10713 : }
2572 : :
2573 : 49623 : void TxGraphImpl::CommitStaging() noexcept
2574 : : {
2575 : : // Staging must exist.
2576 : 49623 : Assume(m_staging_clusterset.has_value());
2577 : 49623 : Assume(m_main_chunkindex_observers == 0);
2578 : : // Delete all conflicting Clusters in main, to make place for moving the staging ones
2579 : : // there. All of these have been copied to staging in PullIn().
2580 : 49623 : auto conflicts = GetConflicts();
2581 [ + + ]: 50841 : for (Cluster* conflict : conflicts) {
2582 : 1218 : conflict->Clear(*this, /*level=*/0);
2583 : 1218 : DeleteCluster(*conflict, /*level=*/0);
2584 : : }
2585 : : // Mark the removed transactions as Missing (so the staging locator for these transactions
2586 : : // can be reused if another staging is created).
2587 [ + + ]: 51159 : for (auto idx : m_staging_clusterset->m_removed) {
2588 : 1536 : m_entries[idx].m_locator[1].SetMissing();
2589 : : }
2590 : : // Then move all Clusters in staging to main.
2591 [ + + ]: 396984 : for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
2592 : 347361 : auto& stage_sets = m_staging_clusterset->m_clusters[quality];
2593 [ + + ]: 397093 : while (!stage_sets.empty()) {
2594 : 49732 : stage_sets.back()->MoveToMain(*this);
2595 : : }
2596 : : }
2597 : : // Move all statistics, precomputed data, and to-be-applied removals and dependencies.
2598 : 49623 : m_main_clusterset.m_deps_to_add = std::move(m_staging_clusterset->m_deps_to_add);
2599 : 49623 : m_main_clusterset.m_to_remove = std::move(m_staging_clusterset->m_to_remove);
2600 : 49623 : m_main_clusterset.m_group_data = std::move(m_staging_clusterset->m_group_data);
2601 : 49623 : m_main_clusterset.m_oversized = std::move(m_staging_clusterset->m_oversized);
2602 : 49623 : m_main_clusterset.m_txcount = std::move(m_staging_clusterset->m_txcount);
2603 : 49623 : m_main_clusterset.m_txcount_oversized = std::move(m_staging_clusterset->m_txcount_oversized);
2604 : : // Delete the old staging graph, after all its information was moved to main.
2605 : 49623 : m_staging_clusterset.reset();
2606 : 49623 : Compact();
2607 : 49623 : }
2608 : :
2609 : 16 : void GenericClusterImpl::SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept
2610 : : {
2611 : : // Make sure the specified DepGraphIndex exists in this Cluster.
2612 [ + - ]: 16 : Assume(m_depgraph.Positions()[idx]);
2613 : : // Bail out if the fee isn't actually being changed.
2614 [ + - ]: 16 : if (m_depgraph.FeeRate(idx).fee == fee) return;
2615 : : // Update the fee, remember that relinearization will be necessary, and update the Entries
2616 : : // in the same Cluster.
2617 [ + - ]: 16 : m_depgraph.FeeRate(idx).fee = fee;
2618 [ + - ]: 16 : if (m_quality == QualityLevel::OVERSIZED_SINGLETON) {
2619 : : // Nothing to do.
2620 [ + + ]: 16 : } else if (IsAcceptable()) {
2621 : 12 : graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::NEEDS_RELINEARIZE);
2622 : : }
2623 : 16 : Updated(graph, level);
2624 : : }
2625 : :
2626 : 287 : void SingletonClusterImpl::SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept
2627 : : {
2628 : 287 : Assume(GetTxCount());
2629 : 287 : Assume(idx == 0);
2630 : 287 : m_feerate.fee = fee;
2631 : 287 : Updated(graph, level);
2632 : 287 : }
2633 : :
2634 : 303 : void TxGraphImpl::SetTransactionFee(const Ref& ref, int64_t fee) noexcept
2635 : : {
2636 : : // Don't do anything if the passed Ref is empty.
2637 [ + - ]: 303 : if (GetRefGraph(ref) == nullptr) return;
2638 : 303 : Assume(GetRefGraph(ref) == this);
2639 : 303 : Assume(m_main_chunkindex_observers == 0);
2640 : : // Find the entry, its locator, and inform its Cluster about the new feerate, if any.
2641 : 303 : auto& entry = m_entries[GetRefIndex(ref)];
2642 [ + + ]: 909 : for (int level = 0; level < MAX_LEVELS; ++level) {
2643 : 606 : auto& locator = entry.m_locator[level];
2644 [ + + ]: 606 : if (locator.IsPresent()) {
2645 : 303 : locator.cluster->SetFee(*this, level, locator.index, fee);
2646 : : }
2647 : : }
2648 : : }
2649 : :
2650 : 87268686 : std::strong_ordering TxGraphImpl::CompareMainOrder(const Ref& a, const Ref& b) noexcept
2651 : : {
2652 : : // The references must not be empty.
2653 : 87268686 : Assume(GetRefGraph(a) == this);
2654 : 87268686 : Assume(GetRefGraph(b) == this);
2655 : : // Apply dependencies in main.
2656 : 87268686 : ApplyDependencies(0);
2657 : 87268686 : Assume(m_main_clusterset.m_deps_to_add.empty());
2658 : : // Make both involved Clusters acceptable, so chunk feerates are relevant.
2659 : 87268686 : const auto& entry_a = m_entries[GetRefIndex(a)];
2660 : 87268686 : const auto& entry_b = m_entries[GetRefIndex(b)];
2661 : 87268686 : const auto& locator_a = entry_a.m_locator[0];
2662 : 87268686 : const auto& locator_b = entry_b.m_locator[0];
2663 : 87268686 : Assume(locator_a.IsPresent());
2664 : 87268686 : Assume(locator_b.IsPresent());
2665 : 87268686 : MakeAcceptable(*locator_a.cluster, /*level=*/0);
2666 : 87268686 : MakeAcceptable(*locator_b.cluster, /*level=*/0);
2667 : : // Invoke comparison logic.
2668 : 87268686 : return CompareMainTransactions(GetRefIndex(a), GetRefIndex(b));
2669 : : }
2670 : :
2671 : 1341 : TxGraph::GraphIndex TxGraphImpl::CountDistinctClusters(std::span<const Ref* const> refs, Level level_select) noexcept
2672 : : {
2673 [ - + ]: 1341 : size_t level = GetSpecifiedLevel(level_select);
2674 : 1341 : ApplyDependencies(level);
2675 : 1341 : auto& clusterset = GetClusterSet(level);
2676 : 1341 : Assume(clusterset.m_deps_to_add.empty());
2677 : : // Build a vector of Clusters that the specified Refs occur in.
2678 : 1341 : std::vector<Cluster*> clusters;
2679 : 1341 : clusters.reserve(refs.size());
2680 [ + + ]: 3937 : for (const Ref* ref : refs) {
2681 [ - + ]: 2596 : Assume(ref);
2682 [ - + ]: 2596 : if (GetRefGraph(*ref) == nullptr) continue;
2683 [ + - ]: 2596 : Assume(GetRefGraph(*ref) == this);
2684 [ + - ]: 2596 : auto cluster = FindCluster(GetRefIndex(*ref), level);
2685 [ + - ]: 2596 : if (cluster != nullptr) clusters.push_back(cluster);
2686 : : }
2687 : : // Count the number of distinct elements in clusters.
2688 : 1341 : std::sort(clusters.begin(), clusters.end(), [](Cluster* a, Cluster* b) noexcept { return CompareClusters(a, b) < 0; });
2689 : 1341 : Cluster* last{nullptr};
2690 : 1341 : GraphIndex ret{0};
2691 [ + + ]: 3937 : for (Cluster* cluster : clusters) {
2692 : 2596 : ret += (cluster != last);
2693 : 2596 : last = cluster;
2694 : : }
2695 : 1341 : return ret;
2696 : 1341 : }
2697 : :
2698 : 1290 : std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>> TxGraphImpl::GetMainStagingDiagrams() noexcept
2699 : : {
2700 : 1290 : Assume(m_staging_clusterset.has_value());
2701 : 1290 : MakeAllAcceptable(0);
2702 : 1290 : Assume(m_main_clusterset.m_deps_to_add.empty()); // can only fail if main is oversized
2703 : 1290 : MakeAllAcceptable(1);
2704 : 1290 : Assume(m_staging_clusterset->m_deps_to_add.empty()); // can only fail if staging is oversized
2705 : : // For all Clusters in main which conflict with Clusters in staging (i.e., all that are removed
2706 : : // by, or replaced in, staging), gather their chunk feerates.
2707 : 1290 : auto main_clusters = GetConflicts();
2708 : 1290 : std::vector<FeeFrac> main_feerates, staging_feerates;
2709 [ + + ]: 2977 : for (Cluster* cluster : main_clusters) {
2710 : 1687 : cluster->AppendChunkFeerates(main_feerates);
2711 : : }
2712 : : // Do the same for the Clusters in staging themselves.
2713 [ + + ]: 10320 : for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
2714 [ + + ]: 10399 : for (const auto& cluster : m_staging_clusterset->m_clusters[quality]) {
2715 : 1369 : cluster->AppendChunkFeerates(staging_feerates);
2716 : : }
2717 : : }
2718 : : // Sort both by decreasing feerate to obtain diagrams, and return them.
2719 [ - - - - : 6316 : std::sort(main_feerates.begin(), main_feerates.end(), [](auto& a, auto& b) { return a > b; });
+ + + + +
+ - + - -
- - - + +
+ - - +
+ ]
2720 [ - - - - : 1935 : std::sort(staging_feerates.begin(), staging_feerates.end(), [](auto& a, auto& b) { return a > b; });
- + + + +
+ - + - -
- - + + -
+ - - +
+ ]
2721 : 1290 : return std::make_pair(std::move(main_feerates), std::move(staging_feerates));
2722 : 1290 : }
2723 : :
2724 : 53618 : void GenericClusterImpl::SanityCheck(const TxGraphImpl& graph, int level) const
2725 : : {
2726 : : // There must be an m_mapping for each m_depgraph position (including holes).
2727 [ - + - + : 53618 : assert(m_depgraph.PositionRange() == m_mapping.size());
- + ]
2728 : : // The linearization for this Cluster must contain every transaction once.
2729 [ - + - + ]: 53618 : assert(m_depgraph.TxCount() == m_linearization.size());
2730 : : // Unless a split is to be applied, the cluster cannot have any holes.
2731 [ + - ]: 53618 : if (!NeedsSplitting()) {
2732 [ - + ]: 53618 : assert(m_depgraph.Positions() == SetType::Fill(m_depgraph.TxCount()));
2733 : : }
2734 : :
2735 : : // Compute the chunking of m_linearization.
2736 : 53618 : auto linchunking = ChunkLinearizationInfo(m_depgraph, m_linearization);
2737 : 53618 : unsigned chunk_num{0};
2738 : :
2739 : : // Verify m_linearization.
2740 : 53618 : SetType m_done;
2741 : 53618 : LinearizationIndex linindex{0};
2742 : 53618 : DepGraphIndex chunk_pos{0}; //!< position within the current chunk
2743 [ - + ]: 53618 : assert(m_depgraph.IsAcyclic());
2744 [ + + ]: 279475 : for (auto lin_pos : m_linearization) {
2745 [ - + - + ]: 225857 : assert(lin_pos < m_mapping.size());
2746 [ + - ]: 225857 : const auto& entry = graph.m_entries[m_mapping[lin_pos]];
2747 : : // Check that the linearization is topological.
2748 [ + - ]: 225857 : m_done.Set(lin_pos);
2749 [ + - ]: 225857 : if (IsTopological()) {
2750 [ - + ]: 225857 : assert(m_done.IsSupersetOf(m_depgraph.Ancestors(lin_pos)));
2751 : : }
2752 : : // Check that the Entry has a locator pointing back to this Cluster & position within it.
2753 [ - + ]: 225857 : assert(entry.m_locator[level].cluster == this);
2754 [ - + ]: 225857 : assert(entry.m_locator[level].index == lin_pos);
2755 : : // For main-level entries, check linearization position and chunk feerate.
2756 [ + - + - ]: 225857 : if (level == 0 && IsAcceptable()) {
2757 [ - + ]: 225857 : assert(entry.m_main_lin_index == linindex);
2758 : 225857 : ++linindex;
2759 [ + + ]: 225857 : if (!linchunking[chunk_num].transactions[lin_pos]) {
2760 : 156027 : ++chunk_num;
2761 : 156027 : chunk_pos = 0;
2762 : : }
2763 [ + - ]: 225857 : assert(entry.m_main_chunk_feerate == linchunking[chunk_num].feerate);
2764 : : // Verify that an entry in the chunk index exists for every chunk-ending transaction.
2765 : 225857 : ++chunk_pos;
2766 [ - + ]: 225857 : bool is_chunk_end = (chunk_pos == linchunking[chunk_num].transactions.Count());
2767 [ - + ]: 225857 : assert((entry.m_main_chunkindex_iterator != graph.m_main_chunkindex.end()) == is_chunk_end);
2768 [ + + ]: 225857 : if (is_chunk_end) {
2769 [ + + ]: 209645 : auto& chunk_data = *entry.m_main_chunkindex_iterator;
2770 [ + + + + ]: 209645 : if (m_done == m_depgraph.Positions() && chunk_pos == 1) {
2771 [ - + ]: 45829 : assert(chunk_data.m_chunk_count == LinearizationIndex(-1));
2772 : : } else {
2773 [ - + ]: 163816 : assert(chunk_data.m_chunk_count == chunk_pos);
2774 : : }
2775 : : }
2776 : : // If this Cluster has an acceptable quality level, its chunks must be connected.
2777 [ - + ]: 225857 : assert(m_depgraph.IsConnected(linchunking[chunk_num].transactions));
2778 : : }
2779 : : }
2780 : : // Verify that each element of m_depgraph occurred in m_linearization.
2781 [ - + ]: 53618 : assert(m_done == m_depgraph.Positions());
2782 : 53618 : }
2783 : :
2784 : 7116897 : void SingletonClusterImpl::SanityCheck(const TxGraphImpl& graph, int level) const
2785 : : {
2786 : : // All singletons are optimal, oversized, or need splitting.
2787 [ + + + + : 7116897 : Assume(IsOptimal() || IsOversized() || NeedsSplitting());
+ + ]
2788 [ + + ]: 7116897 : if (GetTxCount()) {
2789 [ - + ]: 7116886 : const auto& entry = graph.m_entries[m_graph_index];
2790 : : // Check that the Entry has a locator pointing back to this Cluster & position within it.
2791 [ - + ]: 7116886 : assert(entry.m_locator[level].cluster == this);
2792 [ - + ]: 7116886 : assert(entry.m_locator[level].index == 0);
2793 : : // For main-level entries, check linearization position and chunk feerate.
2794 [ + - + + ]: 7116886 : if (level == 0 && IsAcceptable()) {
2795 [ - + ]: 7116874 : assert(entry.m_main_lin_index == 0);
2796 [ + - ]: 7116874 : assert(entry.m_main_chunk_feerate == m_feerate);
2797 [ - + ]: 7116874 : assert(entry.m_main_chunkindex_iterator != graph.m_main_chunkindex.end());
2798 [ - + ]: 7116874 : auto& chunk_data = *entry.m_main_chunkindex_iterator;
2799 [ - + ]: 7116874 : assert(chunk_data.m_chunk_count == LinearizationIndex(-1));
2800 : : }
2801 : : }
2802 : 7116897 : }
2803 : :
2804 : 153112 : void TxGraphImpl::SanityCheck() const
2805 : : {
2806 : : /** Which GraphIndexes ought to occur in m_unlinked, based on m_entries. */
2807 : 153112 : std::set<GraphIndex> expected_unlinked;
2808 : : /** Which Clusters ought to occur in ClusterSet::m_clusters, based on m_entries. */
2809 [ + + ]: 765560 : std::set<const Cluster*> expected_clusters[MAX_LEVELS];
2810 : : /** Which GraphIndexes ought to occur in ClusterSet::m_removed, based on m_entries. */
2811 [ + + ]: 765560 : std::set<GraphIndex> expected_removed[MAX_LEVELS];
2812 : : /** Which Cluster::m_sequence values have been encountered. */
2813 : 153112 : std::set<uint64_t> sequences;
2814 : : /** Which GraphIndexes ought to occur in m_main_chunkindex, based on m_entries. */
2815 : 153112 : std::set<GraphIndex> expected_chunkindex;
2816 : : /** Whether compaction is possible in the current state. */
2817 : 153112 : bool compact_possible{true};
2818 : :
2819 : : // Go over all Entry objects in m_entries.
2820 [ - + + + ]: 7495866 : for (GraphIndex idx = 0; idx < m_entries.size(); ++idx) {
2821 [ - + ]: 7342754 : const auto& entry = m_entries[idx];
2822 [ - + ]: 7342754 : if (entry.m_ref == nullptr) {
2823 : : // Unlinked Entry must have indexes appear in m_unlinked.
2824 [ # # ]: 0 : expected_unlinked.insert(idx);
2825 : : } else {
2826 : : // Every non-unlinked Entry must have a Ref that points back to it.
2827 [ - + ]: 7342754 : assert(GetRefGraph(*entry.m_ref) == this);
2828 [ - + ]: 7342754 : assert(GetRefIndex(*entry.m_ref) == idx);
2829 : : }
2830 [ + + ]: 7342754 : if (entry.m_main_chunkindex_iterator != m_main_chunkindex.end()) {
2831 : : // Remember which entries we see a chunkindex entry for.
2832 [ - + ]: 7326519 : assert(entry.m_locator[0].IsPresent());
2833 [ + - ]: 7326519 : expected_chunkindex.insert(idx);
2834 : : }
2835 : : // Verify the Entry m_locators.
2836 : : bool was_present{false}, was_removed{false};
2837 [ + + ]: 22028262 : for (int level = 0; level < MAX_LEVELS; ++level) {
2838 : 14685508 : const auto& locator = entry.m_locator[level];
2839 : : // Every Locator must be in exactly one of these 3 states.
2840 [ + + + + : 44056524 : assert(locator.IsMissing() + locator.IsRemoved() + locator.IsPresent() == 1);
- + ]
2841 [ + + ]: 14685508 : if (locator.IsPresent()) {
2842 : : // Once removed, a transaction cannot be revived.
2843 [ - + ]: 7342743 : assert(!was_removed);
2844 : : // Verify that the Cluster agrees with where the Locator claims the transaction is.
2845 [ - + ]: 7342743 : assert(locator.cluster->GetClusterEntry(locator.index) == idx);
2846 : : // Remember that we expect said Cluster to appear in the ClusterSet::m_clusters.
2847 [ + - ]: 7342743 : expected_clusters[level].insert(locator.cluster);
2848 : : was_present = true;
2849 [ - + ]: 14685508 : } else if (locator.IsRemoved()) {
2850 : : // Level 0 (main) cannot have IsRemoved locators (IsMissing there means non-existing).
2851 [ # # ]: 0 : assert(level > 0);
2852 : : // A Locator can only be IsRemoved if it was IsPresent before, and only once.
2853 [ # # ]: 0 : assert(was_present && !was_removed);
2854 : : // Remember that we expect this GraphIndex to occur in the ClusterSet::m_removed.
2855 [ # # ]: 0 : expected_removed[level].insert(idx);
2856 : : was_removed = true;
2857 : : }
2858 : : }
2859 : : }
2860 : :
2861 : : // For all levels (0 = main, 1 = staged)...
2862 [ + + ]: 306224 : for (int level = 0; level <= GetTopLevel(); ++level) {
2863 : 153112 : assert(level < MAX_LEVELS);
2864 : 153112 : auto& clusterset = GetClusterSet(level);
2865 : 153112 : std::set<const Cluster*> actual_clusters;
2866 : 153112 : size_t recomputed_cluster_usage{0};
2867 : :
2868 : : // For all quality levels...
2869 [ + + ]: 1224896 : for (int qual = 0; qual < int(QualityLevel::NONE); ++qual) {
2870 : 1071784 : QualityLevel quality{qual};
2871 : 1071784 : const auto& quality_clusters = clusterset.m_clusters[qual];
2872 : : // ... for all clusters in them ...
2873 [ - + + + ]: 8242299 : for (ClusterSetIndex setindex = 0; setindex < quality_clusters.size(); ++setindex) {
2874 : 7170515 : const auto& cluster = *quality_clusters[setindex];
2875 : : // The number of transactions in a Cluster cannot exceed m_max_cluster_count.
2876 [ - + ]: 7170515 : assert(cluster.GetTxCount() <= m_max_cluster_count);
2877 : : // The level must match the Cluster's own idea of what level it is in (but GetLevel
2878 : : // can only be called for non-empty Clusters).
2879 [ + + - + ]: 7170515 : assert(cluster.GetTxCount() == 0 || level == cluster.GetLevel(*this));
2880 : : // The sum of their sizes cannot exceed m_max_cluster_size, unless it is an
2881 : : // individually oversized transaction singleton. Note that groups of to-be-merged
2882 : : // clusters which would exceed this limit are marked oversized, which means they
2883 : : // are never applied.
2884 [ + + - + ]: 7170515 : assert(cluster.IsOversized() || cluster.GetTotalTxSize() <= m_max_cluster_size);
2885 : : // OVERSIZED clusters are singletons.
2886 [ + + - + ]: 7170515 : assert(!cluster.IsOversized() || cluster.GetTxCount() == 1);
2887 : : // Transaction counts cannot exceed the Cluster implementation's maximum
2888 : : // supported transactions count.
2889 [ - + ]: 7170515 : assert(cluster.GetTxCount() <= cluster.GetMaxTxCount());
2890 : : // Unless a Split is yet to be applied, the number of transactions must not be
2891 : : // below the Cluster implementation's intended transaction count.
2892 [ + + ]: 7170515 : if (!cluster.NeedsSplitting()) {
2893 [ - + ]: 7170504 : assert(cluster.GetTxCount() >= cluster.GetMinIntendedTxCount());
2894 : : }
2895 : :
2896 : : // Check the sequence number.
2897 [ - + ]: 7170515 : assert(cluster.m_sequence < m_next_sequence_counter);
2898 [ - + ]: 7170515 : assert(!sequences.contains(cluster.m_sequence));
2899 [ + - ]: 7170515 : sequences.insert(cluster.m_sequence);
2900 : : // Remember we saw this Cluster (only if it is non-empty; empty Clusters aren't
2901 : : // expected to be referenced by the Entry vector).
2902 [ + + ]: 7170515 : if (cluster.GetTxCount() != 0) {
2903 [ + - ]: 7170504 : actual_clusters.insert(&cluster);
2904 : : }
2905 : : // Sanity check the cluster, according to the Cluster's internal rules.
2906 [ + - ]: 7170515 : cluster.SanityCheck(*this, level);
2907 : : // Check that the cluster's quality and setindex matches its position in the quality list.
2908 [ - + ]: 7170515 : assert(cluster.m_quality == quality);
2909 [ - + ]: 7170515 : assert(cluster.m_setindex == setindex);
2910 : : // Count memory usage.
2911 : 7170515 : recomputed_cluster_usage += cluster.TotalMemoryUsage();
2912 : : }
2913 : : }
2914 : :
2915 : : // Verify memory usage.
2916 [ - + ]: 153112 : assert(clusterset.m_cluster_usage == recomputed_cluster_usage);
2917 : :
2918 : : // Verify that all to-be-removed transactions have valid identifiers.
2919 [ + + ]: 153120 : for (GraphIndex idx : clusterset.m_to_remove) {
2920 [ - + - + ]: 8 : assert(idx < m_entries.size());
2921 : : // We cannot assert that all m_to_remove transactions are still present: ~Ref on a
2922 : : // (P,M) transaction (present in main, inherited in staging) will cause an m_to_remove
2923 : : // addition in both main and staging, but a subsequence ApplyRemovals in main will
2924 : : // cause it to disappear from staging too, leaving the m_to_remove in place.
2925 : : }
2926 : :
2927 : : // Verify that all to-be-added dependencies have valid identifiers.
2928 [ - + + + ]: 344528 : for (auto [par_idx, chl_idx] : clusterset.m_deps_to_add) {
2929 [ - + ]: 191416 : assert(par_idx != chl_idx);
2930 [ - + - + ]: 191416 : assert(par_idx < m_entries.size());
2931 [ - + ]: 191416 : assert(chl_idx < m_entries.size());
2932 : : }
2933 : :
2934 : : // Verify that the actually encountered clusters match the ones occurring in Entry vector.
2935 [ - + ]: 153112 : assert(actual_clusters == expected_clusters[level]);
2936 : :
2937 : : // Verify that the contents of m_removed matches what was expected based on the Entry vector.
2938 [ + - ]: 153112 : std::set<GraphIndex> actual_removed(clusterset.m_removed.begin(), clusterset.m_removed.end());
2939 [ - + ]: 153112 : for (auto i : expected_unlinked) {
2940 : : // If a transaction exists in both main and staging, and is removed from staging (adding
2941 : : // it to m_removed there), and consequently destroyed (wiping the locator completely),
2942 : : // it can remain in m_removed despite not having an IsRemoved() locator. Exclude those
2943 : : // transactions from the comparison here.
2944 : 0 : actual_removed.erase(i);
2945 : 0 : expected_removed[level].erase(i);
2946 : : }
2947 : :
2948 [ - + ]: 153112 : assert(actual_removed == expected_removed[level]);
2949 : :
2950 : : // If any GraphIndex entries remain in this ClusterSet, compact is not possible.
2951 [ + + ]: 153112 : if (!clusterset.m_deps_to_add.empty()) compact_possible = false;
2952 [ + + ]: 153112 : if (!clusterset.m_to_remove.empty()) compact_possible = false;
2953 [ - + ]: 153112 : if (!clusterset.m_removed.empty()) compact_possible = false;
2954 : :
2955 : : // For non-top levels, m_oversized must be known (as it cannot change until the level
2956 : : // on top is gone).
2957 [ - + - - ]: 153112 : if (level < GetTopLevel()) assert(clusterset.m_oversized.has_value());
2958 : 153112 : }
2959 : :
2960 : : // Verify that the contents of m_unlinked matches what was expected based on the Entry vector.
2961 [ + - ]: 153112 : std::set<GraphIndex> actual_unlinked(m_unlinked.begin(), m_unlinked.end());
2962 [ - + ]: 153112 : assert(actual_unlinked == expected_unlinked);
2963 : :
2964 : : // If compaction was possible, it should have been performed already, and m_unlinked must be
2965 : : // empty (to prevent memory leaks due to an ever-growing m_entries vector).
2966 [ + + ]: 153112 : if (compact_possible) {
2967 [ - + ]: 153104 : assert(actual_unlinked.empty());
2968 : : }
2969 : :
2970 : : // Finally, check the chunk index.
2971 : 153112 : std::set<GraphIndex> actual_chunkindex;
2972 : 153112 : FeeFrac last_chunk_feerate;
2973 [ + + ]: 7479631 : for (const auto& chunk : m_main_chunkindex) {
2974 : 7326519 : GraphIndex idx = chunk.m_graph_index;
2975 [ + - ]: 7326519 : actual_chunkindex.insert(idx);
2976 [ + + ]: 7326519 : auto chunk_feerate = m_entries[idx].m_main_chunk_feerate;
2977 [ + + ]: 7326519 : if (!last_chunk_feerate.IsEmpty()) {
2978 [ - + ]: 7297318 : assert(FeeRateCompare(last_chunk_feerate, chunk_feerate) >= 0);
2979 : : }
2980 : 7326519 : last_chunk_feerate = chunk_feerate;
2981 : : }
2982 [ - + ]: 153112 : assert(actual_chunkindex == expected_chunkindex);
2983 [ + + + + : 1071784 : }
- - - - ]
2984 : :
2985 : 184916 : bool TxGraphImpl::DoWork(uint64_t iters) noexcept
2986 : : {
2987 : 184916 : uint64_t iters_done{0};
2988 : : // First linearize everything in NEEDS_RELINEARIZE to an acceptable level. If more budget
2989 : : // remains after that, try to make everything optimal.
2990 [ + + ]: 739664 : for (QualityLevel quality : {QualityLevel::NEEDS_FIX, QualityLevel::NEEDS_RELINEARIZE, QualityLevel::ACCEPTABLE}) {
2991 : : // First linearize staging, if it exists, then main.
2992 [ + + ]: 1109496 : for (int level = GetTopLevel(); level >= 0; --level) {
2993 : : // Do not modify main if it has any observers.
2994 [ + - - + ]: 554748 : if (level == 0 && m_main_chunkindex_observers != 0) continue;
2995 : 554748 : ApplyDependencies(level);
2996 : 554748 : auto& clusterset = GetClusterSet(level);
2997 : : // Do not modify oversized levels.
2998 [ + - ]: 554748 : if (clusterset.m_oversized == true) continue;
2999 : 554748 : auto& queue = clusterset.m_clusters[int(quality)];
3000 [ + + ]: 560210 : while (!queue.empty()) {
3001 [ + - ]: 5462 : if (iters_done >= iters) return false;
3002 : : // Randomize the order in which we process, so that if the first cluster somehow
3003 : : // needs more work than what iters allows, we don't keep spending it on the same
3004 : : // one.
3005 [ - + ]: 5462 : auto pos = m_rng.randrange<size_t>(queue.size());
3006 : 5462 : auto iters_now = iters - iters_done;
3007 [ + + ]: 5462 : if (quality == QualityLevel::NEEDS_FIX || quality == QualityLevel::NEEDS_RELINEARIZE) {
3008 : : // If we're working with clusters that need relinearization still, only perform
3009 : : // up to m_acceptable_iters iterations. If they become ACCEPTABLE, and we still
3010 : : // have budget after all other clusters are ACCEPTABLE too, we'll spend the
3011 : : // remaining budget on trying to make them OPTIMAL.
3012 [ - + ]: 5381 : iters_now = std::min(iters_now, m_acceptable_iters);
3013 : : }
3014 [ + - ]: 5462 : auto [cost, improved] = queue[pos].get()->Relinearize(*this, level, iters_now);
3015 : 5462 : iters_done += cost;
3016 : : // If no improvement was made to the Cluster, it means we've essentially run out of
3017 : : // budget. Even though it may be the case that iters_done < iters still, the
3018 : : // linearizer decided there wasn't enough budget left to attempt anything with.
3019 : : // To avoid an infinite loop that keeps trying clusters with minuscule budgets,
3020 : : // stop here too.
3021 [ + - ]: 5462 : if (!improved) return false;
3022 : : }
3023 : : }
3024 : : }
3025 : : // All possible work has been performed, so we can return true. Note that this does *not* mean
3026 : : // that all clusters are optimally linearized now. It may be that there is nothing to do left
3027 : : // because all non-optimal clusters are in oversized and/or observer-bearing levels.
3028 : : return true;
3029 : : }
3030 : :
3031 : 7173445 : void BlockBuilderImpl::Next() noexcept
3032 : : {
3033 : : // Don't do anything if we're already done.
3034 [ + - ]: 7173445 : if (m_cur_iter == m_graph->m_main_chunkindex.end()) return;
3035 : 7173458 : while (true) {
3036 : : // Advance the pointer, and stop if we reach the end.
3037 [ + + ]: 7173458 : ++m_cur_iter;
3038 : 7173458 : m_cur_cluster = nullptr;
3039 [ + + ]: 7173458 : if (m_cur_iter == m_graph->m_main_chunkindex.end()) break;
3040 : : // Find the cluster pointed to by m_cur_iter.
3041 : 7142994 : const auto& chunk_data = *m_cur_iter;
3042 : 7142994 : const auto& chunk_end_entry = m_graph->m_entries[chunk_data.m_graph_index];
3043 : 7142994 : m_cur_cluster = chunk_end_entry.m_locator[0].cluster;
3044 : 7142994 : m_known_end_of_cluster = false;
3045 : : // If we previously skipped a chunk from this cluster we cannot include more from it.
3046 [ + + ]: 7142994 : if (!m_excluded_clusters.contains(m_cur_cluster->m_sequence)) break;
3047 : : }
3048 : : }
3049 : :
3050 : 7367265 : std::optional<std::pair<std::vector<TxGraph::Ref*>, FeePerWeight>> BlockBuilderImpl::GetCurrentChunk() noexcept
3051 : : {
3052 : 7367265 : std::optional<std::pair<std::vector<TxGraph::Ref*>, FeePerWeight>> ret;
3053 : : // Populate the return value if we are not done.
3054 [ + + ]: 7367265 : if (m_cur_iter != m_graph->m_main_chunkindex.end()) {
3055 : 7173491 : ret.emplace();
3056 [ + + ]: 7173491 : const auto& chunk_data = *m_cur_iter;
3057 [ + + ]: 7173491 : const auto& chunk_end_entry = m_graph->m_entries[chunk_data.m_graph_index];
3058 [ + + ]: 7173491 : if (chunk_data.m_chunk_count == LinearizationIndex(-1)) {
3059 : : // Special case in case just a single transaction remains, avoiding the need to
3060 : : // dispatch to and dereference Cluster.
3061 : 7005361 : ret->first.resize(1);
3062 : 7005361 : Assume(chunk_end_entry.m_ref != nullptr);
3063 : 7005361 : ret->first[0] = chunk_end_entry.m_ref;
3064 : 7005361 : m_known_end_of_cluster = true;
3065 : : } else {
3066 : 168130 : Assume(m_cur_cluster);
3067 : 168130 : ret->first.resize(chunk_data.m_chunk_count);
3068 : 168130 : auto start_pos = chunk_end_entry.m_main_lin_index + 1 - chunk_data.m_chunk_count;
3069 [ - + ]: 168130 : m_known_end_of_cluster = m_cur_cluster->GetClusterRefs(*m_graph, ret->first, start_pos);
3070 : : // If the chunk size was 1 and at end of cluster, then the special case above should
3071 : : // have been used.
3072 : 168130 : Assume(!m_known_end_of_cluster || chunk_data.m_chunk_count > 1);
3073 : : }
3074 : 7173491 : ret->second = chunk_end_entry.m_main_chunk_feerate;
3075 : : }
3076 : 7367265 : return ret;
3077 : : }
3078 : :
3079 : 193822 : BlockBuilderImpl::BlockBuilderImpl(TxGraphImpl& graph) noexcept : m_graph(&graph)
3080 : : {
3081 : : // Make sure all clusters in main are up to date, and acceptable.
3082 : 193822 : m_graph->MakeAllAcceptable(0);
3083 : : // There cannot remain any inapplicable dependencies (only possible if main is oversized).
3084 [ + + ]: 193822 : Assume(m_graph->m_main_clusterset.m_deps_to_add.empty());
3085 : : // Remember that this object is observing the graph's index, so that we can detect concurrent
3086 : : // modifications.
3087 : 193822 : ++m_graph->m_main_chunkindex_observers;
3088 : : // Find the first chunk.
3089 [ + + ]: 193822 : m_cur_iter = m_graph->m_main_chunkindex.begin();
3090 : 193822 : m_cur_cluster = nullptr;
3091 [ + + ]: 193822 : if (m_cur_iter != m_graph->m_main_chunkindex.end()) {
3092 : : // Find the cluster pointed to by m_cur_iter.
3093 : 30512 : const auto& chunk_data = *m_cur_iter;
3094 : 30512 : const auto& chunk_end_entry = m_graph->m_entries[chunk_data.m_graph_index];
3095 : 30512 : m_cur_cluster = chunk_end_entry.m_locator[0].cluster;
3096 : : }
3097 : 193822 : }
3098 : :
3099 : 387644 : BlockBuilderImpl::~BlockBuilderImpl()
3100 : : {
3101 : 193822 : Assume(m_graph->m_main_chunkindex_observers > 0);
3102 : : // Permit modifications to the main graph again after destroying the BlockBuilderImpl.
3103 : 193822 : --m_graph->m_main_chunkindex_observers;
3104 : 387644 : }
3105 : :
3106 : 7142948 : void BlockBuilderImpl::Include() noexcept
3107 : : {
3108 : : // The actual inclusion of the chunk is done by the calling code. All we have to do is switch
3109 : : // to the next chunk.
3110 : 7142948 : Next();
3111 : 7142948 : }
3112 : :
3113 : 30497 : void BlockBuilderImpl::Skip() noexcept
3114 : : {
3115 : : // When skipping a chunk we need to not include anything more of the cluster, as that could make
3116 : : // the result topologically invalid. However, don't do this if the chunk is known to be the last
3117 : : // chunk of the cluster. This may significantly reduce the size of m_excluded_clusters,
3118 : : // especially when many singleton clusters are ignored.
3119 [ + - + + ]: 30497 : if (m_cur_cluster != nullptr && !m_known_end_of_cluster) {
3120 : 1 : m_excluded_clusters.insert(m_cur_cluster->m_sequence);
3121 : : }
3122 : 30497 : Next();
3123 : 30497 : }
3124 : :
3125 : 193822 : std::unique_ptr<TxGraph::BlockBuilder> TxGraphImpl::GetBlockBuilder() noexcept
3126 : : {
3127 [ - + ]: 193822 : return std::make_unique<BlockBuilderImpl>(*this);
3128 : : }
3129 : :
3130 : 39 : std::pair<std::vector<TxGraph::Ref*>, FeePerWeight> TxGraphImpl::GetWorstMainChunk() noexcept
3131 : : {
3132 : 39 : std::pair<std::vector<Ref*>, FeePerWeight> ret;
3133 : : // Make sure all clusters in main are up to date, and acceptable.
3134 : 39 : MakeAllAcceptable(0);
3135 [ + - ]: 39 : Assume(m_main_clusterset.m_deps_to_add.empty());
3136 : : // If the graph is not empty, populate ret.
3137 [ + - ]: 39 : if (!m_main_chunkindex.empty()) {
3138 [ + + ]: 39 : const auto& chunk_data = *m_main_chunkindex.rbegin();
3139 [ + + ]: 39 : const auto& chunk_end_entry = m_entries[chunk_data.m_graph_index];
3140 : 39 : Cluster* cluster = chunk_end_entry.m_locator[0].cluster;
3141 [ + + ]: 39 : if (chunk_data.m_chunk_count == LinearizationIndex(-1) || chunk_data.m_chunk_count == 1) {
3142 : : // Special case for singletons.
3143 : 35 : ret.first.resize(1);
3144 : 35 : Assume(chunk_end_entry.m_ref != nullptr);
3145 : 35 : ret.first[0] = chunk_end_entry.m_ref;
3146 : : } else {
3147 : 4 : ret.first.resize(chunk_data.m_chunk_count);
3148 : 4 : auto start_pos = chunk_end_entry.m_main_lin_index + 1 - chunk_data.m_chunk_count;
3149 [ - + ]: 4 : cluster->GetClusterRefs(*this, ret.first, start_pos);
3150 : 4 : std::reverse(ret.first.begin(), ret.first.end());
3151 : : }
3152 : 39 : ret.second = chunk_end_entry.m_main_chunk_feerate;
3153 : : }
3154 : 39 : return ret;
3155 : : }
3156 : :
3157 : 3244 : std::vector<TxGraph::Ref*> TxGraphImpl::Trim() noexcept
3158 : : {
3159 [ + + ]: 3244 : int level = GetTopLevel();
3160 [ + + ]: 3244 : Assume(m_main_chunkindex_observers == 0 || level != 0);
3161 : 3244 : std::vector<TxGraph::Ref*> ret;
3162 : :
3163 : : // Compute the groups of to-be-merged Clusters (which also applies all removals, and splits).
3164 : 3244 : auto& clusterset = GetClusterSet(level);
3165 [ + + ]: 3244 : if (clusterset.m_oversized == false) return ret;
3166 : 27 : GroupClusters(level);
3167 [ + - ]: 27 : Assume(clusterset.m_group_data.has_value());
3168 : : // Nothing to do if not oversized.
3169 : 27 : Assume(clusterset.m_oversized.has_value());
3170 [ + - ]: 27 : if (clusterset.m_oversized == false) return ret;
3171 : :
3172 : : // In this function, would-be clusters (as precomputed in m_group_data by GroupClusters) are
3173 : : // trimmed by removing transactions in them such that the resulting clusters satisfy the size
3174 : : // and count limits.
3175 : : //
3176 : : // It works by defining for each would-be cluster a rudimentary linearization: at every point
3177 : : // the highest-chunk-feerate remaining transaction is picked among those with no unmet
3178 : : // dependencies. "Dependency" here means either a to-be-added dependency (m_deps_to_add), or
3179 : : // an implicit dependency added between any two consecutive transaction in their current
3180 : : // cluster linearization. So it can be seen as a "merge sort" of the chunks of the clusters,
3181 : : // but respecting the dependencies being added.
3182 : : //
3183 : : // This rudimentary linearization is computed lazily, by putting all eligible (no unmet
3184 : : // dependencies) transactions in a heap, and popping the highest-feerate one from it. Along the
3185 : : // way, the counts and sizes of the would-be clusters up to that point are tracked (by
3186 : : // partitioning the involved transactions using a union-find structure). Any transaction whose
3187 : : // addition would cause a violation is removed, along with all their descendants.
3188 : : //
3189 : : // A next invocation of GroupClusters (after applying the removals) will compute the new
3190 : : // resulting clusters, and none of them will violate the limits.
3191 : :
3192 : : /** All dependencies (both to be added ones, and implicit ones between consecutive transactions
3193 : : * in existing cluster linearizations), sorted by parent. */
3194 : 4 : std::vector<std::pair<GraphIndex, GraphIndex>> deps_by_parent;
3195 : : /** Same, but sorted by child. */
3196 : 4 : std::vector<std::pair<GraphIndex, GraphIndex>> deps_by_child;
3197 : : /** Information about all transactions involved in a Cluster group to be trimmed, sorted by
3198 : : * GraphIndex. It contains entries both for transactions that have already been included,
3199 : : * and ones that have not yet been. */
3200 : 4 : std::vector<TrimTxData> trim_data;
3201 : : /** Iterators into trim_data, treated as a max heap according to cmp_fn below. Each entry is
3202 : : * a transaction that has not yet been included yet, but all its ancestors have. */
3203 : 4 : std::vector<std::vector<TrimTxData>::iterator> trim_heap;
3204 : : /** The list of representatives of the partitions a given transaction depends on. */
3205 : 4 : std::vector<TrimTxData*> current_deps;
3206 : :
3207 : : /** Function to define the ordering of trim_heap. */
3208 : 1078442 : static constexpr auto cmp_fn = [](auto a, auto b) noexcept {
3209 : : // Sort by increasing chunk feerate, and then by decreasing size.
3210 : : // We do not need to sort by cluster or within clusters, because due to the implicit
3211 : : // dependency between consecutive linearization elements, no two transactions from the
3212 : : // same Cluster will ever simultaneously be in the heap.
3213 : 1078438 : return a->m_chunk_feerate < b->m_chunk_feerate;
3214 : : };
3215 : :
3216 : : /** Given a TrimTxData entry, find the representative of the partition it is in. */
3217 : 127308 : static constexpr auto find_fn = [](TrimTxData* arg) noexcept {
3218 [ + + - + ]: 189351 : while (arg != arg->m_uf_parent) {
3219 : : // Replace pointer to parent with pointer to grandparent (path splitting).
3220 : : // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Finding_set_representatives.
3221 : 62047 : auto par = arg->m_uf_parent;
3222 : 62047 : arg->m_uf_parent = par->m_uf_parent;
3223 : 62047 : arg = par;
3224 : : }
3225 : 127304 : return arg;
3226 : : };
3227 : :
3228 : : /** Given two TrimTxData entries, union the partitions they are in, and return the
3229 : : * representative. */
3230 : 63100 : static constexpr auto union_fn = [](TrimTxData* arg1, TrimTxData* arg2) noexcept {
3231 : : // Replace arg1 and arg2 by their representatives.
3232 [ - + ]: 126192 : auto rep1 = find_fn(arg1);
3233 : 63096 : auto rep2 = find_fn(arg2);
3234 : : // Bail out if both representatives are the same, because that means arg1 and arg2 are in
3235 : : // the same partition already.
3236 [ + - ]: 63096 : if (rep1 == rep2) return rep1;
3237 : : // Pick the lower-count root to become a child of the higher-count one.
3238 : : // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Union_by_size.
3239 [ + + ]: 63096 : if (rep1->m_uf_count < rep2->m_uf_count) std::swap(rep1, rep2);
3240 : 63096 : rep2->m_uf_parent = rep1;
3241 : : // Add the statistics of arg2 (which is no longer a representative) to those of arg1 (which
3242 : : // is now the representative for both).
3243 : 63096 : rep1->m_uf_size += rep2->m_uf_size;
3244 : 63096 : rep1->m_uf_count += rep2->m_uf_count;
3245 : 63096 : return rep1;
3246 : : };
3247 : :
3248 : : /** Get iterator to TrimTxData entry for a given index. */
3249 : 128420 : auto locate_fn = [&](GraphIndex index) noexcept {
3250 : 128416 : auto it = std::lower_bound(trim_data.begin(), trim_data.end(), index, [](TrimTxData& elem, GraphIndex idx) noexcept {
3251 [ + + ]: 2047999 : return elem.m_index < idx;
3252 : : });
3253 [ + - ]: 128416 : Assume(it != trim_data.end() && it->m_index == index);
3254 : 128416 : return it;
3255 : 4 : };
3256 : :
3257 : : // For each group of to-be-merged Clusters.
3258 [ + + ]: 13 : for (const auto& group_data : clusterset.m_group_data->m_groups) {
3259 [ + + ]: 9 : trim_data.clear();
3260 [ - + ]: 9 : trim_heap.clear();
3261 [ - + ]: 9 : deps_by_child.clear();
3262 [ - + ]: 9 : deps_by_parent.clear();
3263 : :
3264 : : // Gather trim data and implicit dependency data from all involved Clusters.
3265 [ - + ]: 9 : auto cluster_span = std::span{clusterset.m_group_data->m_group_clusters}
3266 : 9 : .subspan(group_data.m_cluster_offset, group_data.m_cluster_count);
3267 : 9 : uint64_t size{0};
3268 [ + + ]: 64226 : for (Cluster* cluster : cluster_span) {
3269 : 64217 : MakeAcceptable(*cluster, cluster->GetLevel(*this));
3270 : 64217 : size += cluster->AppendTrimData(trim_data, deps_by_child);
3271 : : }
3272 : : // If this group of Clusters does not violate any limits, continue to the next group.
3273 [ - + + + : 9 : if (trim_data.size() <= m_max_cluster_count && size <= m_max_cluster_size) continue;
+ - ]
3274 : : // Sort the trim data by GraphIndex. In what follows, we will treat this sorted vector as
3275 : : // a map from GraphIndex to TrimTxData via locate_fn, and its ordering will not change
3276 : : // anymore.
3277 [ + + + + : 1315115 : std::sort(trim_data.begin(), trim_data.end(), [](auto& a, auto& b) noexcept { return a.m_index < b.m_index; });
+ + + + +
+ + + + +
+ + + + +
+ - - +
+ ]
3278 : :
3279 : : // Add the explicitly added dependencies to deps_by_child.
3280 : 9 : deps_by_child.insert(deps_by_child.end(),
3281 : 9 : clusterset.m_deps_to_add.begin() + group_data.m_deps_offset,
3282 : 9 : clusterset.m_deps_to_add.begin() + group_data.m_deps_offset + group_data.m_deps_count);
3283 : :
3284 : : // Sort deps_by_child by child transaction GraphIndex. The order will not be changed
3285 : : // anymore after this.
3286 [ + + + + : 1453049 : std::sort(deps_by_child.begin(), deps_by_child.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
+ + + + +
+ + + + +
+ + + + +
+ - - +
+ ]
3287 : : // Fill m_parents_count and m_parents_offset in trim_data, as well as m_deps_left, and
3288 : : // initially populate trim_heap. Because of the sort above, all dependencies involving the
3289 : : // same child are grouped together, so a single linear scan suffices.
3290 : 9 : auto deps_it = deps_by_child.begin();
3291 [ + + ]: 64226 : for (auto trim_it = trim_data.begin(); trim_it != trim_data.end(); ++trim_it) {
3292 : 64217 : trim_it->m_parent_offset = deps_it - deps_by_child.begin();
3293 : 64217 : trim_it->m_deps_left = 0;
3294 [ + + + + ]: 128425 : while (deps_it != deps_by_child.end() && deps_it->second == trim_it->m_index) {
3295 : 64208 : ++trim_it->m_deps_left;
3296 : 64208 : ++deps_it;
3297 : : }
3298 : 64217 : trim_it->m_parent_count = trim_it->m_deps_left;
3299 : : // If this transaction has no unmet dependencies, and is not oversized, add it to the
3300 : : // heap (just append for now, the heapification happens below).
3301 [ + + + + ]: 64217 : if (trim_it->m_deps_left == 0 && trim_it->m_tx_size <= m_max_cluster_size) {
3302 : 1150 : trim_heap.push_back(trim_it);
3303 : : }
3304 : : }
3305 : 9 : Assume(deps_it == deps_by_child.end());
3306 : :
3307 : : // Construct deps_by_parent, sorted by parent transaction GraphIndex. The order will not be
3308 : : // changed anymore after this.
3309 : 9 : deps_by_parent = deps_by_child;
3310 [ + + + + : 1999054 : std::sort(deps_by_parent.begin(), deps_by_parent.end(), [](auto& a, auto& b) noexcept { return a.first < b.first; });
+ + + + +
+ + + + +
+ + + + +
+ - - +
+ ]
3311 : : // Fill m_children_offset and m_children_count in trim_data. Because of the sort above, all
3312 : : // dependencies involving the same parent are grouped together, so a single linear scan
3313 : : // suffices.
3314 : 9 : deps_it = deps_by_parent.begin();
3315 [ + + ]: 64226 : for (auto& trim_entry : trim_data) {
3316 : 64217 : trim_entry.m_children_count = 0;
3317 : 64217 : trim_entry.m_children_offset = deps_it - deps_by_parent.begin();
3318 [ + + + + ]: 128425 : while (deps_it != deps_by_parent.end() && deps_it->first == trim_entry.m_index) {
3319 : 64208 : ++trim_entry.m_children_count;
3320 : 64208 : ++deps_it;
3321 : : }
3322 : : }
3323 : 9 : Assume(deps_it == deps_by_parent.end());
3324 : :
3325 : : // Build a heap of all transactions with 0 unmet dependencies.
3326 : 9 : std::make_heap(trim_heap.begin(), trim_heap.end(), cmp_fn);
3327 : :
3328 : : // Iterate over to-be-included transactions, and convert them to included transactions, or
3329 : : // skip them if adding them would violate resource limits of the would-be cluster.
3330 : : //
3331 : : // It is possible that the heap empties without ever hitting either cluster limit, in case
3332 : : // the implied graph (to be added dependencies plus implicit dependency between each
3333 : : // original transaction and its predecessor in the linearization it came from) contains
3334 : : // cycles. Such cycles will be removed entirely, because each of the transactions in the
3335 : : // cycle permanently have unmet dependencies. However, this cannot occur in real scenarios
3336 : : // where Trim() is called to deal with reorganizations that would violate cluster limits,
3337 : : // as all added dependencies are in the same direction (from old mempool transactions to
3338 : : // new from-block transactions); cycles require dependencies in both directions to be
3339 : : // added.
3340 [ + + ]: 64229 : while (!trim_heap.empty()) {
3341 : : // Move the best remaining transaction to the end of trim_heap.
3342 : 64211 : std::pop_heap(trim_heap.begin(), trim_heap.end(), cmp_fn);
3343 : : // Pop it, and find its TrimTxData.
3344 [ + + ]: 64211 : auto& entry = *trim_heap.back();
3345 [ + + ]: 64211 : trim_heap.pop_back();
3346 : :
3347 : : // Initialize it as a singleton partition.
3348 : 64211 : entry.m_uf_parent = &entry;
3349 : 64211 : entry.m_uf_count = 1;
3350 : 64211 : entry.m_uf_size = entry.m_tx_size;
3351 : :
3352 : : // Find the distinct transaction partitions this entry depends on.
3353 [ + + ]: 64211 : current_deps.clear();
3354 [ - + + + ]: 128419 : for (auto& [par, chl] : std::span{deps_by_child}.subspan(entry.m_parent_offset, entry.m_parent_count)) {
3355 : 64208 : Assume(chl == entry.m_index);
3356 : 128416 : current_deps.push_back(find_fn(&*locate_fn(par)));
3357 : : }
3358 : 64211 : std::sort(current_deps.begin(), current_deps.end());
3359 : 64211 : current_deps.erase(std::unique(current_deps.begin(), current_deps.end()), current_deps.end());
3360 : :
3361 : : // Compute resource counts.
3362 : 64211 : uint32_t new_count = 1;
3363 : 64211 : uint64_t new_size = entry.m_tx_size;
3364 [ + + ]: 128419 : for (TrimTxData* ptr : current_deps) {
3365 : 64208 : new_count += ptr->m_uf_count;
3366 : 64208 : new_size += ptr->m_uf_size;
3367 : : }
3368 : : // Skip the entry if this would violate any limit.
3369 [ + + - + ]: 64211 : if (new_count > m_max_cluster_count || new_size > m_max_cluster_size) continue;
3370 : :
3371 : : // Union the partitions this transaction and all its dependencies are in together.
3372 : 64198 : auto rep = &entry;
3373 [ + + ]: 127294 : for (TrimTxData* ptr : current_deps) rep = union_fn(ptr, rep);
3374 : : // Mark the entry as included (so the loop below will not remove the transaction).
3375 : 64198 : entry.m_deps_left = uint32_t(-1);
3376 : : // Mark each to-be-added dependency involving this transaction as parent satisfied.
3377 [ - + + + ]: 128406 : for (auto& [par, chl] : std::span{deps_by_parent}.subspan(entry.m_children_offset, entry.m_children_count)) {
3378 : 64208 : Assume(par == entry.m_index);
3379 : 64208 : auto chl_it = locate_fn(chl);
3380 : : // Reduce the number of unmet dependencies of chl_it, and if that brings the number
3381 : : // to zero, add it to the heap of includable transactions.
3382 [ + + ]: 64208 : Assume(chl_it->m_deps_left > 0);
3383 [ + + ]: 64208 : if (--chl_it->m_deps_left == 0) {
3384 : 63061 : trim_heap.push_back(chl_it);
3385 : 63061 : std::push_heap(trim_heap.begin(), trim_heap.end(), cmp_fn);
3386 : : }
3387 : : }
3388 : : }
3389 : :
3390 : : // Remove all the transactions that were not processed above. Because nothing gets
3391 : : // processed until/unless all its dependencies are met, this automatically guarantees
3392 : : // that if a transaction is removed, all its descendants, or would-be descendants, are
3393 : : // removed as well.
3394 [ + + ]: 64226 : for (const auto& trim_entry : trim_data) {
3395 [ + + ]: 64217 : if (trim_entry.m_deps_left != uint32_t(-1)) {
3396 : 19 : ret.push_back(m_entries[trim_entry.m_index].m_ref);
3397 : 19 : clusterset.m_to_remove.push_back(trim_entry.m_index);
3398 : : }
3399 : : }
3400 : : }
3401 [ + - ]: 4 : clusterset.m_group_data.reset();
3402 : 4 : clusterset.m_oversized = false;
3403 : 4 : Assume(!ret.empty());
3404 : 4 : return ret;
3405 : 4 : }
3406 : :
3407 : 532699 : size_t TxGraphImpl::GetMainMemoryUsage() noexcept
3408 : : {
3409 : : // Make sure splits/merges are applied, as memory usage may not be representative otherwise.
3410 : 532699 : SplitAll(/*up_to_level=*/0);
3411 : 532699 : ApplyDependencies(/*level=*/0);
3412 : : // Compute memory usage
3413 : 532699 : size_t usage = /* From clusters */
3414 : 532699 : m_main_clusterset.m_cluster_usage +
3415 : : /* From Entry objects. */
3416 : 532699 : sizeof(Entry) * m_main_clusterset.m_txcount +
3417 : : /* From the chunk index. */
3418 : 532699 : memusage::DynamicUsage(m_main_chunkindex);
3419 : 532699 : return usage;
3420 : : }
3421 : :
3422 : : } // namespace
3423 : :
3424 : 369292 : TxGraph::Ref::~Ref()
3425 : : {
3426 [ + + ]: 369292 : if (m_graph) {
3427 : : // Inform the TxGraph about the Ref being destroyed.
3428 : 61263 : m_graph->UnlinkRef(m_index);
3429 : 61263 : m_graph = nullptr;
3430 : : }
3431 : 369292 : }
3432 : :
3433 : 217421 : TxGraph::Ref::Ref(Ref&& other) noexcept
3434 : : {
3435 : : // Inform the TxGraph of other that its Ref is being moved.
3436 [ + + ]: 217421 : if (other.m_graph) other.m_graph->UpdateRef(other.m_index, *this);
3437 : : // Actually move the contents.
3438 : 217421 : std::swap(m_graph, other.m_graph);
3439 : 217421 : std::swap(m_index, other.m_index);
3440 : 217421 : }
3441 : :
3442 : 1253 : std::unique_ptr<TxGraph> MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept
3443 : : {
3444 [ - + ]: 1253 : return std::make_unique<TxGraphImpl>(max_cluster_count, max_cluster_size, acceptable_iters);
3445 : : }
|