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.
This commit is contained in:
Pieter Wuille
2025-12-26 14:37:28 -05:00
committed by Pieter Wuille
parent 7194de3f7c
commit 3221f1a074

View File

@@ -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<SetIdx, SetIdx> 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<bool DownWard>
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<DownWard>(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<false>(parent_idx);
// Merge the bottom chunk with higher-feerate chunks that depend on it.
MergeSequence<true>(child_idx);
MergeSequence<false>(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<true>(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);