Skip to content

Commit abdd9d3

Browse files
committed
txgraph: Skipping end of cluster has no impact (optimization)
1 parent 604acc2 commit abdd9d3

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/txgraph.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ class Cluster
153153
* union of their descendants to output. */
154154
void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept;
155155
/** Populate range with refs for the transactions in this Cluster's linearization, from
156-
* position start_pos until start_pos+range.size()-1, inclusive. */
157-
void GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept;
156+
* position start_pos until start_pos+range.size()-1, inclusive. Returns whether that
157+
* range includes the last transaction in the linearization. */
158+
bool GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept;
158159
/** Get the individual transaction feerate of a Cluster element. */
159160
FeePerWeight GetIndividualFeerate(DepGraphIndex idx) noexcept;
160161
/** Modify the fee of a Cluster element. */
@@ -586,6 +587,8 @@ class BlockBuilderImpl final : public TxGraph::BlockBuilder
586587
/** Which cluster the current chunk belongs to, so we can exclude further transactions from it
587588
* when that chunk is skipped. */
588589
Cluster* m_cur_cluster;
590+
/** Whether we know that m_cur_iter points to the last chunk of m_cur_cluster. */
591+
bool m_known_end_of_cluster;
589592

590593
// Move m_cur_iter / m_cur_cluster to the next acceptable chunk.
591594
void Next() noexcept;
@@ -1692,7 +1695,7 @@ void Cluster::GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cl
16921695
}
16931696
}
16941697

1695-
void Cluster::GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept
1698+
bool Cluster::GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept
16961699
{
16971700
// Translate the transactions in the Cluster (in linearization order, starting at start_pos in
16981701
// the linearization) to Refs, and fill them in range.
@@ -1702,6 +1705,8 @@ void Cluster::GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range,
17021705
Assume(entry.m_ref != nullptr);
17031706
ref = entry.m_ref;
17041707
}
1708+
// Return whether start_pos has advanced to the end of the Cluster.
1709+
return start_pos == m_linearization.size();
17051710
}
17061711

17071712
FeePerWeight Cluster::GetIndividualFeerate(DepGraphIndex idx) noexcept
@@ -2338,6 +2343,7 @@ void BlockBuilderImpl::Next() noexcept
23382343
const auto& chunk_data = *m_cur_iter;
23392344
const auto& chunk_end_entry = m_graph->m_entries[chunk_data.m_graph_index];
23402345
m_cur_cluster = chunk_end_entry.m_locator[0].cluster;
2346+
m_known_end_of_cluster = false;
23412347
// If we previously skipped a chunk from this cluster we cannot include more from it.
23422348
if (!m_excluded_clusters.contains(m_cur_cluster)) break;
23432349
}
@@ -2354,7 +2360,7 @@ std::optional<std::pair<std::vector<TxGraph::Ref*>, FeePerWeight>> BlockBuilderI
23542360
ret->first.resize(chunk_data.m_chunk_count);
23552361
auto start_pos = chunk_end_entry.m_main_lin_index + 1 - chunk_data.m_chunk_count;
23562362
Assume(m_cur_cluster);
2357-
m_cur_cluster->GetClusterRefs(*m_graph, ret->first, start_pos);
2363+
m_known_end_of_cluster = m_cur_cluster->GetClusterRefs(*m_graph, ret->first, start_pos);
23582364
ret->second = chunk_end_entry.m_main_chunk_feerate;
23592365
}
23602366
return ret;
@@ -2397,8 +2403,12 @@ void BlockBuilderImpl::Include() noexcept
23972403
void BlockBuilderImpl::Skip() noexcept
23982404
{
23992405
// When skipping a chunk we need to not include anything more of the cluster, as that could make
2400-
// the result topologically invalid.
2401-
if (m_cur_cluster != nullptr) m_excluded_clusters.insert(m_cur_cluster);
2406+
// the result topologically invalid. However, don't do this if the chunk is known to be the last
2407+
// chunk of the cluster. This may significantly reduce the size of m_excluded_clusters,
2408+
// especially when many singleton clusters are ignored.
2409+
if (m_cur_cluster != nullptr && !m_known_end_of_cluster) {
2410+
m_excluded_clusters.insert(m_cur_cluster);
2411+
}
24022412
Next();
24032413
}
24042414

0 commit comments

Comments
 (0)