mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
txgraph: make GroupClusters use partition numbers directly (optimization)
This commit is contained in:
parent
c72c8d5d45
commit
2835216ec0
1 changed files with 49 additions and 58 deletions
107
src/txgraph.cpp
107
src/txgraph.cpp
|
@ -1103,38 +1103,37 @@ void TxGraphImpl::GroupClusters(int level) noexcept
|
||||||
// with inefficient and/or oversized Clusters which just end up being split again anyway.
|
// with inefficient and/or oversized Clusters which just end up being split again anyway.
|
||||||
SplitAll(level);
|
SplitAll(level);
|
||||||
|
|
||||||
/** Annotated clusters: an entry for each Cluster, together with the representative for the
|
/** Annotated clusters: an entry for each Cluster, together with the sequence number for the
|
||||||
* partition it is in if known, or with nullptr if not yet known. */
|
* representative for the partition it is in (initially its own, later that of the
|
||||||
std::vector<std::pair<Cluster*, Cluster*>> an_clusters;
|
* to-be-merged group). */
|
||||||
|
std::vector<std::pair<Cluster*, uint64_t>> an_clusters;
|
||||||
/** Annotated dependencies: an entry for each m_deps_to_add entry (excluding ones that apply
|
/** Annotated dependencies: an entry for each m_deps_to_add entry (excluding ones that apply
|
||||||
* to removed transactions), together with the representative root of the partition of
|
* to removed transactions), together with the sequence number of the representative root of
|
||||||
* Clusters it applies to. */
|
* Clusters it applies to (initially that of the child Cluster, later that of the
|
||||||
std::vector<std::pair<std::pair<GraphIndex, GraphIndex>, Cluster*>> an_deps;
|
* to-be-merged group). */
|
||||||
|
std::vector<std::pair<std::pair<GraphIndex, GraphIndex>, uint64_t>> an_deps;
|
||||||
|
|
||||||
// Construct a an_clusters entry for every parent and child in the to-be-applied dependencies.
|
// Construct a an_clusters entry for every parent and child in the to-be-applied dependencies,
|
||||||
|
// and an an_deps entry for each dependency to be applied.
|
||||||
|
an_deps.reserve(clusterset.m_deps_to_add.size());
|
||||||
for (const auto& [par, chl] : clusterset.m_deps_to_add) {
|
for (const auto& [par, chl] : clusterset.m_deps_to_add) {
|
||||||
auto par_cluster = FindCluster(par, level);
|
auto par_cluster = FindCluster(par, level);
|
||||||
auto chl_cluster = FindCluster(chl, level);
|
auto chl_cluster = FindCluster(chl, level);
|
||||||
// Skip dependencies for which the parent or child transaction is removed.
|
// Skip dependencies for which the parent or child transaction is removed.
|
||||||
if (par_cluster == nullptr || chl_cluster == nullptr) continue;
|
if (par_cluster == nullptr || chl_cluster == nullptr) continue;
|
||||||
an_clusters.emplace_back(par_cluster, nullptr);
|
an_clusters.emplace_back(par_cluster, par_cluster->m_sequence);
|
||||||
// Do not include a duplicate when parent and child are identical, as it'll be removed
|
// Do not include a duplicate when parent and child are identical, as it'll be removed
|
||||||
// below anyway.
|
// below anyway.
|
||||||
if (chl_cluster != par_cluster) an_clusters.emplace_back(chl_cluster, nullptr);
|
if (chl_cluster != par_cluster) an_clusters.emplace_back(chl_cluster, chl_cluster->m_sequence);
|
||||||
|
// Add entry to an_deps, using the child sequence number.
|
||||||
|
an_deps.emplace_back(std::pair{par, chl}, chl_cluster->m_sequence);
|
||||||
}
|
}
|
||||||
// Sort and deduplicate an_clusters, so we end up with a sorted list of all involved Clusters
|
// Sort and deduplicate an_clusters, so we end up with a sorted list of all involved Clusters
|
||||||
// to which dependencies apply.
|
// to which dependencies apply.
|
||||||
std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return CompareClusters(a.first, b.first) < 0; });
|
std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
|
||||||
an_clusters.erase(std::unique(an_clusters.begin(), an_clusters.end()), an_clusters.end());
|
an_clusters.erase(std::unique(an_clusters.begin(), an_clusters.end()), an_clusters.end());
|
||||||
|
// Sort an_deps by applying the same order to the involved child cluster.
|
||||||
// Sort the dependencies by child Cluster::m_sequence.
|
std::sort(an_deps.begin(), an_deps.end(), [&](auto& a, auto& b) noexcept { return a.second < b.second; });
|
||||||
std::sort(clusterset.m_deps_to_add.begin(), clusterset.m_deps_to_add.end(), [&](auto& a, auto& b) noexcept {
|
|
||||||
auto [_a_par, a_chl] = a;
|
|
||||||
auto [_b_par, b_chl] = b;
|
|
||||||
auto a_chl_cluster = FindCluster(a_chl, level);
|
|
||||||
auto b_chl_cluster = FindCluster(b_chl, level);
|
|
||||||
return CompareClusters(a_chl_cluster, b_chl_cluster) < 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Run the union-find algorithm to to find partitions of the input Clusters which need to be
|
// Run the union-find algorithm to to find partitions of the input Clusters which need to be
|
||||||
// grouped together. See https://en.wikipedia.org/wiki/Disjoint-set_data_structure.
|
// grouped together. See https://en.wikipedia.org/wiki/Disjoint-set_data_structure.
|
||||||
|
@ -1142,8 +1141,8 @@ void TxGraphImpl::GroupClusters(int level) noexcept
|
||||||
/** Each PartitionData entry contains information about a single input Cluster. */
|
/** Each PartitionData entry contains information about a single input Cluster. */
|
||||||
struct PartitionData
|
struct PartitionData
|
||||||
{
|
{
|
||||||
/** The cluster this holds information for. */
|
/** The sequence number of the cluster this holds information for. */
|
||||||
Cluster* cluster;
|
uint64_t sequence;
|
||||||
/** All PartitionData entries belonging to the same partition are organized in a tree.
|
/** All PartitionData entries belonging to the same partition are organized in a tree.
|
||||||
* Each element points to its parent, or to itself if it is the root. The root is then
|
* Each element points to its parent, or to itself if it is the root. The root is then
|
||||||
* a representative for the entire tree, and can be found by walking upwards from any
|
* a representative for the entire tree, and can be found by walking upwards from any
|
||||||
|
@ -1157,11 +1156,11 @@ void TxGraphImpl::GroupClusters(int level) noexcept
|
||||||
std::vector<PartitionData> partition_data;
|
std::vector<PartitionData> partition_data;
|
||||||
|
|
||||||
/** Given a Cluster, find its corresponding PartitionData. */
|
/** Given a Cluster, find its corresponding PartitionData. */
|
||||||
auto locate_fn = [&](Cluster* arg) noexcept -> PartitionData* {
|
auto locate_fn = [&](uint64_t sequence) noexcept -> PartitionData* {
|
||||||
auto it = std::lower_bound(partition_data.begin(), partition_data.end(), arg,
|
auto it = std::lower_bound(partition_data.begin(), partition_data.end(), sequence,
|
||||||
[](auto& a, Cluster* ptr) noexcept { return CompareClusters(a.cluster, ptr) < 0; });
|
[](auto& a, uint64_t seq) noexcept { return a.sequence < seq; });
|
||||||
Assume(it != partition_data.end());
|
Assume(it != partition_data.end());
|
||||||
Assume(it->cluster == arg);
|
Assume(it->sequence == sequence);
|
||||||
return &*it;
|
return &*it;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1196,67 +1195,59 @@ void TxGraphImpl::GroupClusters(int level) noexcept
|
||||||
// Start by initializing every Cluster as its own singleton partition.
|
// Start by initializing every Cluster as its own singleton partition.
|
||||||
partition_data.resize(an_clusters.size());
|
partition_data.resize(an_clusters.size());
|
||||||
for (size_t i = 0; i < an_clusters.size(); ++i) {
|
for (size_t i = 0; i < an_clusters.size(); ++i) {
|
||||||
partition_data[i].cluster = an_clusters[i].first;
|
partition_data[i].sequence = an_clusters[i].first->m_sequence;
|
||||||
partition_data[i].parent = &partition_data[i];
|
partition_data[i].parent = &partition_data[i];
|
||||||
partition_data[i].rank = 0;
|
partition_data[i].rank = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run through all parent/child pairs in m_deps_to_add, and union the
|
// Run through all parent/child pairs in an_deps, and union the partitions their Clusters
|
||||||
// the partitions their Clusters are in.
|
// are in.
|
||||||
Cluster* last_chl_cluster{nullptr};
|
Cluster* last_chl_cluster{nullptr};
|
||||||
PartitionData* last_partition{nullptr};
|
PartitionData* last_partition{nullptr};
|
||||||
for (const auto& [par, chl] : clusterset.m_deps_to_add) {
|
for (const auto& [dep, _] : an_deps) {
|
||||||
|
auto [par, chl] = dep;
|
||||||
auto par_cluster = FindCluster(par, level);
|
auto par_cluster = FindCluster(par, level);
|
||||||
auto chl_cluster = FindCluster(chl, level);
|
auto chl_cluster = FindCluster(chl, level);
|
||||||
|
Assume(chl_cluster != nullptr && par_cluster != nullptr);
|
||||||
// Nothing to do if parent and child are in the same Cluster.
|
// Nothing to do if parent and child are in the same Cluster.
|
||||||
if (par_cluster == chl_cluster) continue;
|
if (par_cluster == chl_cluster) continue;
|
||||||
// Nothing to do if either parent or child transaction is removed already.
|
|
||||||
if (par_cluster == nullptr || chl_cluster == nullptr) continue;
|
|
||||||
Assume(par != chl);
|
Assume(par != chl);
|
||||||
if (chl_cluster == last_chl_cluster) {
|
if (chl_cluster == last_chl_cluster) {
|
||||||
// If the child Clusters is the same as the previous iteration, union with the
|
// If the child Clusters is the same as the previous iteration, union with the
|
||||||
// tree they were in, avoiding the need for another lookup. Note that m_deps_to_add
|
// tree they were in, avoiding the need for another lookup. Note that an_deps
|
||||||
// is sorted by child Cluster, so batches with the same child are expected.
|
// is sorted by child Cluster, so batches with the same child are expected.
|
||||||
last_partition = union_fn(locate_fn(par_cluster), last_partition);
|
last_partition = union_fn(locate_fn(par_cluster->m_sequence), last_partition);
|
||||||
} else {
|
} else {
|
||||||
last_chl_cluster = chl_cluster;
|
last_chl_cluster = chl_cluster;
|
||||||
last_partition = union_fn(locate_fn(par_cluster), locate_fn(chl_cluster));
|
last_partition = union_fn(locate_fn(par_cluster->m_sequence), locate_fn(chl_cluster->m_sequence));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate the an_clusters and an_deps data structures with the list of input Clusters,
|
// Update the sequence numbers in an_clusters and an_deps to be those of the partition
|
||||||
// and the input dependencies, annotated with the representative of the Cluster partition
|
// representative.
|
||||||
// it applies to.
|
auto deps_it = an_deps.begin();
|
||||||
an_deps.reserve(clusterset.m_deps_to_add.size());
|
|
||||||
auto deps_it = clusterset.m_deps_to_add.begin();
|
|
||||||
for (size_t i = 0; i < partition_data.size(); ++i) {
|
for (size_t i = 0; i < partition_data.size(); ++i) {
|
||||||
auto& data = partition_data[i];
|
auto& data = partition_data[i];
|
||||||
// Find the representative of the partition Cluster i is in, and store it with the
|
// Find the sequence of the representative of the partition Cluster i is in, and store
|
||||||
// Cluster.
|
// it with the Cluster.
|
||||||
auto rep = find_root_fn(&data)->cluster;
|
auto rep_seq = find_root_fn(&data)->sequence;
|
||||||
Assume(an_clusters[i].second == nullptr);
|
an_clusters[i].second = rep_seq;
|
||||||
an_clusters[i].second = rep;
|
|
||||||
// Find all dependencies whose child Cluster is Cluster i, and annotate them with rep.
|
// Find all dependencies whose child Cluster is Cluster i, and annotate them with rep.
|
||||||
while (deps_it != clusterset.m_deps_to_add.end()) {
|
while (deps_it != an_deps.end()) {
|
||||||
auto [par, chl] = *deps_it;
|
auto [par, chl] = deps_it->first;
|
||||||
auto chl_cluster = FindCluster(chl, level);
|
auto chl_cluster = FindCluster(chl, level);
|
||||||
if (CompareClusters(chl_cluster, data.cluster) > 0) break;
|
Assume(chl_cluster != nullptr);
|
||||||
// Skip dependencies that apply to earlier Clusters (those necessary are for
|
if (chl_cluster->m_sequence > data.sequence) break;
|
||||||
// deleted transactions, as otherwise we'd have processed them already).
|
deps_it->second = rep_seq;
|
||||||
if (chl_cluster == data.cluster) {
|
|
||||||
auto par_cluster = FindCluster(par, level);
|
|
||||||
// Also filter out dependencies applying to a removed parent.
|
|
||||||
if (par_cluster != nullptr) an_deps.emplace_back(*deps_it, rep);
|
|
||||||
}
|
|
||||||
++deps_it;
|
++deps_it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort both an_clusters and an_deps by representative of the partition they are in, grouping
|
// Sort both an_clusters and an_deps by sequence number of the representative of the
|
||||||
// all those applying to the same partition together.
|
// partition they are in, grouping all those applying to the same partition together.
|
||||||
std::sort(an_deps.begin(), an_deps.end(), [](auto& a, auto& b) noexcept { return CompareClusters(a.second, b.second) < 0; });
|
std::sort(an_deps.begin(), an_deps.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
|
||||||
std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return CompareClusters(a.second, b.second) < 0; });
|
std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
|
||||||
|
|
||||||
// Translate the resulting cluster groups to the m_group_data structure, and the dependencies
|
// Translate the resulting cluster groups to the m_group_data structure, and the dependencies
|
||||||
// back to m_deps_to_add.
|
// back to m_deps_to_add.
|
||||||
|
|
Loading…
Add table
Reference in a new issue