txgraph: add work limit to DoWork(), try optimal (feature)

This adds an `iters` parameter to DoWork(), which controls how much work it is
allowed to do right now.

Additionally, DoWork() won't stop at just getting everything ACCEPTABLE, but if
there is work budget left, will also attempt to get every cluster linearized
optimally.
This commit is contained in:
Pieter Wuille
2025-04-13 17:16:27 -04:00
parent e96b00d99e
commit f3c2fc867f
3 changed files with 90 additions and 16 deletions

View File

@@ -189,8 +189,9 @@ public:
void Merge(TxGraphImpl& graph, Cluster& cluster) noexcept;
/** Given a span of (parent, child) pairs that all belong to this Cluster, apply them. */
void ApplyDependencies(TxGraphImpl& graph, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept;
/** Improve the linearization of this Cluster. Returns how much work was performed. */
uint64_t Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept;
/** Improve the linearization of this Cluster. Returns how much work was performed and whether
* the Cluster's QualityLevel improved as a result. */
std::pair<uint64_t, bool> Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept;
/** For every chunk in the cluster, append its FeeFrac to ret. */
void AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept;
/** Add a TrimTxData entry (filling m_chunk_feerate, m_index, m_tx_size) for every
@@ -592,7 +593,7 @@ public:
void AddDependency(const Ref& parent, const Ref& child) noexcept final;
void SetTransactionFee(const Ref&, int64_t fee) noexcept final;
void DoWork() noexcept final;
bool DoWork(uint64_t iters) noexcept final;
void StartStaging() noexcept final;
void CommitStaging() noexcept final;
@@ -1655,12 +1656,12 @@ void TxGraphImpl::ApplyDependencies(int level) noexcept
clusterset.m_group_data = GroupData{};
}
uint64_t Cluster::Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept
std::pair<uint64_t, bool> Cluster::Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept
{
// We can only relinearize Clusters that do not need splitting.
Assume(!NeedsSplitting());
// No work is required for Clusters which are already optimally linearized.
if (IsOptimal()) return 0;
if (IsOptimal()) return {0, false};
// Invoke the actual linearization algorithm (passing in the existing one).
uint64_t rng_seed = graph.m_rng.rand64();
auto [linearization, optimal, cost] = Linearize(m_depgraph, max_iters, rng_seed, m_linearization);
@@ -1670,11 +1671,17 @@ uint64_t Cluster::Relinearize(TxGraphImpl& graph, uint64_t max_iters) noexcept
// Update the linearization.
m_linearization = std::move(linearization);
// Update the Cluster's quality.
auto new_quality = optimal ? QualityLevel::OPTIMAL : QualityLevel::ACCEPTABLE;
graph.SetClusterQuality(m_level, m_quality, m_setindex, new_quality);
bool improved = false;
if (optimal) {
graph.SetClusterQuality(m_level, m_quality, m_setindex, QualityLevel::OPTIMAL);
improved = true;
} else if (max_iters >= graph.m_acceptable_iters && !IsAcceptable()) {
graph.SetClusterQuality(m_level, m_quality, m_setindex, QualityLevel::ACCEPTABLE);
improved = true;
}
// Update the Entry objects.
Updated(graph);
return cost;
return {cost, improved};
}
void TxGraphImpl::MakeAcceptable(Cluster& cluster) noexcept
@@ -2478,13 +2485,50 @@ void TxGraphImpl::SanityCheck() const
assert(actual_chunkindex == expected_chunkindex);
}
void TxGraphImpl::DoWork() noexcept
bool TxGraphImpl::DoWork(uint64_t iters) noexcept
{
for (int level = 0; level <= GetTopLevel(); ++level) {
if (level > 0 || m_main_chunkindex_observers == 0) {
MakeAllAcceptable(level);
uint64_t iters_done{0};
// First linearize everything in NEEDS_RELINEARIZE to an acceptable level. If more budget
// remains after that, try to make everything optimal.
for (QualityLevel quality : {QualityLevel::NEEDS_RELINEARIZE, QualityLevel::ACCEPTABLE}) {
// First linearize staging, if it exists, then main.
for (int level = GetTopLevel(); level >= 0; --level) {
// Do not modify main if it has any observers.
if (level == 0 && m_main_chunkindex_observers != 0) continue;
ApplyDependencies(level);
auto& clusterset = GetClusterSet(level);
// Do not modify oversized levels.
if (clusterset.m_oversized == true) continue;
auto& queue = clusterset.m_clusters[int(quality)];
while (!queue.empty()) {
if (iters_done >= iters) return false;
// Randomize the order in which we process, so that if the first cluster somehow
// needs more work than what iters allows, we don't keep spending it on the same
// one.
auto pos = m_rng.randrange<size_t>(queue.size());
auto iters_now = iters - iters_done;
if (quality == QualityLevel::NEEDS_RELINEARIZE) {
// If we're working with clusters that need relinearization still, only perform
// up to m_acceptable_iters iterations. If they become ACCEPTABLE, and we still
// have budget after all other clusters are ACCEPTABLE too, we'll spend the
// remaining budget on trying to make them OPTIMAL.
iters_now = std::min(iters_now, m_acceptable_iters);
}
auto [cost, improved] = queue[pos].get()->Relinearize(*this, iters_now);
iters_done += cost;
// If no improvement was made to the Cluster, it means we've essentially run out of
// budget. Even though it may be the case that iters_done < iters still, the
// linearizer decided there wasn't enough budget left to attempt anything with.
// To avoid an infinite loop that keeps trying clusters with minuscule budgets,
// stop here too.
if (!improved) return false;
}
}
}
// All possible work has been performed, so we can return true. Note that this does *not* mean
// that all clusters are optimally linearized now. It may be that there is nothing to do left
// because all non-optimal clusters are in oversized and/or observer-bearing levels.
return true;
}
void BlockBuilderImpl::Next() noexcept