Repository: bitcoin/bitcoin
File: src/cluster_linearize.h (C/C++)

Code before:
                // Randomize the initial order of nonminimal chunks in the queue.
                TxIdx j = m_rng.randrange<TxIdx>(m_nonminimal_chunks.size());
                if (j != m_nonminimal_chunks.size() - 1) {
                    std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[j]);
                }
            }
        }
    }

    /** Try to reduce a chunk's size. Returns false if all chunks are minimal, true otherwise. */
    bool MinimizeStep() noexcept
    {
        // If the queue of potentially-non-minimal chunks is empty, we are done.
        if (m_nonminimal_chunks.empty()) return false;
        // Pop an entry from the potentially-non-minimal chunk queue.
        auto [chunk_rep, pivot_idx, flags] = m_nonminimal_chunks.front();
        m_nonminimal_chunks.pop_front();
        auto& chunk_data = m_tx_data[chunk_rep];
        Assume(chunk_data.chunk_rep == chunk_rep);
        /** Whether to move the pivot down rather than up. */
        bool move_pivot_down = flags & 1;
        /** Whether this is already the second stage. */
        bool second_stage = flags & 2;

        // Find a random dependency whose top and bottom set feerates are equal, and which has
        // pivot as bottom (if move_pivot_down) or as top (if !move_pivot_down).
        DepIdx candidate_dep = DepIdx(-1);
        uint64_t candidate_tiebreak{0};
        bool have_any = false;
        // Iterate over all transactions.
        for (auto tx_idx : chunk_data.chunk_setinfo.transactions) {
            const auto& tx_data = m_tx_data[tx_idx];
            // Iterate over all active child dependencies of the transaction.
            for (auto dep_idx : tx_data.child_deps) {
                auto& dep_data = m_dep_data[dep_idx];
                // Skip inactive child dependencies.
                if (!dep_data.active) continue;
                // Skip if this dependency does not have equal top and bottom set feerates. Note
                // that the top cannot have higher feerate than the bottom, or OptimizeSteps would
                // have dealt with it.
                if (dep_data.top_setinfo.feerate << chunk_data.chunk_setinfo.feerate) continue;
                have_any = true;
                // Skip if this dependency does not have pivot in the right place.
                if (move_pivot_down == dep_data.top_setinfo.transactions[pivot_idx]) continue;
                // Remember this as our chosen dependency if it has a better tiebreak.
                uint64_t tiebreak = m_rng.rand64() | 1;
                if (tiebreak > candidate_tiebreak) {
                    candidate_tiebreak = tiebreak;
                    candidate_dep = dep_idx;
                }
            }

Review comment (suggestion):
micro-nit
```Suggestion
        // pivot in bottom set (if move_pivot_down) or in top set (if !move_pivot_down).
```

Code after:
                // Randomize the initial order of nonminimal chunks in the queue.
                TxIdx j = m_rng.randrange<TxIdx>(m_nonminimal_chunks.size());
                if (j != m_nonminimal_chunks.size() - 1) {
                    std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[j]);
                }
            }
        }
    }

    /** Try to reduce a chunk's size. Returns false if all chunks are minimal, true otherwise. */
    bool MinimizeStep() noexcept
    {
        // If the queue of potentially-non-minimal chunks is empty, we are done.
        if (m_nonminimal_chunks.empty()) return false;
        // Pop an entry from the potentially-non-minimal chunk queue.
        auto [chunk_rep, pivot_idx, flags] = m_nonminimal_chunks.front();
        m_nonminimal_chunks.pop_front();
        auto& chunk_data = m_tx_data[chunk_rep];
        Assume(chunk_data.chunk_rep == chunk_rep);
        /** Whether to move the pivot down rather than up. */
        bool move_pivot_down = flags & 1;
        /** Whether this is already the second stage. */
        bool second_stage = flags & 2;

        // Find a random dependency whose top and bottom set feerates are equal, and which has
        // pivot in bottom set (if move_pivot_down) or in top set (if !move_pivot_down).
        DepIdx candidate_dep = DepIdx(-1);
        uint64_t candidate_tiebreak{0};
        bool have_any = false;
        // Iterate over all transactions.
        for (auto tx_idx : chunk_data.chunk_setinfo.transactions) {
            const auto& tx_data = m_tx_data[tx_idx];
            // Iterate over all active child dependencies of the transaction.
            for (auto dep_idx : tx_data.child_deps) {
                auto& dep_data = m_dep_data[dep_idx];
                // Skip inactive child dependencies.
                if (!dep_data.active) continue;
                // Skip if this dependency does not have equal top and bottom set feerates. Note
                // that the top cannot have higher feerate than the bottom, or OptimizeSteps would
                // have dealt with it.
                if (dep_data.top_setinfo.feerate << chunk_data.chunk_setinfo.feerate) continue;
                have_any = true;
                // Skip if this dependency does not have pivot in the right place.
                if (move_pivot_down == dep_data.top_setinfo.transactions[pivot_idx]) continue;
                // Remember this as our chosen dependency if it has a better tiebreak.
                uint64_t tiebreak = m_rng.rand64() | 1;
                if (tiebreak > candidate_tiebreak) {
                    candidate_tiebreak = tiebreak;
                    candidate_dep = dep_idx;
                }
            }