Skip to content

Commit cfe9958

Browse files
committed
txgraph: track amount of work done in linearization (preparation)
1 parent 6ba316e commit cfe9958

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/bench/cluster_linearize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ void BenchLinearizeOptimally(benchmark::Bench& bench, const std::array<uint8_t,
229229
reader >> Using<DepGraphFormatter>(depgraph);
230230
uint64_t rng_seed = 0;
231231
bench.run([&] {
232-
auto res = Linearize(depgraph, /*max_iterations=*/10000000, rng_seed++);
233-
assert(res.second);
232+
auto [_lin, optimal, _cost] = Linearize(depgraph, /*max_iterations=*/10000000, rng_seed++);
233+
assert(optimal);
234234
});
235235
};
236236

src/cluster_linearize.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,19 +1030,20 @@ class SearchCandidateFinder
10301030
* linearize.
10311031
* @param[in] old_linearization An existing linearization for the cluster (which must be
10321032
* topologically valid), or empty.
1033-
* @return A pair of:
1033+
* @return A tuple of:
10341034
* - The resulting linearization. It is guaranteed to be at least as
10351035
* good (in the feerate diagram sense) as old_linearization.
10361036
* - A boolean indicating whether the result is guaranteed to be
10371037
* optimal.
1038+
* - How many optimization steps were actually performed.
10381039
*
10391040
* Complexity: possibly O(N * min(max_iterations + N, sqrt(2^N))) where N=depgraph.TxCount().
10401041
*/
10411042
template<typename SetType>
1042-
std::pair<std::vector<DepGraphIndex>, bool> Linearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations, uint64_t rng_seed, std::span<const DepGraphIndex> old_linearization = {}) noexcept
1043+
std::tuple<std::vector<DepGraphIndex>, bool, uint64_t> Linearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations, uint64_t rng_seed, std::span<const DepGraphIndex> old_linearization = {}) noexcept
10431044
{
10441045
Assume(old_linearization.empty() || old_linearization.size() == depgraph.TxCount());
1045-
if (depgraph.TxCount() == 0) return {{}, true};
1046+
if (depgraph.TxCount() == 0) return {{}, true, 0};
10461047

10471048
uint64_t iterations_left = max_iterations;
10481049
std::vector<DepGraphIndex> linearization;
@@ -1113,7 +1114,7 @@ std::pair<std::vector<DepGraphIndex>, bool> Linearize(const DepGraph<SetType>& d
11131114
}
11141115
}
11151116

1116-
return {std::move(linearization), optimal};
1117+
return {std::move(linearization), optimal, max_iterations - iterations_left};
11171118
}
11181119

11191120
/** Improve a given linearization.

src/test/fuzz/cluster_linearize.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,8 @@ FUZZ_TARGET(clusterlin_linearize)
11541154

11551155
// Invoke Linearize().
11561156
iter_count &= 0x7ffff;
1157-
auto [linearization, optimal] = Linearize(depgraph, iter_count, rng_seed, old_linearization);
1157+
auto [linearization, optimal, cost] = Linearize(depgraph, iter_count, rng_seed, old_linearization);
1158+
assert(cost <= iter_count);
11581159
SanityCheck(depgraph, linearization);
11591160
auto chunking = ChunkLinearization(depgraph, linearization);
11601161

@@ -1322,7 +1323,7 @@ FUZZ_TARGET(clusterlin_postlinearize_tree)
13221323

13231324
// Try to find an even better linearization directly. This must not change the diagram for the
13241325
// same reason.
1325-
auto [opt_linearization, _optimal] = Linearize(depgraph_tree, 100000, rng_seed, post_linearization);
1326+
auto [opt_linearization, _optimal, _cost] = Linearize(depgraph_tree, 100000, rng_seed, post_linearization);
13261327
auto opt_chunking = ChunkLinearization(depgraph_tree, opt_linearization);
13271328
auto cmp_opt = CompareChunks(opt_chunking, post_chunking);
13281329
assert(cmp_opt == 0);

src/txgraph.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ class Cluster
189189
void Merge(TxGraphImpl& graph, Cluster& cluster) noexcept;
190190
/** Given a span of (parent, child) pairs that all belong to this Cluster, apply them. */
191191
void ApplyDependencies(TxGraphImpl& graph, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept;
192-
/** Improve the linearization of this Cluster. */
193-
void Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept;
192+
/** Improve the linearization of this Cluster. Returns how much work was performed. */
193+
uint64_t Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept;
194194
/** For every chunk in the cluster, append its FeeFrac to ret. */
195195
void AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept;
196196
/** Add a TrimTxData entry (filling m_chunk_feerate, m_index, m_tx_size) for every
@@ -1651,15 +1651,15 @@ void TxGraphImpl::ApplyDependencies(int level) noexcept
16511651
clusterset.m_group_data = GroupData{};
16521652
}
16531653

1654-
void Cluster::Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept
1654+
uint64_t Cluster::Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept
16551655
{
16561656
// We can only relinearize Clusters that do not need splitting.
16571657
Assume(!NeedsSplitting());
16581658
// No work is required for Clusters which are already optimally linearized.
1659-
if (IsOptimal()) return;
1659+
if (IsOptimal()) return 0;
16601660
// Invoke the actual linearization algorithm (passing in the existing one).
16611661
uint64_t rng_seed = graph.m_rng.rand64();
1662-
auto [linearization, optimal] = Linearize(m_depgraph, max_iters, rng_seed, m_linearization);
1662+
auto [linearization, optimal, cost] = Linearize(m_depgraph, max_iters, rng_seed, m_linearization);
16631663
// Postlinearize if the result isn't optimal already. This guarantees (among other things)
16641664
// that the chunks of the resulting linearization are all connected.
16651665
if (!optimal) PostLinearize(m_depgraph, linearization);
@@ -1670,6 +1670,7 @@ void Cluster::Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept
16701670
graph.SetClusterQuality(m_level, m_quality, m_setindex, new_quality);
16711671
// Update the Entry objects.
16721672
Updated(graph);
1673+
return cost;
16731674
}
16741675

16751676
void TxGraphImpl::MakeAcceptable(Cluster& cluster) noexcept

0 commit comments

Comments
 (0)