From 3221f1a074e7b313e97f969d11ff8a13a1dc5aa6 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Fri, 26 Dec 2025 14:37:28 -0500 Subject: [PATCH] clusterlin: make MergeSequence take SetIdx (simplification) Future changes will rely on knowing the chunk indexes of the two created chunks after a split. It is natural to return this information from Deactivate, which also simplifies MergeSequence. --- src/cluster_linearize.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/cluster_linearize.h b/src/cluster_linearize.h index 58a7b8b191b..47caf1f34b9 100644 --- a/src/cluster_linearize.h +++ b/src/cluster_linearize.h @@ -821,8 +821,9 @@ private: return child_chunk_idx; } - /** Make a specified active dependency inactive. */ - void Deactivate(TxIdx parent_idx, TxIdx child_idx) noexcept + /** Make a specified active dependency inactive. Returns the created parent and child chunk + * indexes. */ + std::pair Deactivate(TxIdx parent_idx, TxIdx child_idx) noexcept { // Gather and check information about the parent transactions. auto& parent_data = m_tx_data[parent_idx]; @@ -853,6 +854,8 @@ private: // Compute the new sets of reachable transactions for each new chunk. m_reachable[child_chunk_idx] = GetReachable(bottom_info.transactions); m_reachable[parent_chunk_idx] = GetReachable(top_info.transactions); + // Return the two new set idxs. + return {parent_chunk_idx, child_chunk_idx}; } /** Activate a dependency from the bottom set to the top set. Return the index of the merged @@ -961,11 +964,11 @@ private: return chunk_idx; } - /** Perform an upward or downward merge sequence on the specified transaction. */ + /** Perform an upward or downward merge sequence on the specified chunk. */ template - void MergeSequence(TxIdx tx_idx) noexcept + void MergeSequence(SetIdx chunk_idx) noexcept { - auto chunk_idx = m_tx_data[tx_idx].chunk_idx; + Assume(m_chunk_idxs[chunk_idx]); while (true) { auto merged_chunk_idx = MergeStep(chunk_idx); if (merged_chunk_idx == INVALID_SET_IDX) break; @@ -981,7 +984,7 @@ private: { // Deactivate the specified dependency, splitting it into two new chunks: a top containing // the parent, and a bottom containing the child. The top should have a higher feerate. - Deactivate(parent_idx, child_idx); + auto [parent_chunk_idx, child_chunk_idx] = Deactivate(parent_idx, child_idx); // At this point we have exactly two chunks which may violate topology constraints (the // parent chunk and child chunk that were produced by deactivation). We can fix @@ -990,9 +993,10 @@ private: // Merge the top chunk with lower-feerate chunks it depends on (which may be the bottom it // was just split from, or other pre-existing chunks). - MergeSequence(parent_idx); - // Merge the bottom chunk with higher-feerate chunks that depend on it. - MergeSequence(child_idx); + MergeSequence(parent_chunk_idx); + // Merge the bottom chunk with higher-feerate chunks that depend on it (if it wasn't merged + // with the top already). + if (m_chunk_idxs[child_chunk_idx]) MergeSequence(child_chunk_idx); } /** Determine the next chunk to optimize, or INVALID_SET_IDX if none. */ @@ -1242,9 +1246,7 @@ public: } // Otherwise, deactivate the dependency that was found. - Deactivate(candidate_dep.first, candidate_dep.second); - auto parent_chunk_idx = m_tx_data[candidate_dep.first].chunk_idx; - auto child_chunk_idx = m_tx_data[candidate_dep.second].chunk_idx; + auto [parent_chunk_idx, child_chunk_idx] = Deactivate(candidate_dep.first, candidate_dep.second); // Try to activate a dependency between the new bottom and the new top (opposite from the // dependency that was just deactivated). auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);