Coverage Report

Created: 2026-05-06 07:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/cluster_linearize.h
Line
Count
Source
1
// Copyright (c) The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#ifndef BITCOIN_CLUSTER_LINEARIZE_H
6
#define BITCOIN_CLUSTER_LINEARIZE_H
7
8
#include <algorithm>
9
#include <cstdint>
10
#include <numeric>
11
#include <optional>
12
#include <ranges>
13
#include <utility>
14
#include <vector>
15
16
#include <attributes.h>
17
#include <memusage.h>
18
#include <random.h>
19
#include <span.h>
20
#include <util/feefrac.h>
21
#include <util/vecdeque.h>
22
23
namespace cluster_linearize {
24
25
/** Data type to represent transaction indices in DepGraphs and the clusters they represent. */
26
using DepGraphIndex = uint32_t;
27
28
/** Data structure that holds a transaction graph's preprocessed data (fee, size, ancestors,
29
 *  descendants). */
30
template<typename SetType>
31
class DepGraph
32
{
33
    /** Information about a single transaction. */
34
    struct Entry
35
    {
36
        /** Fee and size of transaction itself. */
37
        FeeFrac feerate;
38
        /** All ancestors of the transaction (including itself). */
39
        SetType ancestors;
40
        /** All descendants of the transaction (including itself). */
41
        SetType descendants;
42
43
        /** Equality operator (primarily for testing purposes). */
44
1.00M
        friend bool operator==(const Entry&, const Entry&) noexcept = default;
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::Entry const&, cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::Entry const&)
Line
Count
Source
44
277k
        friend bool operator==(const Entry&, const Entry&) noexcept = default;
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::Entry const&, cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::Entry const&)
Line
Count
Source
44
277k
        friend bool operator==(const Entry&, const Entry&) noexcept = default;
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::Entry const&, cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::Entry const&)
Line
Count
Source
44
277k
        friend bool operator==(const Entry&, const Entry&) noexcept = default;
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::Entry const&, cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::Entry const&)
Line
Count
Source
44
85.4k
        friend bool operator==(const Entry&, const Entry&) noexcept = default;
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::Entry const&, cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::Entry const&)
Line
Count
Source
44
84.2k
        friend bool operator==(const Entry&, const Entry&) noexcept = default;
45
46
        /** Construct an empty entry. */
47
75.5k
        Entry() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::Entry::Entry()
Line
Count
Source
47
20.9k
        Entry() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::Entry::Entry()
Line
Count
Source
47
20.9k
        Entry() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::Entry::Entry()
Line
Count
Source
47
20.9k
        Entry() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::Entry::Entry()
Line
Count
Source
47
6.48k
        Entry() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::Entry::Entry()
Line
Count
Source
47
6.39k
        Entry() noexcept = default;
48
        /** Construct an entry with a given feerate, ancestor set, descendant set. */
49
82.1k
        Entry(const FeeFrac& f, const SetType& a, const SetType& d) noexcept : feerate(f), ancestors(a), descendants(d) {}
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::Entry::Entry(FeeFrac const&, bitset_detail::IntBitSet<unsigned long> const&, bitset_detail::IntBitSet<unsigned long> const&)
Line
Count
Source
49
27.8k
        Entry(const FeeFrac& f, const SetType& a, const SetType& d) noexcept : feerate(f), ancestors(a), descendants(d) {}
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::Entry::Entry(FeeFrac const&, bitset_detail::MultiIntBitSet<unsigned int, 2u> const&, bitset_detail::MultiIntBitSet<unsigned int, 2u> const&)
Line
Count
Source
49
20.8k
        Entry(const FeeFrac& f, const SetType& a, const SetType& d) noexcept : feerate(f), ancestors(a), descendants(d) {}
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::Entry::Entry(FeeFrac const&, bitset_detail::MultiIntBitSet<unsigned char, 8u> const&, bitset_detail::MultiIntBitSet<unsigned char, 8u> const&)
Line
Count
Source
49
20.8k
        Entry(const FeeFrac& f, const SetType& a, const SetType& d) noexcept : feerate(f), ancestors(a), descendants(d) {}
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::Entry::Entry(FeeFrac const&, bitset_detail::IntBitSet<unsigned int> const&, bitset_detail::IntBitSet<unsigned int> const&)
Line
Count
Source
49
6.40k
        Entry(const FeeFrac& f, const SetType& a, const SetType& d) noexcept : feerate(f), ancestors(a), descendants(d) {}
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::Entry::Entry(FeeFrac const&, bitset_detail::MultiIntBitSet<unsigned char, 4u> const&, bitset_detail::MultiIntBitSet<unsigned char, 4u> const&)
Line
Count
Source
49
6.31k
        Entry(const FeeFrac& f, const SetType& a, const SetType& d) noexcept : feerate(f), ancestors(a), descendants(d) {}
50
    };
51
52
    /** Data for each transaction. */
53
    std::vector<Entry> entries;
54
55
    /** Which positions are used. */
56
    SetType m_used;
57
58
public:
59
    /** Equality operator (primarily for testing purposes). */
60
    friend bool operator==(const DepGraph& a, const DepGraph& b) noexcept
61
1.88k
    {
62
1.88k
        if (a.m_used != b.m_used) return false;
63
        // Only compare the used positions within the entries vector.
64
50.0k
        for (auto idx : a.m_used) {
65
50.0k
            if (a.entries[idx] != b.entries[idx]) return false;
66
50.0k
        }
67
1.88k
        return true;
68
1.88k
    }
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>> const&, cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>> const&)
Line
Count
Source
61
454
    {
62
454
        if (a.m_used != b.m_used) return false;
63
        // Only compare the used positions within the entries vector.
64
13.8k
        for (auto idx : a.m_used) {
65
13.8k
            if (a.entries[idx] != b.entries[idx]) return false;
66
13.8k
        }
67
454
        return true;
68
454
    }
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>> const&, cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>> const&)
Line
Count
Source
61
454
    {
62
454
        if (a.m_used != b.m_used) return false;
63
        // Only compare the used positions within the entries vector.
64
13.8k
        for (auto idx : a.m_used) {
65
13.8k
            if (a.entries[idx] != b.entries[idx]) return false;
66
13.8k
        }
67
454
        return true;
68
454
    }
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>> const&, cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>> const&)
Line
Count
Source
61
454
    {
62
454
        if (a.m_used != b.m_used) return false;
63
        // Only compare the used positions within the entries vector.
64
13.8k
        for (auto idx : a.m_used) {
65
13.8k
            if (a.entries[idx] != b.entries[idx]) return false;
66
13.8k
        }
67
454
        return true;
68
454
    }
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>> const&, cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>> const&)
Line
Count
Source
61
271
    {
62
271
        if (a.m_used != b.m_used) return false;
63
        // Only compare the used positions within the entries vector.
64
4.27k
        for (auto idx : a.m_used) {
65
4.27k
            if (a.entries[idx] != b.entries[idx]) return false;
66
4.27k
        }
67
271
        return true;
68
271
    }
cluster_linearize::operator==(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>> const&, cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>> const&)
Line
Count
Source
61
250
    {
62
250
        if (a.m_used != b.m_used) return false;
63
        // Only compare the used positions within the entries vector.
64
4.21k
        for (auto idx : a.m_used) {
65
4.21k
            if (a.entries[idx] != b.entries[idx]) return false;
66
4.21k
        }
67
250
        return true;
68
250
    }
69
70
    // Default constructors.
71
8.59k
    DepGraph() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::DepGraph()
Line
Count
Source
71
4.32k
    DepGraph() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::DepGraph()
Line
Count
Source
71
1.36k
    DepGraph() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::DepGraph()
Line
Count
Source
71
1.36k
    DepGraph() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::DepGraph()
Line
Count
Source
71
799
    DepGraph() noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::DepGraph()
Line
Count
Source
71
750
    DepGraph() noexcept = default;
72
    DepGraph(const DepGraph&) noexcept = default;
73
    DepGraph(DepGraph&&) noexcept = default;
74
332
    DepGraph& operator=(const DepGraph&) noexcept = default;
75
5.09k
    DepGraph& operator=(DepGraph&&) noexcept = default;
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::operator=(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>&&)
Line
Count
Source
75
2.24k
    DepGraph& operator=(DepGraph&&) noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::operator=(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>&&)
Line
Count
Source
75
908
    DepGraph& operator=(DepGraph&&) noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::operator=(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>&&)
Line
Count
Source
75
908
    DepGraph& operator=(DepGraph&&) noexcept = default;
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::operator=(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>&&)
Line
Count
Source
75
528
    DepGraph& operator=(DepGraph&&) noexcept = default;
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::operator=(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>&&)
Line
Count
Source
75
500
    DepGraph& operator=(DepGraph&&) noexcept = default;
76
77
    /** Construct a DepGraph object given another DepGraph and a mapping from old to new.
78
     *
79
     * @param depgraph   The original DepGraph that is being remapped.
80
     *
81
     * @param mapping    A span such that mapping[i] gives the position in the new DepGraph
82
     *                   for position i in the old depgraph. Its size must be equal to
83
     *                   depgraph.PositionRange(). The value of mapping[i] is ignored if
84
     *                   position i is a hole in depgraph (i.e., if !depgraph.Positions()[i]).
85
     *
86
     * @param pos_range  The PositionRange() for the new DepGraph. It must equal the largest
87
     *                   value in mapping for any used position in depgraph plus 1, or 0 if
88
     *                   depgraph.TxCount() == 0.
89
     *
90
     * Complexity: O(N^2) where N=depgraph.TxCount().
91
     */
92
2.81k
    DepGraph(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> mapping, DepGraphIndex pos_range) noexcept : entries(pos_range)
93
2.81k
    {
94
2.81k
        Assume(mapping.size() == depgraph.PositionRange());
95
2.81k
        Assume((pos_range == 0) == (depgraph.TxCount() == 0));
96
75.1k
        for (DepGraphIndex i : depgraph.Positions()) {
97
75.1k
            auto new_idx = mapping[i];
98
75.1k
            Assume(new_idx < pos_range);
99
            // Add transaction.
100
75.1k
            entries[new_idx].ancestors = SetType::Singleton(new_idx);
101
75.1k
            entries[new_idx].descendants = SetType::Singleton(new_idx);
102
75.1k
            m_used.Set(new_idx);
103
            // Fill in fee and size.
104
75.1k
            entries[new_idx].feerate = depgraph.entries[i].feerate;
105
75.1k
        }
106
75.1k
        for (DepGraphIndex i : depgraph.Positions()) {
107
            // Fill in dependencies by mapping direct parents.
108
75.1k
            SetType parents;
109
224k
            for (auto j : depgraph.GetReducedParents(i)) parents.Set(mapping[j]);
110
75.1k
            AddDependencies(parents, mapping[i]);
111
75.1k
        }
112
        // Verify that the provided pos_range was correct (no unused positions at the end).
113
2.81k
        Assume(m_used.None() ? (pos_range == 0) : (pos_range == m_used.Last() + 1));
114
2.81k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::DepGraph(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>> const&, std::span<unsigned int const, 18446744073709551615ul>, unsigned int)
Line
Count
Source
92
681
    DepGraph(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> mapping, DepGraphIndex pos_range) noexcept : entries(pos_range)
93
681
    {
94
681
        Assume(mapping.size() == depgraph.PositionRange());
95
681
        Assume((pos_range == 0) == (depgraph.TxCount() == 0));
96
20.8k
        for (DepGraphIndex i : depgraph.Positions()) {
97
20.8k
            auto new_idx = mapping[i];
98
20.8k
            Assume(new_idx < pos_range);
99
            // Add transaction.
100
20.8k
            entries[new_idx].ancestors = SetType::Singleton(new_idx);
101
20.8k
            entries[new_idx].descendants = SetType::Singleton(new_idx);
102
20.8k
            m_used.Set(new_idx);
103
            // Fill in fee and size.
104
20.8k
            entries[new_idx].feerate = depgraph.entries[i].feerate;
105
20.8k
        }
106
20.8k
        for (DepGraphIndex i : depgraph.Positions()) {
107
            // Fill in dependencies by mapping direct parents.
108
20.8k
            SetType parents;
109
66.4k
            for (auto j : depgraph.GetReducedParents(i)) parents.Set(mapping[j]);
110
20.8k
            AddDependencies(parents, mapping[i]);
111
20.8k
        }
112
        // Verify that the provided pos_range was correct (no unused positions at the end).
113
681
        Assume(m_used.None() ? (pos_range == 0) : (pos_range == m_used.Last() + 1));
114
681
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::DepGraph(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>> const&, std::span<unsigned int const, 18446744073709551615ul>, unsigned int)
Line
Count
Source
92
681
    DepGraph(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> mapping, DepGraphIndex pos_range) noexcept : entries(pos_range)
93
681
    {
94
681
        Assume(mapping.size() == depgraph.PositionRange());
95
681
        Assume((pos_range == 0) == (depgraph.TxCount() == 0));
96
20.8k
        for (DepGraphIndex i : depgraph.Positions()) {
97
20.8k
            auto new_idx = mapping[i];
98
20.8k
            Assume(new_idx < pos_range);
99
            // Add transaction.
100
20.8k
            entries[new_idx].ancestors = SetType::Singleton(new_idx);
101
20.8k
            entries[new_idx].descendants = SetType::Singleton(new_idx);
102
20.8k
            m_used.Set(new_idx);
103
            // Fill in fee and size.
104
20.8k
            entries[new_idx].feerate = depgraph.entries[i].feerate;
105
20.8k
        }
106
20.8k
        for (DepGraphIndex i : depgraph.Positions()) {
107
            // Fill in dependencies by mapping direct parents.
108
20.8k
            SetType parents;
109
66.4k
            for (auto j : depgraph.GetReducedParents(i)) parents.Set(mapping[j]);
110
20.8k
            AddDependencies(parents, mapping[i]);
111
20.8k
        }
112
        // Verify that the provided pos_range was correct (no unused positions at the end).
113
681
        Assume(m_used.None() ? (pos_range == 0) : (pos_range == m_used.Last() + 1));
114
681
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::DepGraph(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>> const&, std::span<unsigned int const, 18446744073709551615ul>, unsigned int)
Line
Count
Source
92
681
    DepGraph(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> mapping, DepGraphIndex pos_range) noexcept : entries(pos_range)
93
681
    {
94
681
        Assume(mapping.size() == depgraph.PositionRange());
95
681
        Assume((pos_range == 0) == (depgraph.TxCount() == 0));
96
20.8k
        for (DepGraphIndex i : depgraph.Positions()) {
97
20.8k
            auto new_idx = mapping[i];
98
20.8k
            Assume(new_idx < pos_range);
99
            // Add transaction.
100
20.8k
            entries[new_idx].ancestors = SetType::Singleton(new_idx);
101
20.8k
            entries[new_idx].descendants = SetType::Singleton(new_idx);
102
20.8k
            m_used.Set(new_idx);
103
            // Fill in fee and size.
104
20.8k
            entries[new_idx].feerate = depgraph.entries[i].feerate;
105
20.8k
        }
106
20.8k
        for (DepGraphIndex i : depgraph.Positions()) {
107
            // Fill in dependencies by mapping direct parents.
108
20.8k
            SetType parents;
109
66.4k
            for (auto j : depgraph.GetReducedParents(i)) parents.Set(mapping[j]);
110
20.8k
            AddDependencies(parents, mapping[i]);
111
20.8k
        }
112
        // Verify that the provided pos_range was correct (no unused positions at the end).
113
681
        Assume(m_used.None() ? (pos_range == 0) : (pos_range == m_used.Last() + 1));
114
681
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::DepGraph(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>> const&, std::span<unsigned int const, 18446744073709551615ul>, unsigned int)
Line
Count
Source
92
396
    DepGraph(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> mapping, DepGraphIndex pos_range) noexcept : entries(pos_range)
93
396
    {
94
396
        Assume(mapping.size() == depgraph.PositionRange());
95
396
        Assume((pos_range == 0) == (depgraph.TxCount() == 0));
96
6.37k
        for (DepGraphIndex i : depgraph.Positions()) {
97
6.37k
            auto new_idx = mapping[i];
98
6.37k
            Assume(new_idx < pos_range);
99
            // Add transaction.
100
6.37k
            entries[new_idx].ancestors = SetType::Singleton(new_idx);
101
6.37k
            entries[new_idx].descendants = SetType::Singleton(new_idx);
102
6.37k
            m_used.Set(new_idx);
103
            // Fill in fee and size.
104
6.37k
            entries[new_idx].feerate = depgraph.entries[i].feerate;
105
6.37k
        }
106
6.37k
        for (DepGraphIndex i : depgraph.Positions()) {
107
            // Fill in dependencies by mapping direct parents.
108
6.37k
            SetType parents;
109
12.7k
            for (auto j : depgraph.GetReducedParents(i)) parents.Set(mapping[j]);
110
6.37k
            AddDependencies(parents, mapping[i]);
111
6.37k
        }
112
        // Verify that the provided pos_range was correct (no unused positions at the end).
113
396
        Assume(m_used.None() ? (pos_range == 0) : (pos_range == m_used.Last() + 1));
114
396
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::DepGraph(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>> const&, std::span<unsigned int const, 18446744073709551615ul>, unsigned int)
Line
Count
Source
92
375
    DepGraph(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> mapping, DepGraphIndex pos_range) noexcept : entries(pos_range)
93
375
    {
94
375
        Assume(mapping.size() == depgraph.PositionRange());
95
375
        Assume((pos_range == 0) == (depgraph.TxCount() == 0));
96
6.31k
        for (DepGraphIndex i : depgraph.Positions()) {
97
6.31k
            auto new_idx = mapping[i];
98
6.31k
            Assume(new_idx < pos_range);
99
            // Add transaction.
100
6.31k
            entries[new_idx].ancestors = SetType::Singleton(new_idx);
101
6.31k
            entries[new_idx].descendants = SetType::Singleton(new_idx);
102
6.31k
            m_used.Set(new_idx);
103
            // Fill in fee and size.
104
6.31k
            entries[new_idx].feerate = depgraph.entries[i].feerate;
105
6.31k
        }
106
6.31k
        for (DepGraphIndex i : depgraph.Positions()) {
107
            // Fill in dependencies by mapping direct parents.
108
6.31k
            SetType parents;
109
12.6k
            for (auto j : depgraph.GetReducedParents(i)) parents.Set(mapping[j]);
110
6.31k
            AddDependencies(parents, mapping[i]);
111
6.31k
        }
112
        // Verify that the provided pos_range was correct (no unused positions at the end).
113
375
        Assume(m_used.None() ? (pos_range == 0) : (pos_range == m_used.Last() + 1));
114
375
    }
115
116
    /** Get the set of transactions positions in use. Complexity: O(1). */
117
5.63M
    const SetType& Positions() const noexcept { return m_used; }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::Positions() const
Line
Count
Source
117
1.82M
    const SetType& Positions() const noexcept { return m_used; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::Positions() const
Line
Count
Source
117
1.44M
    const SetType& Positions() const noexcept { return m_used; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::Positions() const
Line
Count
Source
117
1.44M
    const SetType& Positions() const noexcept { return m_used; }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::Positions() const
Line
Count
Source
117
451k
    const SetType& Positions() const noexcept { return m_used; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::Positions() const
Line
Count
Source
117
451k
    const SetType& Positions() const noexcept { return m_used; }
118
    /** Get the range of positions in this DepGraph. All entries in Positions() are in [0, PositionRange() - 1]. */
119
260k
    DepGraphIndex PositionRange() const noexcept { return entries.size(); }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::PositionRange() const
Line
Count
Source
119
114k
    DepGraphIndex PositionRange() const noexcept { return entries.size(); }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::PositionRange() const
Line
Count
Source
119
46.9k
    DepGraphIndex PositionRange() const noexcept { return entries.size(); }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::PositionRange() const
Line
Count
Source
119
46.9k
    DepGraphIndex PositionRange() const noexcept { return entries.size(); }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::PositionRange() const
Line
Count
Source
119
25.9k
    DepGraphIndex PositionRange() const noexcept { return entries.size(); }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::PositionRange() const
Line
Count
Source
119
25.8k
    DepGraphIndex PositionRange() const noexcept { return entries.size(); }
120
    /** Get the number of transactions in the graph. Complexity: O(1). */
121
562k
    auto TxCount() const noexcept { return m_used.Count(); }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::TxCount() const
Line
Count
Source
121
222k
    auto TxCount() const noexcept { return m_used.Count(); }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::TxCount() const
Line
Count
Source
121
112k
    auto TxCount() const noexcept { return m_used.Count(); }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::TxCount() const
Line
Count
Source
121
112k
    auto TxCount() const noexcept { return m_used.Count(); }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::TxCount() const
Line
Count
Source
121
57.0k
    auto TxCount() const noexcept { return m_used.Count(); }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::TxCount() const
Line
Count
Source
121
56.9k
    auto TxCount() const noexcept { return m_used.Count(); }
122
    /** Get the feerate of a given transaction i. Complexity: O(1). */
123
23.0M
    const FeeFrac& FeeRate(DepGraphIndex i) const noexcept { return entries[i].feerate; }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::FeeRate(unsigned int) const
Line
Count
Source
123
7.07M
    const FeeFrac& FeeRate(DepGraphIndex i) const noexcept { return entries[i].feerate; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::FeeRate(unsigned int) const
Line
Count
Source
123
6.22M
    const FeeFrac& FeeRate(DepGraphIndex i) const noexcept { return entries[i].feerate; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::FeeRate(unsigned int) const
Line
Count
Source
123
6.22M
    const FeeFrac& FeeRate(DepGraphIndex i) const noexcept { return entries[i].feerate; }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::FeeRate(unsigned int) const
Line
Count
Source
123
1.75M
    const FeeFrac& FeeRate(DepGraphIndex i) const noexcept { return entries[i].feerate; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::FeeRate(unsigned int) const
Line
Count
Source
123
1.75M
    const FeeFrac& FeeRate(DepGraphIndex i) const noexcept { return entries[i].feerate; }
124
    /** Get the mutable feerate of a given transaction i. Complexity: O(1). */
125
476
    FeeFrac& FeeRate(DepGraphIndex i) noexcept { return entries[i].feerate; }
126
    /** Get the ancestors of a given transaction i. Complexity: O(1). */
127
48.0M
    const SetType& Ancestors(DepGraphIndex i) const noexcept { return entries[i].ancestors; }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::Ancestors(unsigned int) const
Line
Count
Source
127
15.4M
    const SetType& Ancestors(DepGraphIndex i) const noexcept { return entries[i].ancestors; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::Ancestors(unsigned int) const
Line
Count
Source
127
13.1M
    const SetType& Ancestors(DepGraphIndex i) const noexcept { return entries[i].ancestors; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::Ancestors(unsigned int) const
Line
Count
Source
127
13.1M
    const SetType& Ancestors(DepGraphIndex i) const noexcept { return entries[i].ancestors; }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::Ancestors(unsigned int) const
Line
Count
Source
127
3.18M
    const SetType& Ancestors(DepGraphIndex i) const noexcept { return entries[i].ancestors; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::Ancestors(unsigned int) const
Line
Count
Source
127
3.17M
    const SetType& Ancestors(DepGraphIndex i) const noexcept { return entries[i].ancestors; }
128
    /** Get the descendants of a given transaction i. Complexity: O(1). */
129
2.97M
    const SetType& Descendants(DepGraphIndex i) const noexcept { return entries[i].descendants; }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::Descendants(unsigned int) const
Line
Count
Source
129
1.68M
    const SetType& Descendants(DepGraphIndex i) const noexcept { return entries[i].descendants; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::Descendants(unsigned int) const
Line
Count
Source
129
547k
    const SetType& Descendants(DepGraphIndex i) const noexcept { return entries[i].descendants; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::Descendants(unsigned int) const
Line
Count
Source
129
547k
    const SetType& Descendants(DepGraphIndex i) const noexcept { return entries[i].descendants; }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::Descendants(unsigned int) const
Line
Count
Source
129
95.5k
    const SetType& Descendants(DepGraphIndex i) const noexcept { return entries[i].descendants; }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::Descendants(unsigned int) const
Line
Count
Source
129
95.2k
    const SetType& Descendants(DepGraphIndex i) const noexcept { return entries[i].descendants; }
130
131
    /** Add a new unconnected transaction to this transaction graph (in the first available
132
     *  position), and return its DepGraphIndex.
133
     *
134
     * Complexity: O(1) (amortized, due to resizing of backing vector).
135
     */
136
    DepGraphIndex AddTransaction(const FeeFrac& feefrac) noexcept
137
82.1k
    {
138
82.1k
        static constexpr auto ALL_POSITIONS = SetType::Fill(SetType::Size());
139
82.1k
        auto available = ALL_POSITIONS - m_used;
140
82.1k
        Assume(available.Any());
141
82.1k
        DepGraphIndex new_idx = available.First();
142
82.1k
        if (new_idx == entries.size()) {
143
82.1k
            entries.emplace_back(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
144
82.1k
        } else {
145
0
            entries[new_idx] = Entry(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
146
0
        }
147
82.1k
        m_used.Set(new_idx);
148
82.1k
        return new_idx;
149
82.1k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::AddTransaction(FeeFrac const&)
Line
Count
Source
137
27.8k
    {
138
27.8k
        static constexpr auto ALL_POSITIONS = SetType::Fill(SetType::Size());
139
27.8k
        auto available = ALL_POSITIONS - m_used;
140
27.8k
        Assume(available.Any());
141
27.8k
        DepGraphIndex new_idx = available.First();
142
27.8k
        if (new_idx == entries.size()) {
143
27.8k
            entries.emplace_back(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
144
27.8k
        } else {
145
0
            entries[new_idx] = Entry(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
146
0
        }
147
27.8k
        m_used.Set(new_idx);
148
27.8k
        return new_idx;
149
27.8k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::AddTransaction(FeeFrac const&)
Line
Count
Source
137
20.8k
    {
138
20.8k
        static constexpr auto ALL_POSITIONS = SetType::Fill(SetType::Size());
139
20.8k
        auto available = ALL_POSITIONS - m_used;
140
20.8k
        Assume(available.Any());
141
20.8k
        DepGraphIndex new_idx = available.First();
142
20.8k
        if (new_idx == entries.size()) {
143
20.8k
            entries.emplace_back(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
144
20.8k
        } else {
145
0
            entries[new_idx] = Entry(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
146
0
        }
147
20.8k
        m_used.Set(new_idx);
148
20.8k
        return new_idx;
149
20.8k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::AddTransaction(FeeFrac const&)
Line
Count
Source
137
20.8k
    {
138
20.8k
        static constexpr auto ALL_POSITIONS = SetType::Fill(SetType::Size());
139
20.8k
        auto available = ALL_POSITIONS - m_used;
140
20.8k
        Assume(available.Any());
141
20.8k
        DepGraphIndex new_idx = available.First();
142
20.8k
        if (new_idx == entries.size()) {
143
20.8k
            entries.emplace_back(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
144
20.8k
        } else {
145
0
            entries[new_idx] = Entry(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
146
0
        }
147
20.8k
        m_used.Set(new_idx);
148
20.8k
        return new_idx;
149
20.8k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::AddTransaction(FeeFrac const&)
Line
Count
Source
137
6.40k
    {
138
6.40k
        static constexpr auto ALL_POSITIONS = SetType::Fill(SetType::Size());
139
6.40k
        auto available = ALL_POSITIONS - m_used;
140
6.40k
        Assume(available.Any());
141
6.40k
        DepGraphIndex new_idx = available.First();
142
6.40k
        if (new_idx == entries.size()) {
143
6.40k
            entries.emplace_back(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
144
6.40k
        } else {
145
0
            entries[new_idx] = Entry(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
146
0
        }
147
6.40k
        m_used.Set(new_idx);
148
6.40k
        return new_idx;
149
6.40k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::AddTransaction(FeeFrac const&)
Line
Count
Source
137
6.31k
    {
138
6.31k
        static constexpr auto ALL_POSITIONS = SetType::Fill(SetType::Size());
139
6.31k
        auto available = ALL_POSITIONS - m_used;
140
6.31k
        Assume(available.Any());
141
6.31k
        DepGraphIndex new_idx = available.First();
142
6.31k
        if (new_idx == entries.size()) {
143
6.31k
            entries.emplace_back(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
144
6.31k
        } else {
145
0
            entries[new_idx] = Entry(feefrac, SetType::Singleton(new_idx), SetType::Singleton(new_idx));
146
0
        }
147
6.31k
        m_used.Set(new_idx);
148
6.31k
        return new_idx;
149
6.31k
    }
150
151
    /** Remove the specified positions from this DepGraph.
152
     *
153
     * The specified positions will no longer be part of Positions(), and dependencies with them are
154
     * removed. Note that due to DepGraph only tracking ancestors/descendants (and not direct
155
     * dependencies), if a parent is removed while a grandparent remains, the grandparent will
156
     * remain an ancestor.
157
     *
158
     * Complexity: O(N) where N=TxCount().
159
     */
160
    void RemoveTransactions(const SetType& del) noexcept
161
1.13k
    {
162
1.13k
        m_used -= del;
163
        // Remove now-unused trailing entries.
164
5.47k
        while (!entries.empty() && !m_used[entries.size() - 1]) {
165
4.34k
            entries.pop_back();
166
4.34k
        }
167
        // Remove the deleted transactions from ancestors/descendants of other transactions. Note
168
        // that the deleted positions will retain old feerate and dependency information. This does
169
        // not matter as they will be overwritten by AddTransaction if they get used again.
170
1.13k
        for (auto& entry : entries) {
171
887
            entry.ancestors &= m_used;
172
887
            entry.descendants &= m_used;
173
887
        }
174
1.13k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::RemoveTransactions(bitset_detail::IntBitSet<unsigned int> const&)
Line
Count
Source
161
7
    {
162
7
        m_used -= del;
163
        // Remove now-unused trailing entries.
164
7
        while (!entries.empty() && !m_used[entries.size() - 1]) {
165
0
            entries.pop_back();
166
0
        }
167
        // Remove the deleted transactions from ancestors/descendants of other transactions. Note
168
        // that the deleted positions will retain old feerate and dependency information. This does
169
        // not matter as they will be overwritten by AddTransaction if they get used again.
170
29
        for (auto& entry : entries) {
171
29
            entry.ancestors &= m_used;
172
29
            entry.descendants &= m_used;
173
29
        }
174
7
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::RemoveTransactions(bitset_detail::IntBitSet<unsigned long> const&)
Line
Count
Source
161
1.12k
    {
162
1.12k
        m_used -= del;
163
        // Remove now-unused trailing entries.
164
5.46k
        while (!entries.empty() && !m_used[entries.size() - 1]) {
165
4.34k
            entries.pop_back();
166
4.34k
        }
167
        // Remove the deleted transactions from ancestors/descendants of other transactions. Note
168
        // that the deleted positions will retain old feerate and dependency information. This does
169
        // not matter as they will be overwritten by AddTransaction if they get used again.
170
1.12k
        for (auto& entry : entries) {
171
858
            entry.ancestors &= m_used;
172
858
            entry.descendants &= m_used;
173
858
        }
174
1.12k
    }
175
176
    /** Modify this transaction graph, adding multiple parents to a specified child.
177
     *
178
     * Complexity: O(N) where N=TxCount().
179
     */
180
    void AddDependencies(const SetType& parents, DepGraphIndex child) noexcept
181
162k
    {
182
162k
        Assume(m_used[child]);
183
162k
        Assume(parents.IsSubsetOf(m_used));
184
        // Compute the ancestors of parents that are not already ancestors of child.
185
162k
        SetType par_anc;
186
641k
        for (auto par : parents - Ancestors(child)) {
187
641k
            par_anc |= Ancestors(par);
188
641k
        }
189
162k
        par_anc -= Ancestors(child);
190
        // Bail out if there are no such ancestors.
191
162k
        if (par_anc.None()) return;
192
        // To each such ancestor, add as descendants the descendants of the child.
193
123k
        const auto& chl_des = entries[child].descendants;
194
869k
        for (auto anc_of_par : par_anc) {
195
869k
            entries[anc_of_par].descendants |= chl_des;
196
869k
        }
197
        // To each descendant of the child, add those ancestors.
198
123k
        for (auto dec_of_chl : Descendants(child)) {
199
123k
            entries[dec_of_chl].ancestors |= par_anc;
200
123k
        }
201
123k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::AddDependencies(bitset_detail::IntBitSet<unsigned long> const&, unsigned int)
Line
Count
Source
181
54.1k
    {
182
54.1k
        Assume(m_used[child]);
183
54.1k
        Assume(parents.IsSubsetOf(m_used));
184
        // Compute the ancestors of parents that are not already ancestors of child.
185
54.1k
        SetType par_anc;
186
194k
        for (auto par : parents - Ancestors(child)) {
187
194k
            par_anc |= Ancestors(par);
188
194k
        }
189
54.1k
        par_anc -= Ancestors(child);
190
        // Bail out if there are no such ancestors.
191
54.1k
        if (par_anc.None()) return;
192
        // To each such ancestor, add as descendants the descendants of the child.
193
38.0k
        const auto& chl_des = entries[child].descendants;
194
292k
        for (auto anc_of_par : par_anc) {
195
292k
            entries[anc_of_par].descendants |= chl_des;
196
292k
        }
197
        // To each descendant of the child, add those ancestors.
198
38.0k
        for (auto dec_of_chl : Descendants(child)) {
199
38.0k
            entries[dec_of_chl].ancestors |= par_anc;
200
38.0k
        }
201
38.0k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::AddDependencies(bitset_detail::MultiIntBitSet<unsigned int, 2u> const&, unsigned int)
Line
Count
Source
181
41.6k
    {
182
41.6k
        Assume(m_used[child]);
183
41.6k
        Assume(parents.IsSubsetOf(m_used));
184
        // Compute the ancestors of parents that are not already ancestors of child.
185
41.6k
        SetType par_anc;
186
186k
        for (auto par : parents - Ancestors(child)) {
187
186k
            par_anc |= Ancestors(par);
188
186k
        }
189
41.6k
        par_anc -= Ancestors(child);
190
        // Bail out if there are no such ancestors.
191
41.6k
        if (par_anc.None()) return;
192
        // To each such ancestor, add as descendants the descendants of the child.
193
32.8k
        const auto& chl_des = entries[child].descendants;
194
240k
        for (auto anc_of_par : par_anc) {
195
240k
            entries[anc_of_par].descendants |= chl_des;
196
240k
        }
197
        // To each descendant of the child, add those ancestors.
198
32.8k
        for (auto dec_of_chl : Descendants(child)) {
199
32.8k
            entries[dec_of_chl].ancestors |= par_anc;
200
32.8k
        }
201
32.8k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::AddDependencies(bitset_detail::MultiIntBitSet<unsigned char, 8u> const&, unsigned int)
Line
Count
Source
181
41.6k
    {
182
41.6k
        Assume(m_used[child]);
183
41.6k
        Assume(parents.IsSubsetOf(m_used));
184
        // Compute the ancestors of parents that are not already ancestors of child.
185
41.6k
        SetType par_anc;
186
186k
        for (auto par : parents - Ancestors(child)) {
187
186k
            par_anc |= Ancestors(par);
188
186k
        }
189
41.6k
        par_anc -= Ancestors(child);
190
        // Bail out if there are no such ancestors.
191
41.6k
        if (par_anc.None()) return;
192
        // To each such ancestor, add as descendants the descendants of the child.
193
32.8k
        const auto& chl_des = entries[child].descendants;
194
240k
        for (auto anc_of_par : par_anc) {
195
240k
            entries[anc_of_par].descendants |= chl_des;
196
240k
        }
197
        // To each descendant of the child, add those ancestors.
198
32.8k
        for (auto dec_of_chl : Descendants(child)) {
199
32.8k
            entries[dec_of_chl].ancestors |= par_anc;
200
32.8k
        }
201
32.8k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::AddDependencies(bitset_detail::IntBitSet<unsigned int> const&, unsigned int)
Line
Count
Source
181
12.7k
    {
182
12.7k
        Assume(m_used[child]);
183
12.7k
        Assume(parents.IsSubsetOf(m_used));
184
        // Compute the ancestors of parents that are not already ancestors of child.
185
12.7k
        SetType par_anc;
186
36.9k
        for (auto par : parents - Ancestors(child)) {
187
36.9k
            par_anc |= Ancestors(par);
188
36.9k
        }
189
12.7k
        par_anc -= Ancestors(child);
190
        // Bail out if there are no such ancestors.
191
12.7k
        if (par_anc.None()) return;
192
        // To each such ancestor, add as descendants the descendants of the child.
193
9.89k
        const auto& chl_des = entries[child].descendants;
194
48.4k
        for (auto anc_of_par : par_anc) {
195
48.4k
            entries[anc_of_par].descendants |= chl_des;
196
48.4k
        }
197
        // To each descendant of the child, add those ancestors.
198
9.89k
        for (auto dec_of_chl : Descendants(child)) {
199
9.89k
            entries[dec_of_chl].ancestors |= par_anc;
200
9.89k
        }
201
9.89k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::AddDependencies(bitset_detail::MultiIntBitSet<unsigned char, 4u> const&, unsigned int)
Line
Count
Source
181
12.6k
    {
182
12.6k
        Assume(m_used[child]);
183
12.6k
        Assume(parents.IsSubsetOf(m_used));
184
        // Compute the ancestors of parents that are not already ancestors of child.
185
12.6k
        SetType par_anc;
186
36.8k
        for (auto par : parents - Ancestors(child)) {
187
36.8k
            par_anc |= Ancestors(par);
188
36.8k
        }
189
12.6k
        par_anc -= Ancestors(child);
190
        // Bail out if there are no such ancestors.
191
12.6k
        if (par_anc.None()) return;
192
        // To each such ancestor, add as descendants the descendants of the child.
193
9.82k
        const auto& chl_des = entries[child].descendants;
194
48.3k
        for (auto anc_of_par : par_anc) {
195
48.3k
            entries[anc_of_par].descendants |= chl_des;
196
48.3k
        }
197
        // To each descendant of the child, add those ancestors.
198
9.82k
        for (auto dec_of_chl : Descendants(child)) {
199
9.82k
            entries[dec_of_chl].ancestors |= par_anc;
200
9.82k
        }
201
9.82k
    }
202
203
    /** Compute the (reduced) set of parents of node i in this graph.
204
     *
205
     * This returns the minimal subset of the parents of i whose ancestors together equal all of
206
     * i's ancestors (unless i is part of a cycle of dependencies). Note that DepGraph does not
207
     * store the set of parents; this information is inferred from the ancestor sets.
208
     *
209
     * Complexity: O(N) where N=Ancestors(i).Count() (which is bounded by TxCount()).
210
     */
211
    SetType GetReducedParents(DepGraphIndex i) const noexcept
212
5.19M
    {
213
5.19M
        SetType parents = Ancestors(i);
214
5.19M
        parents.Reset(i);
215
26.1M
        for (auto parent : parents) {
216
26.1M
            if (parents[parent]) {
217
22.1M
                parents -= Ancestors(parent);
218
22.1M
                parents.Set(parent);
219
22.1M
            }
220
26.1M
        }
221
5.19M
        return parents;
222
5.19M
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::GetReducedParents(unsigned int) const
Line
Count
Source
212
1.49M
    {
213
1.49M
        SetType parents = Ancestors(i);
214
1.49M
        parents.Reset(i);
215
9.00M
        for (auto parent : parents) {
216
9.00M
            if (parents[parent]) {
217
7.00M
                parents -= Ancestors(parent);
218
7.00M
                parents.Set(parent);
219
7.00M
            }
220
9.00M
        }
221
1.49M
        return parents;
222
1.49M
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::GetReducedParents(unsigned int) const
Line
Count
Source
212
1.42M
    {
213
1.42M
        SetType parents = Ancestors(i);
214
1.42M
        parents.Reset(i);
215
7.57M
        for (auto parent : parents) {
216
7.57M
            if (parents[parent]) {
217
6.22M
                parents -= Ancestors(parent);
218
6.22M
                parents.Set(parent);
219
6.22M
            }
220
7.57M
        }
221
1.42M
        return parents;
222
1.42M
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::GetReducedParents(unsigned int) const
Line
Count
Source
212
1.42M
    {
213
1.42M
        SetType parents = Ancestors(i);
214
1.42M
        parents.Reset(i);
215
6.52M
        for (auto parent : parents) {
216
6.52M
            if (parents[parent]) {
217
6.22M
                parents -= Ancestors(parent);
218
6.22M
                parents.Set(parent);
219
6.22M
            }
220
6.52M
        }
221
1.42M
        return parents;
222
1.42M
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::GetReducedParents(unsigned int) const
Line
Count
Source
212
431k
    {
213
431k
        SetType parents = Ancestors(i);
214
431k
        parents.Reset(i);
215
1.65M
        for (auto parent : parents) {
216
1.65M
            if (parents[parent]) {
217
1.33M
                parents -= Ancestors(parent);
218
1.33M
                parents.Set(parent);
219
1.33M
            }
220
1.65M
        }
221
431k
        return parents;
222
431k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::GetReducedParents(unsigned int) const
Line
Count
Source
212
431k
    {
213
431k
        SetType parents = Ancestors(i);
214
431k
        parents.Reset(i);
215
1.43M
        for (auto parent : parents) {
216
1.43M
            if (parents[parent]) {
217
1.33M
                parents -= Ancestors(parent);
218
1.33M
                parents.Set(parent);
219
1.33M
            }
220
1.43M
        }
221
431k
        return parents;
222
431k
    }
223
224
    /** Compute the (reduced) set of children of node i in this graph.
225
     *
226
     * This returns the minimal subset of the children of i whose descendants together equal all of
227
     * i's descendants (unless i is part of a cycle of dependencies). Note that DepGraph does not
228
     * store the set of children; this information is inferred from the descendant sets.
229
     *
230
     * Complexity: O(N) where N=Descendants(i).Count() (which is bounded by TxCount()).
231
     */
232
    SetType GetReducedChildren(DepGraphIndex i) const noexcept
233
50.0k
    {
234
50.0k
        SetType children = Descendants(i);
235
50.0k
        children.Reset(i);
236
233k
        for (auto child : children) {
237
233k
            if (children[child]) {
238
167k
                children -= Descendants(child);
239
167k
                children.Set(child);
240
167k
            }
241
233k
        }
242
50.0k
        return children;
243
50.0k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::GetReducedChildren(unsigned int) const
Line
Count
Source
233
13.8k
    {
234
13.8k
        SetType children = Descendants(i);
235
13.8k
        children.Reset(i);
236
80.1k
        for (auto child : children) {
237
80.1k
            if (children[child]) {
238
49.4k
                children -= Descendants(child);
239
49.4k
                children.Set(child);
240
49.4k
            }
241
80.1k
        }
242
13.8k
        return children;
243
13.8k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::GetReducedChildren(unsigned int) const
Line
Count
Source
233
13.8k
    {
234
13.8k
        SetType children = Descendants(i);
235
13.8k
        children.Reset(i);
236
70.5k
        for (auto child : children) {
237
70.5k
            if (children[child]) {
238
49.4k
                children -= Descendants(child);
239
49.4k
                children.Set(child);
240
49.4k
            }
241
70.5k
        }
242
13.8k
        return children;
243
13.8k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::GetReducedChildren(unsigned int) const
Line
Count
Source
233
13.8k
    {
234
13.8k
        SetType children = Descendants(i);
235
13.8k
        children.Reset(i);
236
55.2k
        for (auto child : children) {
237
55.2k
            if (children[child]) {
238
49.4k
                children -= Descendants(child);
239
49.4k
                children.Set(child);
240
49.4k
            }
241
55.2k
        }
242
13.8k
        return children;
243
13.8k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::GetReducedChildren(unsigned int) const
Line
Count
Source
233
4.25k
    {
234
4.25k
        SetType children = Descendants(i);
235
4.25k
        children.Reset(i);
236
16.1k
        for (auto child : children) {
237
16.1k
            if (children[child]) {
238
9.50k
                children -= Descendants(child);
239
9.50k
                children.Set(child);
240
9.50k
            }
241
16.1k
        }
242
4.25k
        return children;
243
4.25k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::GetReducedChildren(unsigned int) const
Line
Count
Source
233
4.21k
    {
234
4.21k
        SetType children = Descendants(i);
235
4.21k
        children.Reset(i);
236
11.6k
        for (auto child : children) {
237
11.6k
            if (children[child]) {
238
9.47k
                children -= Descendants(child);
239
9.47k
                children.Set(child);
240
9.47k
            }
241
11.6k
        }
242
4.21k
        return children;
243
4.21k
    }
244
245
    /** Compute the aggregate feerate of a set of nodes in this graph.
246
     *
247
     * Complexity: O(N) where N=elems.Count().
248
     **/
249
    FeeFrac FeeRate(const SetType& elems) const noexcept
250
    {
251
        FeeFrac ret;
252
        for (auto pos : elems) ret += entries[pos].feerate;
253
        return ret;
254
    }
255
256
    /** Get the connected component within the subset "todo" that contains tx (which must be in
257
     *  todo).
258
     *
259
     * Two transactions are considered connected if they are both in `todo`, and one is an ancestor
260
     * of the other in the entire graph (so not just within `todo`), or transitively there is a
261
     * path of transactions connecting them. This does mean that if `todo` contains a transaction
262
     * and a grandparent, but misses the parent, they will still be part of the same component.
263
     *
264
     * Complexity: O(ret.Count()).
265
     */
266
    SetType GetConnectedComponent(const SetType& todo, DepGraphIndex tx) const noexcept
267
229k
    {
268
229k
        Assume(todo[tx]);
269
229k
        Assume(todo.IsSubsetOf(m_used));
270
229k
        auto to_add = SetType::Singleton(tx);
271
229k
        SetType ret;
272
461k
        do {
273
461k
            SetType old = ret;
274
790k
            for (auto add : to_add) {
275
790k
                ret |= Descendants(add);
276
790k
                ret |= Ancestors(add);
277
790k
            }
278
461k
            ret &= todo;
279
461k
            to_add = ret - old;
280
461k
        } while (to_add.Any());
281
229k
        return ret;
282
229k
    }
283
284
    /** Find some connected component within the subset "todo" of this graph.
285
     *
286
     * Specifically, this finds the connected component which contains the first transaction of
287
     * todo (if any).
288
     *
289
     * Complexity: O(ret.Count()).
290
     */
291
    SetType FindConnectedComponent(const SetType& todo) const noexcept
292
229k
    {
293
229k
        if (todo.None()) return todo;
294
229k
        return GetConnectedComponent(todo, todo.First());
295
229k
    }
296
297
    /** Determine if a subset is connected.
298
     *
299
     * Complexity: O(subset.Count()).
300
     */
301
    bool IsConnected(const SetType& subset) const noexcept
302
228k
    {
303
228k
        return FindConnectedComponent(subset) == subset;
304
228k
    }
305
306
    /** Determine if this entire graph is connected.
307
     *
308
     * Complexity: O(TxCount()).
309
     */
310
    bool IsConnected() const noexcept { return IsConnected(m_used); }
311
312
    /** Append the entries of select to list in a topologically valid order.
313
     *
314
     * Complexity: O(select.Count() * log(select.Count())).
315
     */
316
    void AppendTopo(std::vector<DepGraphIndex>& list, const SetType& select) const noexcept
317
    {
318
        DepGraphIndex old_len = list.size();
319
        for (auto i : select) list.push_back(i);
320
        std::ranges::sort(std::span{list}.subspan(old_len), [&](DepGraphIndex a, DepGraphIndex b) noexcept {
321
            const auto a_anc_count = entries[a].ancestors.Count();
322
            const auto b_anc_count = entries[b].ancestors.Count();
323
            if (a_anc_count != b_anc_count) return a_anc_count < b_anc_count;
324
            return a < b;
325
        });
326
    }
327
328
    /** Check if this graph is acyclic. */
329
    bool IsAcyclic() const noexcept
330
55.8k
    {
331
253k
        for (auto i : Positions()) {
332
253k
            if ((Ancestors(i) & Descendants(i)) != SetType::Singleton(i)) {
333
0
                return false;
334
0
            }
335
253k
        }
336
55.8k
        return true;
337
55.8k
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>>::IsAcyclic() const
Line
Count
Source
330
55.1k
    {
331
235k
        for (auto i : Positions()) {
332
235k
            if ((Ancestors(i) & Descendants(i)) != SetType::Singleton(i)) {
333
0
                return false;
334
0
            }
335
235k
        }
336
55.1k
        return true;
337
55.1k
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::IsAcyclic() const
Line
Count
Source
330
227
    {
331
6.93k
        for (auto i : Positions()) {
332
6.93k
            if ((Ancestors(i) & Descendants(i)) != SetType::Singleton(i)) {
333
0
                return false;
334
0
            }
335
6.93k
        }
336
227
        return true;
337
227
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::IsAcyclic() const
Line
Count
Source
330
227
    {
331
6.93k
        for (auto i : Positions()) {
332
6.93k
            if ((Ancestors(i) & Descendants(i)) != SetType::Singleton(i)) {
333
0
                return false;
334
0
            }
335
6.93k
        }
336
227
        return true;
337
227
    }
cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>>::IsAcyclic() const
Line
Count
Source
330
132
    {
331
2.12k
        for (auto i : Positions()) {
332
2.12k
            if ((Ancestors(i) & Descendants(i)) != SetType::Singleton(i)) {
333
0
                return false;
334
0
            }
335
2.12k
        }
336
132
        return true;
337
132
    }
cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::IsAcyclic() const
Line
Count
Source
330
125
    {
331
2.10k
        for (auto i : Positions()) {
332
2.10k
            if ((Ancestors(i) & Descendants(i)) != SetType::Singleton(i)) {
333
0
                return false;
334
0
            }
335
2.10k
        }
336
125
        return true;
337
125
    }
338
339
    unsigned CountDependencies() const noexcept
340
    {
341
        unsigned ret = 0;
342
        for (auto i : Positions()) {
343
            ret += GetReducedParents(i).Count();
344
        }
345
        return ret;
346
    }
347
348
    /** Reduce memory usage if possible. No observable effect. */
349
    void Compact() noexcept
350
6.28k
    {
351
6.28k
        entries.shrink_to_fit();
352
6.28k
    }
353
354
    size_t DynamicMemoryUsage() const noexcept
355
67.9k
    {
356
67.9k
        return memusage::DynamicUsage(entries);
357
67.9k
    }
358
};
359
360
/** A set of transactions together with their aggregate feerate. */
361
template<typename SetType>
362
struct SetInfo
363
{
364
    /** The transactions in the set. */
365
    SetType transactions;
366
    /** Their combined fee and size. */
367
    FeeFrac feerate;
368
369
    /** Construct a SetInfo for the empty set. */
370
5.07M
    SetInfo() noexcept = default;
cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned long>>::SetInfo()
Line
Count
Source
370
1.45M
    SetInfo() noexcept = default;
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::SetInfo()
Line
Count
Source
370
1.38M
    SetInfo() noexcept = default;
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::SetInfo()
Line
Count
Source
370
1.38M
    SetInfo() noexcept = default;
cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned int>>::SetInfo()
Line
Count
Source
370
421k
    SetInfo() noexcept = default;
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::SetInfo()
Line
Count
Source
370
421k
    SetInfo() noexcept = default;
371
372
    /** Construct a SetInfo for a specified set and feerate. */
373
    SetInfo(const SetType& txn, const FeeFrac& fr) noexcept : transactions(txn), feerate(fr) {}
374
375
    /** Construct a SetInfo for a given transaction in a depgraph. */
376
    explicit SetInfo(const DepGraph<SetType>& depgraph, DepGraphIndex pos) noexcept :
377
5.37M
        transactions(SetType::Singleton(pos)), feerate(depgraph.FeeRate(pos)) {}
cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned long>>::SetInfo(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>> const&, unsigned int)
Line
Count
Source
377
1.75M
        transactions(SetType::Singleton(pos)), feerate(depgraph.FeeRate(pos)) {}
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::SetInfo(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>> const&, unsigned int)
Line
Count
Source
377
1.38M
        transactions(SetType::Singleton(pos)), feerate(depgraph.FeeRate(pos)) {}
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::SetInfo(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>> const&, unsigned int)
Line
Count
Source
377
1.38M
        transactions(SetType::Singleton(pos)), feerate(depgraph.FeeRate(pos)) {}
cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned int>>::SetInfo(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>> const&, unsigned int)
Line
Count
Source
377
421k
        transactions(SetType::Singleton(pos)), feerate(depgraph.FeeRate(pos)) {}
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::SetInfo(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>> const&, unsigned int)
Line
Count
Source
377
421k
        transactions(SetType::Singleton(pos)), feerate(depgraph.FeeRate(pos)) {}
378
379
    /** Construct a SetInfo for a set of transactions in a depgraph. */
380
    explicit SetInfo(const DepGraph<SetType>& depgraph, const SetType& txn) noexcept :
381
        transactions(txn), feerate(depgraph.FeeRate(txn)) {}
382
383
    /** Add a transaction to this SetInfo (which must not yet be in it). */
384
    void Set(const DepGraph<SetType>& depgraph, DepGraphIndex pos) noexcept
385
    {
386
        Assume(!transactions[pos]);
387
        transactions.Set(pos);
388
        feerate += depgraph.FeeRate(pos);
389
    }
390
391
    /** Add the transactions of other to this SetInfo (no overlap allowed). */
392
    SetInfo& operator|=(const SetInfo& other) noexcept
393
35.4M
    {
394
35.4M
        Assume(!transactions.Overlaps(other.transactions));
395
35.4M
        transactions |= other.transactions;
396
35.4M
        feerate += other.feerate;
397
35.4M
        return *this;
398
35.4M
    }
cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned long>>::operator|=(cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned long>> const&)
Line
Count
Source
393
10.6M
    {
394
10.6M
        Assume(!transactions.Overlaps(other.transactions));
395
10.6M
        transactions |= other.transactions;
396
10.6M
        feerate += other.feerate;
397
10.6M
        return *this;
398
10.6M
    }
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::operator|=(cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned int, 2u>> const&)
Line
Count
Source
393
10.3M
    {
394
10.3M
        Assume(!transactions.Overlaps(other.transactions));
395
10.3M
        transactions |= other.transactions;
396
10.3M
        feerate += other.feerate;
397
10.3M
        return *this;
398
10.3M
    }
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::operator|=(cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 8u>> const&)
Line
Count
Source
393
10.3M
    {
394
10.3M
        Assume(!transactions.Overlaps(other.transactions));
395
10.3M
        transactions |= other.transactions;
396
10.3M
        feerate += other.feerate;
397
10.3M
        return *this;
398
10.3M
    }
cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned int>>::operator|=(cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned int>> const&)
Line
Count
Source
393
2.00M
    {
394
2.00M
        Assume(!transactions.Overlaps(other.transactions));
395
2.00M
        transactions |= other.transactions;
396
2.00M
        feerate += other.feerate;
397
2.00M
        return *this;
398
2.00M
    }
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::operator|=(cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 4u>> const&)
Line
Count
Source
393
2.00M
    {
394
2.00M
        Assume(!transactions.Overlaps(other.transactions));
395
2.00M
        transactions |= other.transactions;
396
2.00M
        feerate += other.feerate;
397
2.00M
        return *this;
398
2.00M
    }
399
400
    /** Remove the transactions of other from this SetInfo (which must be a subset). */
401
    SetInfo& operator-=(const SetInfo& other) noexcept
402
15.8M
    {
403
15.8M
        Assume(other.transactions.IsSubsetOf(transactions));
404
15.8M
        transactions -= other.transactions;
405
15.8M
        feerate -= other.feerate;
406
15.8M
        return *this;
407
15.8M
    }
cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned long>>::operator-=(cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned long>> const&)
Line
Count
Source
402
4.96M
    {
403
4.96M
        Assume(other.transactions.IsSubsetOf(transactions));
404
4.96M
        transactions -= other.transactions;
405
4.96M
        feerate -= other.feerate;
406
4.96M
        return *this;
407
4.96M
    }
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned int, 2u>>::operator-=(cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned int, 2u>> const&)
Line
Count
Source
402
4.76M
    {
403
4.76M
        Assume(other.transactions.IsSubsetOf(transactions));
404
4.76M
        transactions -= other.transactions;
405
4.76M
        feerate -= other.feerate;
406
4.76M
        return *this;
407
4.76M
    }
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 8u>>::operator-=(cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 8u>> const&)
Line
Count
Source
402
4.74M
    {
403
4.74M
        Assume(other.transactions.IsSubsetOf(transactions));
404
4.74M
        transactions -= other.transactions;
405
4.74M
        feerate -= other.feerate;
406
4.74M
        return *this;
407
4.74M
    }
cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned int>>::operator-=(cluster_linearize::SetInfo<bitset_detail::IntBitSet<unsigned int>> const&)
Line
Count
Source
402
705k
    {
403
705k
        Assume(other.transactions.IsSubsetOf(transactions));
404
705k
        transactions -= other.transactions;
405
705k
        feerate -= other.feerate;
406
705k
        return *this;
407
705k
    }
cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 4u>>::operator-=(cluster_linearize::SetInfo<bitset_detail::MultiIntBitSet<unsigned char, 4u>> const&)
Line
Count
Source
402
703k
    {
403
703k
        Assume(other.transactions.IsSubsetOf(transactions));
404
703k
        transactions -= other.transactions;
405
703k
        feerate -= other.feerate;
406
703k
        return *this;
407
703k
    }
408
409
    /** Compute the difference between this and other SetInfo (which must be a subset). */
410
    SetInfo operator-(const SetInfo& other) const noexcept
411
    {
412
        Assume(other.transactions.IsSubsetOf(transactions));
413
        return {transactions - other.transactions, feerate - other.feerate};
414
    }
415
416
    /** Swap two SetInfo objects. */
417
    friend void swap(SetInfo& a, SetInfo& b) noexcept
418
    {
419
        swap(a.transactions, b.transactions);
420
        swap(a.feerate, b.feerate);
421
    }
422
423
    /** Permit equality testing. */
424
    friend bool operator==(const SetInfo&, const SetInfo&) noexcept = default;
425
};
426
427
/** Compute the chunks of linearization as SetInfos. */
428
template<typename SetType>
429
std::vector<SetInfo<SetType>> ChunkLinearizationInfo(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> linearization) noexcept
430
60.5k
{
431
60.5k
    std::vector<SetInfo<SetType>> ret;
432
301k
    for (DepGraphIndex i : linearization) {
433
        /** The new chunk to be added, initially a singleton. */
434
301k
        SetInfo<SetType> new_chunk(depgraph, i);
435
        // As long as the new chunk has a higher feerate than the last chunk so far, absorb it.
436
330k
        while (!ret.empty() && ByRatio{new_chunk.feerate} > ByRatio{ret.back().feerate}) {
437
28.4k
            new_chunk |= ret.back();
438
28.4k
            ret.pop_back();
439
28.4k
        }
440
        // Actually move that new chunk into the chunking.
441
301k
        ret.emplace_back(std::move(new_chunk));
442
301k
    }
443
60.5k
    return ret;
444
60.5k
}
445
446
/** Compute the feerates of the chunks of linearization. Identical to ChunkLinearizationInfo, but
447
 *  only returns the chunk feerates, not the corresponding transaction sets. */
448
template<typename SetType>
449
std::vector<FeeFrac> ChunkLinearization(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> linearization) noexcept
450
410
{
451
410
    std::vector<FeeFrac> ret;
452
2.13k
    for (DepGraphIndex i : linearization) {
453
        /** The new chunk to be added, initially a singleton. */
454
2.13k
        auto new_chunk = depgraph.FeeRate(i);
455
        // As long as the new chunk has a higher feerate than the last chunk so far, absorb it.
456
3.00k
        while (!ret.empty() && ByRatio{new_chunk} > ByRatio{ret.back()}) {
457
875
            new_chunk += ret.back();
458
875
            ret.pop_back();
459
875
        }
460
        // Actually move that new chunk into the chunking.
461
2.13k
        ret.push_back(std::move(new_chunk));
462
2.13k
    }
463
410
    return ret;
464
410
}
465
466
/** Concept for function objects that return std::strong_ordering when invoked with two Args. */
467
template<typename F, typename Arg>
468
concept StrongComparator =
469
    std::regular_invocable<F, Arg, Arg> &&
470
    std::is_same_v<std::invoke_result_t<F, Arg, Arg>, std::strong_ordering>;
471
472
/** Simple default transaction ordering function for SpanningForestState::GetLinearization() and
473
 *  Linearize(), which just sorts by DepGraphIndex. */
474
using IndexTxOrder = std::compare_three_way;
475
476
/** A default cost model for SFL for SetType=BitSet<64>, based on benchmarks.
477
 *
478
 * The numbers here were obtained in February 2026 by:
479
 * - For a variety of machines:
480
 *   - Running a fixed collection of ~385000 clusters found through random generation and fuzzing,
481
 *     optimizing for difficulty of linearization.
482
 *     - Linearize each ~3000 times, with different random seeds. Sometimes without input
483
 *       linearization, sometimes with a bad one.
484
 *       - Gather cycle counts for each of the operations included in this cost model,
485
 *         broken down by their parameters.
486
 *   - Correct the data by subtracting the runtime of obtaining the cycle count.
487
 *   - Drop the 5% top and bottom samples from each cycle count dataset, and compute the average
488
 *     of the remaining samples.
489
 *   - For each operation, fit a least-squares linear function approximation through the samples.
490
 * - Rescale all machine expressions to make their total time match, as we only care about
491
 *   relative cost of each operation.
492
 * - Take the per-operation average of operation expressions across all machines, to construct
493
 *   expressions for an average machine.
494
 * - Approximate the result with integer coefficients. Each cost unit corresponds to somewhere
495
 *   between 0.5 ns and 2.5 ns, depending on the hardware.
496
 */
497
class SFLDefaultCostModel
498
{
499
    uint64_t m_cost{0};
500
501
public:
502
191k
    inline void InitializeBegin() noexcept {}
503
    inline void InitializeEnd(int num_txns, int num_deps) noexcept
504
191k
    {
505
         // Cost of initialization.
506
191k
         m_cost += 39 * num_txns;
507
         // Cost of producing linearization at the end.
508
191k
         m_cost += 48 * num_txns + 4 * num_deps;
509
191k
    }
510
191k
    inline void GetLinearizationBegin() noexcept {}
511
    inline void GetLinearizationEnd(int num_txns, int num_deps) noexcept
512
191k
    {
513
        // Note that we account for the cost of the final linearization at the beginning (see
514
        // InitializeEnd), because the cost budget decision needs to be made before calling
515
        // GetLinearization.
516
        // This function exists here to allow overriding it easily for benchmark purposes.
517
191k
    }
518
98.7k
    inline void MakeTopologicalBegin() noexcept {}
519
    inline void MakeTopologicalEnd(int num_chunks, int num_steps) noexcept
520
98.7k
    {
521
98.7k
        m_cost += 20 * num_chunks + 28 * num_steps;
522
98.7k
    }
523
191k
    inline void StartOptimizingBegin() noexcept {}
524
191k
    inline void StartOptimizingEnd(int num_chunks) noexcept { m_cost += 13 * num_chunks; }
525
4.58M
    inline void ActivateBegin() noexcept {}
526
4.58M
    inline void ActivateEnd(int num_deps) noexcept { m_cost += 10 * num_deps + 1; }
527
1.30M
    inline void DeactivateBegin() noexcept {}
528
1.30M
    inline void DeactivateEnd(int num_deps) noexcept { m_cost += 11 * num_deps + 8; }
529
4.58M
    inline void MergeChunksBegin() noexcept {}
530
4.58M
    inline void MergeChunksMid(int num_txns) noexcept { m_cost += 2 * num_txns; }
531
4.58M
    inline void MergeChunksEnd(int num_steps) noexcept { m_cost += 3 * num_steps + 5; }
532
9.22M
    inline void PickMergeCandidateBegin() noexcept {}
533
9.22M
    inline void PickMergeCandidateEnd(int num_steps) noexcept { m_cost += 8 * num_steps; }
534
2.55M
    inline void PickChunkToOptimizeBegin() noexcept {}
535
2.55M
    inline void PickChunkToOptimizeEnd(int num_steps) noexcept { m_cost += num_steps + 4; }
536
2.55M
    inline void PickDependencyToSplitBegin() noexcept {}
537
2.55M
    inline void PickDependencyToSplitEnd(int num_txns) noexcept { m_cost += 8 * num_txns + 9; }
538
191k
    inline void StartMinimizingBegin() noexcept {}
539
191k
    inline void StartMinimizingEnd(int num_chunks) noexcept { m_cost += 18 * num_chunks; }
540
2.12M
    inline void MinimizeStepBegin() noexcept {}
541
2.12M
    inline void MinimizeStepMid(int num_txns) noexcept { m_cost += 11 * num_txns + 11; }
542
286k
    inline void MinimizeStepEnd(bool split) noexcept { m_cost += 17 * split + 7; }
543
544
5.07M
    inline uint64_t GetCost() const noexcept { return m_cost; }
545
};
546
547
/** Class to represent the internal state of the spanning-forest linearization (SFL) algorithm.
548
 *
549
 * At all times, each dependency is marked as either "active" or "inactive". The subset of active
550
 * dependencies is the state of the SFL algorithm. The implementation maintains several other
551
 * values to speed up operations, but everything is ultimately a function of what that subset of
552
 * active dependencies is.
553
 *
554
 * Given such a subset, define a chunk as the set of transactions that are connected through active
555
 * dependencies (ignoring their parent/child direction). Thus, every state implies a particular
556
 * partitioning of the graph into chunks (including potential singletons). In the extreme, each
557
 * transaction may be in its own chunk, or in the other extreme all transactions may form a single
558
 * chunk. A chunk's feerate is its total fee divided by its total size.
559
 *
560
 * The algorithm consists of switching dependencies between active and inactive. The final
561
 * linearization that is produced at the end consists of these chunks, sorted from high to low
562
 * feerate, each individually sorted in an arbitrary but topological (= no child before parent)
563
 * way.
564
 *
565
 * We define four quality properties the state can have:
566
 *
567
 * - acyclic: The state is acyclic whenever no cycle of active dependencies exists within the
568
 *            graph, ignoring the parent/child direction. This is equivalent to saying that within
569
 *            each chunk the set of active dependencies form a tree, and thus the overall set of
570
 *            active dependencies in the graph form a spanning forest, giving the algorithm its
571
 *            name. Being acyclic is also equivalent to every chunk of N transactions having
572
 *            exactly N-1 active dependencies.
573
 *
574
 *            For example in a diamond graph, D->{B,C}->A, the 4 dependencies cannot be
575
 *            simultaneously active. If at least one is inactive, the state is acyclic.
576
 *
577
 *            The algorithm maintains an acyclic state at *all* times as an invariant. This implies
578
 *            that activating a dependency always corresponds to merging two chunks, and that
579
 *            deactivating one always corresponds to splitting two chunks.
580
 *
581
 * - topological: We say the state is topological whenever it is acyclic and no inactive dependency
582
 *                exists between two distinct chunks such that the child chunk has higher or equal
583
 *                feerate than the parent chunk.
584
 *
585
 *                The relevance is that whenever the state is topological, the produced output
586
 *                linearization will be topological too (i.e., not have children before parents).
587
 *                Note that the "or equal" part of the definition matters: if not, one can end up
588
 *                in a situation with mutually-dependent equal-feerate chunks that cannot be
589
 *                linearized. For example C->{A,B} and D->{A,B}, with C->A and D->B active. The AC
590
 *                chunk depends on DB through C->B, and the BD chunk depends on AC through D->A.
591
 *                Merging them into a single ABCD chunk fixes this.
592
 *
593
 *                The algorithm attempts to keep the state topological as much as possible, so it
594
 *                can be interrupted to produce an output whenever, but will sometimes need to
595
 *                temporarily deviate from it when improving the state.
596
 *
597
 * - optimal: For every active dependency, define its top and bottom set as the set of transactions
598
 *            in the chunks that would result if the dependency were deactivated; the top being the
599
 *            one with the dependency's parent, and the bottom being the one with the child. Note
600
 *            that due to acyclicity, every deactivation splits a chunk exactly in two.
601
 *
602
 *            We say the state is optimal whenever it is topological and it has no active
603
 *            dependency whose top feerate is strictly higher than its bottom feerate. The
604
 *            relevance is that it can be proven that whenever the state is optimal, the produced
605
 *            linearization will also be optimal (in the convexified feerate diagram sense). It can
606
 *            also be proven that for every graph at least one optimal state exists.
607
 *
608
 *            Note that it is possible for the SFL state to not be optimal, but the produced
609
 *            linearization to still be optimal. This happens when the chunks of a state are
610
 *            identical to those of an optimal state, but the exact set of active dependencies
611
 *            within a chunk differ in such a way that the state optimality condition is not
612
 *            satisfied. Thus, the state being optimal is more a "the eventual output is *known*
613
 *            to be optimal".
614
 *
615
 * - minimal: We say the state is minimal when it is:
616
 *            - acyclic
617
 *            - topological, except that inactive dependencies between equal-feerate chunks are
618
 *              allowed as long as they do not form a loop.
619
 *            - like optimal, no active dependencies whose top feerate is strictly higher than
620
 *              the bottom feerate are allowed.
621
 *            - no chunk contains a proper non-empty subset which includes all its own in-chunk
622
 *              dependencies of the same feerate as the chunk itself.
623
 *
624
 *            A minimal state effectively corresponds to an optimal state, where every chunk has
625
 *            been split into its minimal equal-feerate components.
626
 *
627
 *            The algorithm terminates whenever a minimal state is reached.
628
 *
629
 *
630
 * This leads to the following high-level algorithm:
631
 * - Start with all dependencies inactive, and thus all transactions in their own chunk. This is
632
 *   definitely acyclic.
633
 * - Activate dependencies (merging chunks) until the state is topological.
634
 * - Loop until optimal (no dependencies with higher-feerate top than bottom), or time runs out:
635
 *   - Deactivate a violating dependency, potentially making the state non-topological.
636
 *   - Activate other dependencies to make the state topological again.
637
 * - If there is time left and the state is optimal:
638
 *   - Attempt to split chunks into equal-feerate parts without mutual dependencies between them.
639
 *     When this succeeds, recurse into them.
640
 *   - If no such chunks can be found, the state is minimal.
641
 * - Output the chunks from high to low feerate, each internally sorted topologically.
642
 *
643
 * When merging, we always either:
644
 * - Merge upwards: merge a chunk with the lowest-feerate other chunk it depends on, among those
645
 *                  with lower or equal feerate than itself.
646
 * - Merge downwards: merge a chunk with the highest-feerate other chunk that depends on it, among
647
 *                    those with higher or equal feerate than itself.
648
 *
649
 * Using these strategies in the improvement loop above guarantees that the output linearization
650
 * after a deactivate + merge step is never worse or incomparable (in the convexified feerate
651
 * diagram sense) than the output linearization that would be produced before the step. With that,
652
 * we can refine the high-level algorithm to:
653
 * - Start with all dependencies inactive.
654
 * - Perform merges as described until none are possible anymore, making the state topological.
655
 * - Loop until optimal or time runs out:
656
 *   - Pick a dependency D to deactivate among those with higher feerate top than bottom.
657
 *   - Deactivate D, causing the chunk it is in to split into top T and bottom B.
658
 *   - Do an upwards merge of T, if possible. If so, repeat the same with the merged result.
659
 *   - Do a downwards merge of B, if possible. If so, repeat the same with the merged result.
660
 * - Split chunks further to obtain a minimal state, see below.
661
 * - Output the chunks from high to low feerate, each internally sorted topologically.
662
 *
663
 * Instead of performing merges arbitrarily to make the initial state topological, it is possible
664
 * to do so guided by an existing linearization. This has the advantage that the state's would-be
665
 * output linearization is immediately as good as the existing linearization it was based on:
666
 * - Start with all dependencies inactive.
667
 * - For each transaction t in the existing linearization:
668
 *   - Find the chunk C that transaction is in (which will be singleton).
669
 *   - Do an upwards merge of C, if possible. If so, repeat the same with the merged result.
670
 * No downwards merges are needed in this case.
671
 *
672
 * After reaching an optimal state, it can be transformed into a minimal state by attempting to
673
 * split chunks further into equal-feerate parts. To do so, pick a specific transaction in each
674
 * chunk (the pivot), and rerun the above split-then-merge procedure again:
675
 * - first, while pretending the pivot transaction has an infinitesimally higher (or lower) fee
676
 *   than it really has. If a split exists with the pivot in the top part (or bottom part), this
677
 *   will find it.
678
 * - if that fails to split, repeat while pretending the pivot transaction has an infinitesimally
679
 *   lower (or higher) fee. If a split exists with the pivot in the bottom part (or top part), this
680
 *   will find it.
681
 * - if either succeeds, repeat the procedure for the newly found chunks to split them further.
682
 *   If not, the chunk is already minimal.
683
 * If the chunk can be split into equal-feerate parts, then the pivot must exist in either the top
684
 * or bottom part of that potential split. By trying both with the same pivot, if a split exists,
685
 * it will be found.
686
 *
687
 * What remains to be specified are a number of heuristics:
688
 *
689
 * - How to decide which chunks to merge:
690
 *   - The merge upwards and downward rules specify that the lowest-feerate respectively
691
 *     highest-feerate candidate chunk is merged with, but if there are multiple equal-feerate
692
 *     candidates, a uniformly random one among them is picked.
693
 *
694
 * - How to decide what dependency to activate (when merging chunks):
695
 *   - After picking two chunks to be merged (see above), a uniformly random dependency between the
696
 *     two chunks is activated.
697
 *
698
 * - How to decide which chunk to find a dependency to split in:
699
 *   - A round-robin queue of chunks to improve is maintained. The initial ordering of this queue
700
 *     is uniformly randomly permuted.
701
 *
702
 * - How to decide what dependency to deactivate (when splitting chunks):
703
 *   - Inside the selected chunk (see above), among the dependencies whose top feerate is strictly
704
 *     higher than its bottom feerate in the selected chunk, if any, a uniformly random dependency
705
 *     is deactivated.
706
 *   - After every split, it is possible that the top and the bottom chunk merge with each other
707
 *     again in the merge sequence (through a top->bottom dependency, not through the deactivated
708
 *     one, which was bottom->top). Call this a self-merge. If a self-merge does not occur after
709
 *     a split, the resulting linearization is strictly improved (the area under the convexified
710
 *     feerate diagram increases by at least gain/2), while self-merges do not change it.
711
 *
712
 * - How to decide the exact output linearization:
713
 *   - When there are multiple equal-feerate chunks with no dependencies between them, pick the
714
 *     smallest one first. If there are multiple smallest ones, pick the one that contains the
715
 *     last transaction (according to the provided fallback order) last (note that this is not the
716
 *     same as picking the chunk with the first transaction first).
717
 *   - Within chunks, pick among all transactions without missing dependencies the one with the
718
 *     highest individual feerate. If there are multiple ones with the same individual feerate,
719
 *     pick the smallest first. If there are multiple with the same fee and size, pick the one
720
 *     that sorts first according to the fallback order first.
721
 */
722
template<typename SetType, typename CostModel = SFLDefaultCostModel>
723
class SpanningForestState
724
{
725
private:
726
    /** Internal RNG. */
727
    InsecureRandomContext m_rng;
728
729
    /** Data type to represent indexing into m_tx_data. */
730
    using TxIdx = DepGraphIndex;
731
    /** Data type to represent indexing into m_set_info. Use the smallest type possible to improve
732
     *  cache locality. */
733
    using SetIdx = std::conditional_t<(SetType::Size() <= 0xff),
734
                                      uint8_t,
735
                                      std::conditional_t<(SetType::Size() <= 0xffff),
736
                                                         uint16_t,
737
                                                         uint32_t>>;
738
    /** An invalid SetIdx. */
739
    static constexpr SetIdx INVALID_SET_IDX = SetIdx(-1);
740
741
    /** Structure with information about a single transaction. */
742
    struct TxData {
743
        /** The top set for every active child dependency this transaction has, indexed by child
744
         *  TxIdx. Only defined for indexes in active_children. */
745
        std::array<SetIdx, SetType::Size()> dep_top_idx;
746
        /** The set of parent transactions of this transaction. Immutable after construction. */
747
        SetType parents;
748
        /** The set of child transactions of this transaction. Immutable after construction. */
749
        SetType children;
750
        /** The set of child transactions reachable through an active dependency. */
751
        SetType active_children;
752
        /** Which chunk this transaction belongs to. */
753
        SetIdx chunk_idx;
754
    };
755
756
    /** The set of all TxIdx's of transactions in the cluster indexing into m_tx_data. */
757
    SetType m_transaction_idxs;
758
    /** The set of all chunk SetIdx's. This excludes the SetIdxs that refer to active
759
     *  dependencies' tops. */
760
    SetType m_chunk_idxs;
761
    /** The set of all SetIdx's that appear in m_suboptimal_chunks. Note that they do not need to
762
     *  be chunks: some of these sets may have been converted to a dependency's top set since being
763
     *  added to m_suboptimal_chunks. */
764
    SetType m_suboptimal_idxs;
765
    /** Information about each transaction (and chunks). Keeps the "holes" from DepGraph during
766
     *  construction. Indexed by TxIdx. */
767
    std::vector<TxData> m_tx_data;
768
    /** Information about each set (chunk, or active dependency top set). Indexed by SetIdx. */
769
    std::vector<SetInfo<SetType>> m_set_info;
770
    /** For each chunk, indexed by SetIdx, the set of out-of-chunk reachable transactions, in the
771
     *  upwards (.first) and downwards (.second) direction. */
772
    std::vector<std::pair<SetType, SetType>> m_reachable;
773
    /** A FIFO of chunk SetIdxs for chunks that may be improved still. */
774
    VecDeque<SetIdx> m_suboptimal_chunks;
775
    /** A FIFO of chunk indexes with a pivot transaction in them, and a flag to indicate their
776
     *  status:
777
     *  - bit 1: currently attempting to move the pivot down, rather than up.
778
     *  - bit 2: this is the second stage, so we have already tried moving the pivot in the other
779
     *           direction.
780
     */
781
    VecDeque<std::tuple<SetIdx, TxIdx, unsigned>> m_nonminimal_chunks;
782
783
    /** The DepGraph we are trying to linearize. */
784
    const DepGraph<SetType>& m_depgraph;
785
786
    /** Accounting for the cost of this computation. */
787
    CostModel m_cost;
788
789
    /** Pick a random transaction within a set (which must be non-empty). */
790
    TxIdx PickRandomTx(const SetType& tx_idxs) noexcept
791
1.79M
    {
792
1.79M
        Assume(tx_idxs.Any());
793
1.79M
        unsigned pos = m_rng.randrange<unsigned>(tx_idxs.Count());
794
3.71M
        for (auto tx_idx : tx_idxs) {
795
3.71M
            if (pos == 0) return tx_idx;
796
1.92M
            --pos;
797
1.92M
        }
798
0
        Assume(false);
799
0
        return TxIdx(-1);
800
1.79M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::PickRandomTx(bitset_detail::IntBitSet<unsigned long> const&)
Line
Count
Source
791
545k
    {
792
545k
        Assume(tx_idxs.Any());
793
545k
        unsigned pos = m_rng.randrange<unsigned>(tx_idxs.Count());
794
1.11M
        for (auto tx_idx : tx_idxs) {
795
1.11M
            if (pos == 0) return tx_idx;
796
565k
            --pos;
797
565k
        }
798
0
        Assume(false);
799
0
        return TxIdx(-1);
800
545k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::PickRandomTx(bitset_detail::MultiIntBitSet<unsigned int, 2u> const&)
Line
Count
Source
791
484k
    {
792
484k
        Assume(tx_idxs.Any());
793
484k
        unsigned pos = m_rng.randrange<unsigned>(tx_idxs.Count());
794
989k
        for (auto tx_idx : tx_idxs) {
795
989k
            if (pos == 0) return tx_idx;
796
505k
            --pos;
797
505k
        }
798
0
        Assume(false);
799
0
        return TxIdx(-1);
800
484k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::PickRandomTx(bitset_detail::MultiIntBitSet<unsigned char, 8u> const&)
Line
Count
Source
791
484k
    {
792
484k
        Assume(tx_idxs.Any());
793
484k
        unsigned pos = m_rng.randrange<unsigned>(tx_idxs.Count());
794
991k
        for (auto tx_idx : tx_idxs) {
795
991k
            if (pos == 0) return tx_idx;
796
507k
            --pos;
797
507k
        }
798
0
        Assume(false);
799
0
        return TxIdx(-1);
800
484k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::PickRandomTx(bitset_detail::IntBitSet<unsigned int> const&)
Line
Count
Source
791
138k
    {
792
138k
        Assume(tx_idxs.Any());
793
138k
        unsigned pos = m_rng.randrange<unsigned>(tx_idxs.Count());
794
309k
        for (auto tx_idx : tx_idxs) {
795
309k
            if (pos == 0) return tx_idx;
796
170k
            --pos;
797
170k
        }
798
0
        Assume(false);
799
0
        return TxIdx(-1);
800
138k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::PickRandomTx(bitset_detail::MultiIntBitSet<unsigned char, 4u> const&)
Line
Count
Source
791
138k
    {
792
138k
        Assume(tx_idxs.Any());
793
138k
        unsigned pos = m_rng.randrange<unsigned>(tx_idxs.Count());
794
309k
        for (auto tx_idx : tx_idxs) {
795
309k
            if (pos == 0) return tx_idx;
796
171k
            --pos;
797
171k
        }
798
0
        Assume(false);
799
0
        return TxIdx(-1);
800
138k
    }
801
802
    /** Find the set of out-of-chunk transactions reachable from tx_idxs, both in upwards and
803
     *  downwards direction. Only used by SanityCheck to verify the precomputed reachable sets in
804
     *  m_reachable that are maintained by Activate/Deactivate. */
805
    std::pair<SetType, SetType> GetReachable(const SetType& tx_idxs) const noexcept
806
    {
807
        SetType parents, children;
808
        for (auto tx_idx : tx_idxs) {
809
            const auto& tx_data = m_tx_data[tx_idx];
810
            parents |= tx_data.parents;
811
            children |= tx_data.children;
812
        }
813
        return {parents - tx_idxs, children - tx_idxs};
814
    }
815
816
    /** Make the inactive dependency from child to parent, which must not be in the same chunk
817
     *  already, active. Returns the merged chunk idx. */
818
    SetIdx Activate(TxIdx parent_idx, TxIdx child_idx) noexcept
819
4.58M
    {
820
4.58M
        m_cost.ActivateBegin();
821
        // Gather and check information about the parent and child transactions.
822
4.58M
        auto& parent_data = m_tx_data[parent_idx];
823
4.58M
        auto& child_data = m_tx_data[child_idx];
824
4.58M
        Assume(parent_data.children[child_idx]);
825
4.58M
        Assume(!parent_data.active_children[child_idx]);
826
        // Get the set index of the chunks the parent and child are currently in. The parent chunk
827
        // will become the top set of the newly activated dependency, while the child chunk will be
828
        // grown to become the merged chunk.
829
4.58M
        auto parent_chunk_idx = parent_data.chunk_idx;
830
4.58M
        auto child_chunk_idx = child_data.chunk_idx;
831
4.58M
        Assume(parent_chunk_idx != child_chunk_idx);
832
4.58M
        Assume(m_chunk_idxs[parent_chunk_idx]);
833
4.58M
        Assume(m_chunk_idxs[child_chunk_idx]);
834
4.58M
        auto& top_info = m_set_info[parent_chunk_idx];
835
4.58M
        auto& bottom_info = m_set_info[child_chunk_idx];
836
837
        // Consider the following example:
838
        //
839
        //    A           A     There are two chunks, ABC and DEF, and the inactive E->C dependency
840
        //   / \         / \    is activated, resulting in a single chunk ABCDEF.
841
        //  B   C       B   C
842
        //      :  ==>      |   Dependency | top set before | top set after | change
843
        //  D   E       D   E   B->A       | AC             | ACDEF         | +DEF
844
        //   \ /         \ /    C->A       | AB             | AB            |
845
        //    F           F     F->D       | D              | D             |
846
        //                      F->E       | E              | ABCE          | +ABC
847
        //
848
        // The common pattern here is that any dependency which has the parent or child of the
849
        // dependency being activated (E->C here) in its top set, will have the opposite part added
850
        // to it. This is true for B->A and F->E, but not for C->A and F->D.
851
        //
852
        // Traverse the old parent chunk top_info (ABC in example), and add bottom_info (DEF) to
853
        // every dependency's top set which has the parent (C) in it. At the same time, change the
854
        // chunk_idx for each to be child_chunk_idx, which becomes the set for the merged chunk.
855
39.0M
        for (auto tx_idx : top_info.transactions) {
856
39.0M
            auto& tx_data = m_tx_data[tx_idx];
857
39.0M
            tx_data.chunk_idx = child_chunk_idx;
858
39.0M
            for (auto dep_child_idx : tx_data.active_children) {
859
34.5M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
860
34.5M
                if (dep_top_info.transactions[parent_idx]) dep_top_info |= bottom_info;
861
34.5M
            }
862
39.0M
        }
863
        // Traverse the old child chunk bottom_info (DEF in example), and add top_info (ABC) to
864
        // every dependency's top set which has the child (E) in it.
865
20.0M
        for (auto tx_idx : bottom_info.transactions) {
866
20.0M
            auto& tx_data = m_tx_data[tx_idx];
867
20.0M
            for (auto dep_child_idx : tx_data.active_children) {
868
15.4M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
869
15.4M
                if (dep_top_info.transactions[child_idx]) dep_top_info |= top_info;
870
15.4M
            }
871
20.0M
        }
872
        // Merge top_info into bottom_info, which becomes the merged chunk.
873
4.58M
        bottom_info |= top_info;
874
        // Compute merged sets of reachable transactions from the new chunk, based on the input
875
        // chunks' reachable sets.
876
4.58M
        m_reachable[child_chunk_idx].first |= m_reachable[parent_chunk_idx].first;
877
4.58M
        m_reachable[child_chunk_idx].second |= m_reachable[parent_chunk_idx].second;
878
4.58M
        m_reachable[child_chunk_idx].first -= bottom_info.transactions;
879
4.58M
        m_reachable[child_chunk_idx].second -= bottom_info.transactions;
880
        // Make parent chunk the set for the new active dependency.
881
4.58M
        parent_data.dep_top_idx[child_idx] = parent_chunk_idx;
882
4.58M
        parent_data.active_children.Set(child_idx);
883
4.58M
        m_chunk_idxs.Reset(parent_chunk_idx);
884
        // Return the newly merged chunk.
885
4.58M
        m_cost.ActivateEnd(/*num_deps=*/bottom_info.transactions.Count() - 1);
886
4.58M
        return child_chunk_idx;
887
4.58M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::Activate(unsigned int, unsigned int)
Line
Count
Source
819
1.31M
    {
820
1.31M
        m_cost.ActivateBegin();
821
        // Gather and check information about the parent and child transactions.
822
1.31M
        auto& parent_data = m_tx_data[parent_idx];
823
1.31M
        auto& child_data = m_tx_data[child_idx];
824
1.31M
        Assume(parent_data.children[child_idx]);
825
1.31M
        Assume(!parent_data.active_children[child_idx]);
826
        // Get the set index of the chunks the parent and child are currently in. The parent chunk
827
        // will become the top set of the newly activated dependency, while the child chunk will be
828
        // grown to become the merged chunk.
829
1.31M
        auto parent_chunk_idx = parent_data.chunk_idx;
830
1.31M
        auto child_chunk_idx = child_data.chunk_idx;
831
1.31M
        Assume(parent_chunk_idx != child_chunk_idx);
832
1.31M
        Assume(m_chunk_idxs[parent_chunk_idx]);
833
1.31M
        Assume(m_chunk_idxs[child_chunk_idx]);
834
1.31M
        auto& top_info = m_set_info[parent_chunk_idx];
835
1.31M
        auto& bottom_info = m_set_info[child_chunk_idx];
836
837
        // Consider the following example:
838
        //
839
        //    A           A     There are two chunks, ABC and DEF, and the inactive E->C dependency
840
        //   / \         / \    is activated, resulting in a single chunk ABCDEF.
841
        //  B   C       B   C
842
        //      :  ==>      |   Dependency | top set before | top set after | change
843
        //  D   E       D   E   B->A       | AC             | ACDEF         | +DEF
844
        //   \ /         \ /    C->A       | AB             | AB            |
845
        //    F           F     F->D       | D              | D             |
846
        //                      F->E       | E              | ABCE          | +ABC
847
        //
848
        // The common pattern here is that any dependency which has the parent or child of the
849
        // dependency being activated (E->C here) in its top set, will have the opposite part added
850
        // to it. This is true for B->A and F->E, but not for C->A and F->D.
851
        //
852
        // Traverse the old parent chunk top_info (ABC in example), and add bottom_info (DEF) to
853
        // every dependency's top set which has the parent (C) in it. At the same time, change the
854
        // chunk_idx for each to be child_chunk_idx, which becomes the set for the merged chunk.
855
12.1M
        for (auto tx_idx : top_info.transactions) {
856
12.1M
            auto& tx_data = m_tx_data[tx_idx];
857
12.1M
            tx_data.chunk_idx = child_chunk_idx;
858
12.1M
            for (auto dep_child_idx : tx_data.active_children) {
859
10.8M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
860
10.8M
                if (dep_top_info.transactions[parent_idx]) dep_top_info |= bottom_info;
861
10.8M
            }
862
12.1M
        }
863
        // Traverse the old child chunk bottom_info (DEF in example), and add top_info (ABC) to
864
        // every dependency's top set which has the child (E) in it.
865
5.94M
        for (auto tx_idx : bottom_info.transactions) {
866
5.94M
            auto& tx_data = m_tx_data[tx_idx];
867
5.94M
            for (auto dep_child_idx : tx_data.active_children) {
868
4.62M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
869
4.62M
                if (dep_top_info.transactions[child_idx]) dep_top_info |= top_info;
870
4.62M
            }
871
5.94M
        }
872
        // Merge top_info into bottom_info, which becomes the merged chunk.
873
1.31M
        bottom_info |= top_info;
874
        // Compute merged sets of reachable transactions from the new chunk, based on the input
875
        // chunks' reachable sets.
876
1.31M
        m_reachable[child_chunk_idx].first |= m_reachable[parent_chunk_idx].first;
877
1.31M
        m_reachable[child_chunk_idx].second |= m_reachable[parent_chunk_idx].second;
878
1.31M
        m_reachable[child_chunk_idx].first -= bottom_info.transactions;
879
1.31M
        m_reachable[child_chunk_idx].second -= bottom_info.transactions;
880
        // Make parent chunk the set for the new active dependency.
881
1.31M
        parent_data.dep_top_idx[child_idx] = parent_chunk_idx;
882
1.31M
        parent_data.active_children.Set(child_idx);
883
1.31M
        m_chunk_idxs.Reset(parent_chunk_idx);
884
        // Return the newly merged chunk.
885
1.31M
        m_cost.ActivateEnd(/*num_deps=*/bottom_info.transactions.Count() - 1);
886
1.31M
        return child_chunk_idx;
887
1.31M
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::Activate(unsigned int, unsigned int)
Line
Count
Source
819
1.25M
    {
820
1.25M
        m_cost.ActivateBegin();
821
        // Gather and check information about the parent and child transactions.
822
1.25M
        auto& parent_data = m_tx_data[parent_idx];
823
1.25M
        auto& child_data = m_tx_data[child_idx];
824
1.25M
        Assume(parent_data.children[child_idx]);
825
1.25M
        Assume(!parent_data.active_children[child_idx]);
826
        // Get the set index of the chunks the parent and child are currently in. The parent chunk
827
        // will become the top set of the newly activated dependency, while the child chunk will be
828
        // grown to become the merged chunk.
829
1.25M
        auto parent_chunk_idx = parent_data.chunk_idx;
830
1.25M
        auto child_chunk_idx = child_data.chunk_idx;
831
1.25M
        Assume(parent_chunk_idx != child_chunk_idx);
832
1.25M
        Assume(m_chunk_idxs[parent_chunk_idx]);
833
1.25M
        Assume(m_chunk_idxs[child_chunk_idx]);
834
1.25M
        auto& top_info = m_set_info[parent_chunk_idx];
835
1.25M
        auto& bottom_info = m_set_info[child_chunk_idx];
836
837
        // Consider the following example:
838
        //
839
        //    A           A     There are two chunks, ABC and DEF, and the inactive E->C dependency
840
        //   / \         / \    is activated, resulting in a single chunk ABCDEF.
841
        //  B   C       B   C
842
        //      :  ==>      |   Dependency | top set before | top set after | change
843
        //  D   E       D   E   B->A       | AC             | ACDEF         | +DEF
844
        //   \ /         \ /    C->A       | AB             | AB            |
845
        //    F           F     F->D       | D              | D             |
846
        //                      F->E       | E              | ABCE          | +ABC
847
        //
848
        // The common pattern here is that any dependency which has the parent or child of the
849
        // dependency being activated (E->C here) in its top set, will have the opposite part added
850
        // to it. This is true for B->A and F->E, but not for C->A and F->D.
851
        //
852
        // Traverse the old parent chunk top_info (ABC in example), and add bottom_info (DEF) to
853
        // every dependency's top set which has the parent (C) in it. At the same time, change the
854
        // chunk_idx for each to be child_chunk_idx, which becomes the set for the merged chunk.
855
11.2M
        for (auto tx_idx : top_info.transactions) {
856
11.2M
            auto& tx_data = m_tx_data[tx_idx];
857
11.2M
            tx_data.chunk_idx = child_chunk_idx;
858
11.2M
            for (auto dep_child_idx : tx_data.active_children) {
859
9.97M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
860
9.97M
                if (dep_top_info.transactions[parent_idx]) dep_top_info |= bottom_info;
861
9.97M
            }
862
11.2M
        }
863
        // Traverse the old child chunk bottom_info (DEF in example), and add top_info (ABC) to
864
        // every dependency's top set which has the child (E) in it.
865
5.92M
        for (auto tx_idx : bottom_info.transactions) {
866
5.92M
            auto& tx_data = m_tx_data[tx_idx];
867
5.92M
            for (auto dep_child_idx : tx_data.active_children) {
868
4.66M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
869
4.66M
                if (dep_top_info.transactions[child_idx]) dep_top_info |= top_info;
870
4.66M
            }
871
5.92M
        }
872
        // Merge top_info into bottom_info, which becomes the merged chunk.
873
1.25M
        bottom_info |= top_info;
874
        // Compute merged sets of reachable transactions from the new chunk, based on the input
875
        // chunks' reachable sets.
876
1.25M
        m_reachable[child_chunk_idx].first |= m_reachable[parent_chunk_idx].first;
877
1.25M
        m_reachable[child_chunk_idx].second |= m_reachable[parent_chunk_idx].second;
878
1.25M
        m_reachable[child_chunk_idx].first -= bottom_info.transactions;
879
1.25M
        m_reachable[child_chunk_idx].second -= bottom_info.transactions;
880
        // Make parent chunk the set for the new active dependency.
881
1.25M
        parent_data.dep_top_idx[child_idx] = parent_chunk_idx;
882
1.25M
        parent_data.active_children.Set(child_idx);
883
1.25M
        m_chunk_idxs.Reset(parent_chunk_idx);
884
        // Return the newly merged chunk.
885
1.25M
        m_cost.ActivateEnd(/*num_deps=*/bottom_info.transactions.Count() - 1);
886
1.25M
        return child_chunk_idx;
887
1.25M
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::Activate(unsigned int, unsigned int)
Line
Count
Source
819
1.25M
    {
820
1.25M
        m_cost.ActivateBegin();
821
        // Gather and check information about the parent and child transactions.
822
1.25M
        auto& parent_data = m_tx_data[parent_idx];
823
1.25M
        auto& child_data = m_tx_data[child_idx];
824
1.25M
        Assume(parent_data.children[child_idx]);
825
1.25M
        Assume(!parent_data.active_children[child_idx]);
826
        // Get the set index of the chunks the parent and child are currently in. The parent chunk
827
        // will become the top set of the newly activated dependency, while the child chunk will be
828
        // grown to become the merged chunk.
829
1.25M
        auto parent_chunk_idx = parent_data.chunk_idx;
830
1.25M
        auto child_chunk_idx = child_data.chunk_idx;
831
1.25M
        Assume(parent_chunk_idx != child_chunk_idx);
832
1.25M
        Assume(m_chunk_idxs[parent_chunk_idx]);
833
1.25M
        Assume(m_chunk_idxs[child_chunk_idx]);
834
1.25M
        auto& top_info = m_set_info[parent_chunk_idx];
835
1.25M
        auto& bottom_info = m_set_info[child_chunk_idx];
836
837
        // Consider the following example:
838
        //
839
        //    A           A     There are two chunks, ABC and DEF, and the inactive E->C dependency
840
        //   / \         / \    is activated, resulting in a single chunk ABCDEF.
841
        //  B   C       B   C
842
        //      :  ==>      |   Dependency | top set before | top set after | change
843
        //  D   E       D   E   B->A       | AC             | ACDEF         | +DEF
844
        //   \ /         \ /    C->A       | AB             | AB            |
845
        //    F           F     F->D       | D              | D             |
846
        //                      F->E       | E              | ABCE          | +ABC
847
        //
848
        // The common pattern here is that any dependency which has the parent or child of the
849
        // dependency being activated (E->C here) in its top set, will have the opposite part added
850
        // to it. This is true for B->A and F->E, but not for C->A and F->D.
851
        //
852
        // Traverse the old parent chunk top_info (ABC in example), and add bottom_info (DEF) to
853
        // every dependency's top set which has the parent (C) in it. At the same time, change the
854
        // chunk_idx for each to be child_chunk_idx, which becomes the set for the merged chunk.
855
11.2M
        for (auto tx_idx : top_info.transactions) {
856
11.2M
            auto& tx_data = m_tx_data[tx_idx];
857
11.2M
            tx_data.chunk_idx = child_chunk_idx;
858
11.2M
            for (auto dep_child_idx : tx_data.active_children) {
859
9.97M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
860
9.97M
                if (dep_top_info.transactions[parent_idx]) dep_top_info |= bottom_info;
861
9.97M
            }
862
11.2M
        }
863
        // Traverse the old child chunk bottom_info (DEF in example), and add top_info (ABC) to
864
        // every dependency's top set which has the child (E) in it.
865
5.90M
        for (auto tx_idx : bottom_info.transactions) {
866
5.90M
            auto& tx_data = m_tx_data[tx_idx];
867
5.90M
            for (auto dep_child_idx : tx_data.active_children) {
868
4.64M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
869
4.64M
                if (dep_top_info.transactions[child_idx]) dep_top_info |= top_info;
870
4.64M
            }
871
5.90M
        }
872
        // Merge top_info into bottom_info, which becomes the merged chunk.
873
1.25M
        bottom_info |= top_info;
874
        // Compute merged sets of reachable transactions from the new chunk, based on the input
875
        // chunks' reachable sets.
876
1.25M
        m_reachable[child_chunk_idx].first |= m_reachable[parent_chunk_idx].first;
877
1.25M
        m_reachable[child_chunk_idx].second |= m_reachable[parent_chunk_idx].second;
878
1.25M
        m_reachable[child_chunk_idx].first -= bottom_info.transactions;
879
1.25M
        m_reachable[child_chunk_idx].second -= bottom_info.transactions;
880
        // Make parent chunk the set for the new active dependency.
881
1.25M
        parent_data.dep_top_idx[child_idx] = parent_chunk_idx;
882
1.25M
        parent_data.active_children.Set(child_idx);
883
1.25M
        m_chunk_idxs.Reset(parent_chunk_idx);
884
        // Return the newly merged chunk.
885
1.25M
        m_cost.ActivateEnd(/*num_deps=*/bottom_info.transactions.Count() - 1);
886
1.25M
        return child_chunk_idx;
887
1.25M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::Activate(unsigned int, unsigned int)
Line
Count
Source
819
378k
    {
820
378k
        m_cost.ActivateBegin();
821
        // Gather and check information about the parent and child transactions.
822
378k
        auto& parent_data = m_tx_data[parent_idx];
823
378k
        auto& child_data = m_tx_data[child_idx];
824
378k
        Assume(parent_data.children[child_idx]);
825
378k
        Assume(!parent_data.active_children[child_idx]);
826
        // Get the set index of the chunks the parent and child are currently in. The parent chunk
827
        // will become the top set of the newly activated dependency, while the child chunk will be
828
        // grown to become the merged chunk.
829
378k
        auto parent_chunk_idx = parent_data.chunk_idx;
830
378k
        auto child_chunk_idx = child_data.chunk_idx;
831
378k
        Assume(parent_chunk_idx != child_chunk_idx);
832
378k
        Assume(m_chunk_idxs[parent_chunk_idx]);
833
378k
        Assume(m_chunk_idxs[child_chunk_idx]);
834
378k
        auto& top_info = m_set_info[parent_chunk_idx];
835
378k
        auto& bottom_info = m_set_info[child_chunk_idx];
836
837
        // Consider the following example:
838
        //
839
        //    A           A     There are two chunks, ABC and DEF, and the inactive E->C dependency
840
        //   / \         / \    is activated, resulting in a single chunk ABCDEF.
841
        //  B   C       B   C
842
        //      :  ==>      |   Dependency | top set before | top set after | change
843
        //  D   E       D   E   B->A       | AC             | ACDEF         | +DEF
844
        //   \ /         \ /    C->A       | AB             | AB            |
845
        //    F           F     F->D       | D              | D             |
846
        //                      F->E       | E              | ABCE          | +ABC
847
        //
848
        // The common pattern here is that any dependency which has the parent or child of the
849
        // dependency being activated (E->C here) in its top set, will have the opposite part added
850
        // to it. This is true for B->A and F->E, but not for C->A and F->D.
851
        //
852
        // Traverse the old parent chunk top_info (ABC in example), and add bottom_info (DEF) to
853
        // every dependency's top set which has the parent (C) in it. At the same time, change the
854
        // chunk_idx for each to be child_chunk_idx, which becomes the set for the merged chunk.
855
2.25M
        for (auto tx_idx : top_info.transactions) {
856
2.25M
            auto& tx_data = m_tx_data[tx_idx];
857
2.25M
            tx_data.chunk_idx = child_chunk_idx;
858
2.25M
            for (auto dep_child_idx : tx_data.active_children) {
859
1.87M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
860
1.87M
                if (dep_top_info.transactions[parent_idx]) dep_top_info |= bottom_info;
861
1.87M
            }
862
2.25M
        }
863
        // Traverse the old child chunk bottom_info (DEF in example), and add top_info (ABC) to
864
        // every dependency's top set which has the child (E) in it.
865
1.13M
        for (auto tx_idx : bottom_info.transactions) {
866
1.13M
            auto& tx_data = m_tx_data[tx_idx];
867
1.13M
            for (auto dep_child_idx : tx_data.active_children) {
868
753k
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
869
753k
                if (dep_top_info.transactions[child_idx]) dep_top_info |= top_info;
870
753k
            }
871
1.13M
        }
872
        // Merge top_info into bottom_info, which becomes the merged chunk.
873
378k
        bottom_info |= top_info;
874
        // Compute merged sets of reachable transactions from the new chunk, based on the input
875
        // chunks' reachable sets.
876
378k
        m_reachable[child_chunk_idx].first |= m_reachable[parent_chunk_idx].first;
877
378k
        m_reachable[child_chunk_idx].second |= m_reachable[parent_chunk_idx].second;
878
378k
        m_reachable[child_chunk_idx].first -= bottom_info.transactions;
879
378k
        m_reachable[child_chunk_idx].second -= bottom_info.transactions;
880
        // Make parent chunk the set for the new active dependency.
881
378k
        parent_data.dep_top_idx[child_idx] = parent_chunk_idx;
882
378k
        parent_data.active_children.Set(child_idx);
883
378k
        m_chunk_idxs.Reset(parent_chunk_idx);
884
        // Return the newly merged chunk.
885
378k
        m_cost.ActivateEnd(/*num_deps=*/bottom_info.transactions.Count() - 1);
886
378k
        return child_chunk_idx;
887
378k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::Activate(unsigned int, unsigned int)
Line
Count
Source
819
378k
    {
820
378k
        m_cost.ActivateBegin();
821
        // Gather and check information about the parent and child transactions.
822
378k
        auto& parent_data = m_tx_data[parent_idx];
823
378k
        auto& child_data = m_tx_data[child_idx];
824
378k
        Assume(parent_data.children[child_idx]);
825
378k
        Assume(!parent_data.active_children[child_idx]);
826
        // Get the set index of the chunks the parent and child are currently in. The parent chunk
827
        // will become the top set of the newly activated dependency, while the child chunk will be
828
        // grown to become the merged chunk.
829
378k
        auto parent_chunk_idx = parent_data.chunk_idx;
830
378k
        auto child_chunk_idx = child_data.chunk_idx;
831
378k
        Assume(parent_chunk_idx != child_chunk_idx);
832
378k
        Assume(m_chunk_idxs[parent_chunk_idx]);
833
378k
        Assume(m_chunk_idxs[child_chunk_idx]);
834
378k
        auto& top_info = m_set_info[parent_chunk_idx];
835
378k
        auto& bottom_info = m_set_info[child_chunk_idx];
836
837
        // Consider the following example:
838
        //
839
        //    A           A     There are two chunks, ABC and DEF, and the inactive E->C dependency
840
        //   / \         / \    is activated, resulting in a single chunk ABCDEF.
841
        //  B   C       B   C
842
        //      :  ==>      |   Dependency | top set before | top set after | change
843
        //  D   E       D   E   B->A       | AC             | ACDEF         | +DEF
844
        //   \ /         \ /    C->A       | AB             | AB            |
845
        //    F           F     F->D       | D              | D             |
846
        //                      F->E       | E              | ABCE          | +ABC
847
        //
848
        // The common pattern here is that any dependency which has the parent or child of the
849
        // dependency being activated (E->C here) in its top set, will have the opposite part added
850
        // to it. This is true for B->A and F->E, but not for C->A and F->D.
851
        //
852
        // Traverse the old parent chunk top_info (ABC in example), and add bottom_info (DEF) to
853
        // every dependency's top set which has the parent (C) in it. At the same time, change the
854
        // chunk_idx for each to be child_chunk_idx, which becomes the set for the merged chunk.
855
2.25M
        for (auto tx_idx : top_info.transactions) {
856
2.25M
            auto& tx_data = m_tx_data[tx_idx];
857
2.25M
            tx_data.chunk_idx = child_chunk_idx;
858
2.25M
            for (auto dep_child_idx : tx_data.active_children) {
859
1.87M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
860
1.87M
                if (dep_top_info.transactions[parent_idx]) dep_top_info |= bottom_info;
861
1.87M
            }
862
2.25M
        }
863
        // Traverse the old child chunk bottom_info (DEF in example), and add top_info (ABC) to
864
        // every dependency's top set which has the child (E) in it.
865
1.13M
        for (auto tx_idx : bottom_info.transactions) {
866
1.13M
            auto& tx_data = m_tx_data[tx_idx];
867
1.13M
            for (auto dep_child_idx : tx_data.active_children) {
868
755k
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
869
755k
                if (dep_top_info.transactions[child_idx]) dep_top_info |= top_info;
870
755k
            }
871
1.13M
        }
872
        // Merge top_info into bottom_info, which becomes the merged chunk.
873
378k
        bottom_info |= top_info;
874
        // Compute merged sets of reachable transactions from the new chunk, based on the input
875
        // chunks' reachable sets.
876
378k
        m_reachable[child_chunk_idx].first |= m_reachable[parent_chunk_idx].first;
877
378k
        m_reachable[child_chunk_idx].second |= m_reachable[parent_chunk_idx].second;
878
378k
        m_reachable[child_chunk_idx].first -= bottom_info.transactions;
879
378k
        m_reachable[child_chunk_idx].second -= bottom_info.transactions;
880
        // Make parent chunk the set for the new active dependency.
881
378k
        parent_data.dep_top_idx[child_idx] = parent_chunk_idx;
882
378k
        parent_data.active_children.Set(child_idx);
883
378k
        m_chunk_idxs.Reset(parent_chunk_idx);
884
        // Return the newly merged chunk.
885
378k
        m_cost.ActivateEnd(/*num_deps=*/bottom_info.transactions.Count() - 1);
886
378k
        return child_chunk_idx;
887
378k
    }
888
889
    /** Make a specified active dependency inactive. Returns the created parent and child chunk
890
     *  indexes. */
891
    std::pair<SetIdx, SetIdx> Deactivate(TxIdx parent_idx, TxIdx child_idx) noexcept
892
1.30M
    {
893
1.30M
        m_cost.DeactivateBegin();
894
        // Gather and check information about the parent transactions.
895
1.30M
        auto& parent_data = m_tx_data[parent_idx];
896
1.30M
        Assume(parent_data.children[child_idx]);
897
1.30M
        Assume(parent_data.active_children[child_idx]);
898
        // Get the top set of the active dependency (which will become the parent chunk) and the
899
        // chunk set the transactions are currently in (which will become the bottom chunk).
900
1.30M
        auto parent_chunk_idx = parent_data.dep_top_idx[child_idx];
901
1.30M
        auto child_chunk_idx = parent_data.chunk_idx;
902
1.30M
        Assume(parent_chunk_idx != child_chunk_idx);
903
1.30M
        Assume(m_chunk_idxs[child_chunk_idx]);
904
1.30M
        Assume(!m_chunk_idxs[parent_chunk_idx]); // top set, not a chunk
905
1.30M
        auto& top_info = m_set_info[parent_chunk_idx];
906
1.30M
        auto& bottom_info = m_set_info[child_chunk_idx];
907
908
        // Remove the active dependency.
909
1.30M
        parent_data.active_children.Reset(child_idx);
910
1.30M
        m_chunk_idxs.Set(parent_chunk_idx);
911
1.30M
        auto ntx = bottom_info.transactions.Count();
912
        // Subtract the top_info from the bottom_info, as it will become the child chunk.
913
1.30M
        bottom_info -= top_info;
914
        // See the comment above in Activate(). We perform the opposite operations here, removing
915
        // instead of adding. Simultaneously, aggregate the top/bottom's union of parents/children.
916
1.30M
        SetType top_parents, top_children;
917
15.8M
        for (auto tx_idx : top_info.transactions) {
918
15.8M
            auto& tx_data = m_tx_data[tx_idx];
919
15.8M
            tx_data.chunk_idx = parent_chunk_idx;
920
15.8M
            top_parents |= tx_data.parents;
921
15.8M
            top_children |= tx_data.children;
922
15.8M
            for (auto dep_child_idx : tx_data.active_children) {
923
14.5M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
924
14.5M
                if (dep_top_info.transactions[parent_idx]) dep_top_info -= bottom_info;
925
14.5M
            }
926
15.8M
        }
927
1.30M
        SetType bottom_parents, bottom_children;
928
12.3M
        for (auto tx_idx : bottom_info.transactions) {
929
12.3M
            auto& tx_data = m_tx_data[tx_idx];
930
12.3M
            bottom_parents |= tx_data.parents;
931
12.3M
            bottom_children |= tx_data.children;
932
12.3M
            for (auto dep_child_idx : tx_data.active_children) {
933
11.0M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
934
11.0M
                if (dep_top_info.transactions[child_idx]) dep_top_info -= top_info;
935
11.0M
            }
936
12.3M
        }
937
        // Compute the new sets of reachable transactions for each new chunk, based on the
938
        // top/bottom parents and children computed above.
939
1.30M
        m_reachable[parent_chunk_idx].first = top_parents - top_info.transactions;
940
1.30M
        m_reachable[parent_chunk_idx].second = top_children - top_info.transactions;
941
1.30M
        m_reachable[child_chunk_idx].first = bottom_parents - bottom_info.transactions;
942
1.30M
        m_reachable[child_chunk_idx].second = bottom_children - bottom_info.transactions;
943
        // Return the two new set idxs.
944
1.30M
        m_cost.DeactivateEnd(/*num_deps=*/ntx - 1);
945
1.30M
        return {parent_chunk_idx, child_chunk_idx};
946
1.30M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::Deactivate(unsigned int, unsigned int)
Line
Count
Source
892
405k
    {
893
405k
        m_cost.DeactivateBegin();
894
        // Gather and check information about the parent transactions.
895
405k
        auto& parent_data = m_tx_data[parent_idx];
896
405k
        Assume(parent_data.children[child_idx]);
897
405k
        Assume(parent_data.active_children[child_idx]);
898
        // Get the top set of the active dependency (which will become the parent chunk) and the
899
        // chunk set the transactions are currently in (which will become the bottom chunk).
900
405k
        auto parent_chunk_idx = parent_data.dep_top_idx[child_idx];
901
405k
        auto child_chunk_idx = parent_data.chunk_idx;
902
405k
        Assume(parent_chunk_idx != child_chunk_idx);
903
405k
        Assume(m_chunk_idxs[child_chunk_idx]);
904
405k
        Assume(!m_chunk_idxs[parent_chunk_idx]); // top set, not a chunk
905
405k
        auto& top_info = m_set_info[parent_chunk_idx];
906
405k
        auto& bottom_info = m_set_info[child_chunk_idx];
907
908
        // Remove the active dependency.
909
405k
        parent_data.active_children.Reset(child_idx);
910
405k
        m_chunk_idxs.Set(parent_chunk_idx);
911
405k
        auto ntx = bottom_info.transactions.Count();
912
        // Subtract the top_info from the bottom_info, as it will become the child chunk.
913
405k
        bottom_info -= top_info;
914
        // See the comment above in Activate(). We perform the opposite operations here, removing
915
        // instead of adding. Simultaneously, aggregate the top/bottom's union of parents/children.
916
405k
        SetType top_parents, top_children;
917
4.95M
        for (auto tx_idx : top_info.transactions) {
918
4.95M
            auto& tx_data = m_tx_data[tx_idx];
919
4.95M
            tx_data.chunk_idx = parent_chunk_idx;
920
4.95M
            top_parents |= tx_data.parents;
921
4.95M
            top_children |= tx_data.children;
922
4.95M
            for (auto dep_child_idx : tx_data.active_children) {
923
4.54M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
924
4.54M
                if (dep_top_info.transactions[parent_idx]) dep_top_info -= bottom_info;
925
4.54M
            }
926
4.95M
        }
927
405k
        SetType bottom_parents, bottom_children;
928
3.88M
        for (auto tx_idx : bottom_info.transactions) {
929
3.88M
            auto& tx_data = m_tx_data[tx_idx];
930
3.88M
            bottom_parents |= tx_data.parents;
931
3.88M
            bottom_children |= tx_data.children;
932
3.88M
            for (auto dep_child_idx : tx_data.active_children) {
933
3.47M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
934
3.47M
                if (dep_top_info.transactions[child_idx]) dep_top_info -= top_info;
935
3.47M
            }
936
3.88M
        }
937
        // Compute the new sets of reachable transactions for each new chunk, based on the
938
        // top/bottom parents and children computed above.
939
405k
        m_reachable[parent_chunk_idx].first = top_parents - top_info.transactions;
940
405k
        m_reachable[parent_chunk_idx].second = top_children - top_info.transactions;
941
405k
        m_reachable[child_chunk_idx].first = bottom_parents - bottom_info.transactions;
942
405k
        m_reachable[child_chunk_idx].second = bottom_children - bottom_info.transactions;
943
        // Return the two new set idxs.
944
405k
        m_cost.DeactivateEnd(/*num_deps=*/ntx - 1);
945
405k
        return {parent_chunk_idx, child_chunk_idx};
946
405k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::Deactivate(unsigned int, unsigned int)
Line
Count
Source
892
356k
    {
893
356k
        m_cost.DeactivateBegin();
894
        // Gather and check information about the parent transactions.
895
356k
        auto& parent_data = m_tx_data[parent_idx];
896
356k
        Assume(parent_data.children[child_idx]);
897
356k
        Assume(parent_data.active_children[child_idx]);
898
        // Get the top set of the active dependency (which will become the parent chunk) and the
899
        // chunk set the transactions are currently in (which will become the bottom chunk).
900
356k
        auto parent_chunk_idx = parent_data.dep_top_idx[child_idx];
901
356k
        auto child_chunk_idx = parent_data.chunk_idx;
902
356k
        Assume(parent_chunk_idx != child_chunk_idx);
903
356k
        Assume(m_chunk_idxs[child_chunk_idx]);
904
356k
        Assume(!m_chunk_idxs[parent_chunk_idx]); // top set, not a chunk
905
356k
        auto& top_info = m_set_info[parent_chunk_idx];
906
356k
        auto& bottom_info = m_set_info[child_chunk_idx];
907
908
        // Remove the active dependency.
909
356k
        parent_data.active_children.Reset(child_idx);
910
356k
        m_chunk_idxs.Set(parent_chunk_idx);
911
356k
        auto ntx = bottom_info.transactions.Count();
912
        // Subtract the top_info from the bottom_info, as it will become the child chunk.
913
356k
        bottom_info -= top_info;
914
        // See the comment above in Activate(). We perform the opposite operations here, removing
915
        // instead of adding. Simultaneously, aggregate the top/bottom's union of parents/children.
916
356k
        SetType top_parents, top_children;
917
4.76M
        for (auto tx_idx : top_info.transactions) {
918
4.76M
            auto& tx_data = m_tx_data[tx_idx];
919
4.76M
            tx_data.chunk_idx = parent_chunk_idx;
920
4.76M
            top_parents |= tx_data.parents;
921
4.76M
            top_children |= tx_data.children;
922
4.76M
            for (auto dep_child_idx : tx_data.active_children) {
923
4.40M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
924
4.40M
                if (dep_top_info.transactions[parent_idx]) dep_top_info -= bottom_info;
925
4.40M
            }
926
4.76M
        }
927
356k
        SetType bottom_parents, bottom_children;
928
3.75M
        for (auto tx_idx : bottom_info.transactions) {
929
3.75M
            auto& tx_data = m_tx_data[tx_idx];
930
3.75M
            bottom_parents |= tx_data.parents;
931
3.75M
            bottom_children |= tx_data.children;
932
3.75M
            for (auto dep_child_idx : tx_data.active_children) {
933
3.39M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
934
3.39M
                if (dep_top_info.transactions[child_idx]) dep_top_info -= top_info;
935
3.39M
            }
936
3.75M
        }
937
        // Compute the new sets of reachable transactions for each new chunk, based on the
938
        // top/bottom parents and children computed above.
939
356k
        m_reachable[parent_chunk_idx].first = top_parents - top_info.transactions;
940
356k
        m_reachable[parent_chunk_idx].second = top_children - top_info.transactions;
941
356k
        m_reachable[child_chunk_idx].first = bottom_parents - bottom_info.transactions;
942
356k
        m_reachable[child_chunk_idx].second = bottom_children - bottom_info.transactions;
943
        // Return the two new set idxs.
944
356k
        m_cost.DeactivateEnd(/*num_deps=*/ntx - 1);
945
356k
        return {parent_chunk_idx, child_chunk_idx};
946
356k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::Deactivate(unsigned int, unsigned int)
Line
Count
Source
892
355k
    {
893
355k
        m_cost.DeactivateBegin();
894
        // Gather and check information about the parent transactions.
895
355k
        auto& parent_data = m_tx_data[parent_idx];
896
355k
        Assume(parent_data.children[child_idx]);
897
355k
        Assume(parent_data.active_children[child_idx]);
898
        // Get the top set of the active dependency (which will become the parent chunk) and the
899
        // chunk set the transactions are currently in (which will become the bottom chunk).
900
355k
        auto parent_chunk_idx = parent_data.dep_top_idx[child_idx];
901
355k
        auto child_chunk_idx = parent_data.chunk_idx;
902
355k
        Assume(parent_chunk_idx != child_chunk_idx);
903
355k
        Assume(m_chunk_idxs[child_chunk_idx]);
904
355k
        Assume(!m_chunk_idxs[parent_chunk_idx]); // top set, not a chunk
905
355k
        auto& top_info = m_set_info[parent_chunk_idx];
906
355k
        auto& bottom_info = m_set_info[child_chunk_idx];
907
908
        // Remove the active dependency.
909
355k
        parent_data.active_children.Reset(child_idx);
910
355k
        m_chunk_idxs.Set(parent_chunk_idx);
911
355k
        auto ntx = bottom_info.transactions.Count();
912
        // Subtract the top_info from the bottom_info, as it will become the child chunk.
913
355k
        bottom_info -= top_info;
914
        // See the comment above in Activate(). We perform the opposite operations here, removing
915
        // instead of adding. Simultaneously, aggregate the top/bottom's union of parents/children.
916
355k
        SetType top_parents, top_children;
917
4.75M
        for (auto tx_idx : top_info.transactions) {
918
4.75M
            auto& tx_data = m_tx_data[tx_idx];
919
4.75M
            tx_data.chunk_idx = parent_chunk_idx;
920
4.75M
            top_parents |= tx_data.parents;
921
4.75M
            top_children |= tx_data.children;
922
4.75M
            for (auto dep_child_idx : tx_data.active_children) {
923
4.39M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
924
4.39M
                if (dep_top_info.transactions[parent_idx]) dep_top_info -= bottom_info;
925
4.39M
            }
926
4.75M
        }
927
355k
        SetType bottom_parents, bottom_children;
928
3.74M
        for (auto tx_idx : bottom_info.transactions) {
929
3.74M
            auto& tx_data = m_tx_data[tx_idx];
930
3.74M
            bottom_parents |= tx_data.parents;
931
3.74M
            bottom_children |= tx_data.children;
932
3.74M
            for (auto dep_child_idx : tx_data.active_children) {
933
3.38M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
934
3.38M
                if (dep_top_info.transactions[child_idx]) dep_top_info -= top_info;
935
3.38M
            }
936
3.74M
        }
937
        // Compute the new sets of reachable transactions for each new chunk, based on the
938
        // top/bottom parents and children computed above.
939
355k
        m_reachable[parent_chunk_idx].first = top_parents - top_info.transactions;
940
355k
        m_reachable[parent_chunk_idx].second = top_children - top_info.transactions;
941
355k
        m_reachable[child_chunk_idx].first = bottom_parents - bottom_info.transactions;
942
355k
        m_reachable[child_chunk_idx].second = bottom_children - bottom_info.transactions;
943
        // Return the two new set idxs.
944
355k
        m_cost.DeactivateEnd(/*num_deps=*/ntx - 1);
945
355k
        return {parent_chunk_idx, child_chunk_idx};
946
355k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::Deactivate(unsigned int, unsigned int)
Line
Count
Source
892
96.1k
    {
893
96.1k
        m_cost.DeactivateBegin();
894
        // Gather and check information about the parent transactions.
895
96.1k
        auto& parent_data = m_tx_data[parent_idx];
896
96.1k
        Assume(parent_data.children[child_idx]);
897
96.1k
        Assume(parent_data.active_children[child_idx]);
898
        // Get the top set of the active dependency (which will become the parent chunk) and the
899
        // chunk set the transactions are currently in (which will become the bottom chunk).
900
96.1k
        auto parent_chunk_idx = parent_data.dep_top_idx[child_idx];
901
96.1k
        auto child_chunk_idx = parent_data.chunk_idx;
902
96.1k
        Assume(parent_chunk_idx != child_chunk_idx);
903
96.1k
        Assume(m_chunk_idxs[child_chunk_idx]);
904
96.1k
        Assume(!m_chunk_idxs[parent_chunk_idx]); // top set, not a chunk
905
96.1k
        auto& top_info = m_set_info[parent_chunk_idx];
906
96.1k
        auto& bottom_info = m_set_info[child_chunk_idx];
907
908
        // Remove the active dependency.
909
96.1k
        parent_data.active_children.Reset(child_idx);
910
96.1k
        m_chunk_idxs.Set(parent_chunk_idx);
911
96.1k
        auto ntx = bottom_info.transactions.Count();
912
        // Subtract the top_info from the bottom_info, as it will become the child chunk.
913
96.1k
        bottom_info -= top_info;
914
        // See the comment above in Activate(). We perform the opposite operations here, removing
915
        // instead of adding. Simultaneously, aggregate the top/bottom's union of parents/children.
916
96.1k
        SetType top_parents, top_children;
917
714k
        for (auto tx_idx : top_info.transactions) {
918
714k
            auto& tx_data = m_tx_data[tx_idx];
919
714k
            tx_data.chunk_idx = parent_chunk_idx;
920
714k
            top_parents |= tx_data.parents;
921
714k
            top_children |= tx_data.children;
922
714k
            for (auto dep_child_idx : tx_data.active_children) {
923
618k
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
924
618k
                if (dep_top_info.transactions[parent_idx]) dep_top_info -= bottom_info;
925
618k
            }
926
714k
        }
927
96.1k
        SetType bottom_parents, bottom_children;
928
474k
        for (auto tx_idx : bottom_info.transactions) {
929
474k
            auto& tx_data = m_tx_data[tx_idx];
930
474k
            bottom_parents |= tx_data.parents;
931
474k
            bottom_children |= tx_data.children;
932
474k
            for (auto dep_child_idx : tx_data.active_children) {
933
378k
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
934
378k
                if (dep_top_info.transactions[child_idx]) dep_top_info -= top_info;
935
378k
            }
936
474k
        }
937
        // Compute the new sets of reachable transactions for each new chunk, based on the
938
        // top/bottom parents and children computed above.
939
96.1k
        m_reachable[parent_chunk_idx].first = top_parents - top_info.transactions;
940
96.1k
        m_reachable[parent_chunk_idx].second = top_children - top_info.transactions;
941
96.1k
        m_reachable[child_chunk_idx].first = bottom_parents - bottom_info.transactions;
942
96.1k
        m_reachable[child_chunk_idx].second = bottom_children - bottom_info.transactions;
943
        // Return the two new set idxs.
944
96.1k
        m_cost.DeactivateEnd(/*num_deps=*/ntx - 1);
945
96.1k
        return {parent_chunk_idx, child_chunk_idx};
946
96.1k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::Deactivate(unsigned int, unsigned int)
Line
Count
Source
892
95.8k
    {
893
95.8k
        m_cost.DeactivateBegin();
894
        // Gather and check information about the parent transactions.
895
95.8k
        auto& parent_data = m_tx_data[parent_idx];
896
95.8k
        Assume(parent_data.children[child_idx]);
897
95.8k
        Assume(parent_data.active_children[child_idx]);
898
        // Get the top set of the active dependency (which will become the parent chunk) and the
899
        // chunk set the transactions are currently in (which will become the bottom chunk).
900
95.8k
        auto parent_chunk_idx = parent_data.dep_top_idx[child_idx];
901
95.8k
        auto child_chunk_idx = parent_data.chunk_idx;
902
95.8k
        Assume(parent_chunk_idx != child_chunk_idx);
903
95.8k
        Assume(m_chunk_idxs[child_chunk_idx]);
904
95.8k
        Assume(!m_chunk_idxs[parent_chunk_idx]); // top set, not a chunk
905
95.8k
        auto& top_info = m_set_info[parent_chunk_idx];
906
95.8k
        auto& bottom_info = m_set_info[child_chunk_idx];
907
908
        // Remove the active dependency.
909
95.8k
        parent_data.active_children.Reset(child_idx);
910
95.8k
        m_chunk_idxs.Set(parent_chunk_idx);
911
95.8k
        auto ntx = bottom_info.transactions.Count();
912
        // Subtract the top_info from the bottom_info, as it will become the child chunk.
913
95.8k
        bottom_info -= top_info;
914
        // See the comment above in Activate(). We perform the opposite operations here, removing
915
        // instead of adding. Simultaneously, aggregate the top/bottom's union of parents/children.
916
95.8k
        SetType top_parents, top_children;
917
713k
        for (auto tx_idx : top_info.transactions) {
918
713k
            auto& tx_data = m_tx_data[tx_idx];
919
713k
            tx_data.chunk_idx = parent_chunk_idx;
920
713k
            top_parents |= tx_data.parents;
921
713k
            top_children |= tx_data.children;
922
713k
            for (auto dep_child_idx : tx_data.active_children) {
923
618k
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
924
618k
                if (dep_top_info.transactions[parent_idx]) dep_top_info -= bottom_info;
925
618k
            }
926
713k
        }
927
95.8k
        SetType bottom_parents, bottom_children;
928
474k
        for (auto tx_idx : bottom_info.transactions) {
929
474k
            auto& tx_data = m_tx_data[tx_idx];
930
474k
            bottom_parents |= tx_data.parents;
931
474k
            bottom_children |= tx_data.children;
932
474k
            for (auto dep_child_idx : tx_data.active_children) {
933
378k
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[dep_child_idx]];
934
378k
                if (dep_top_info.transactions[child_idx]) dep_top_info -= top_info;
935
378k
            }
936
474k
        }
937
        // Compute the new sets of reachable transactions for each new chunk, based on the
938
        // top/bottom parents and children computed above.
939
95.8k
        m_reachable[parent_chunk_idx].first = top_parents - top_info.transactions;
940
95.8k
        m_reachable[parent_chunk_idx].second = top_children - top_info.transactions;
941
95.8k
        m_reachable[child_chunk_idx].first = bottom_parents - bottom_info.transactions;
942
95.8k
        m_reachable[child_chunk_idx].second = bottom_children - bottom_info.transactions;
943
        // Return the two new set idxs.
944
95.8k
        m_cost.DeactivateEnd(/*num_deps=*/ntx - 1);
945
95.8k
        return {parent_chunk_idx, child_chunk_idx};
946
95.8k
    }
947
948
    /** Activate a dependency from the bottom set to the top set, which must exist. Return the
949
     *  index of the merged chunk. */
950
    SetIdx MergeChunks(SetIdx top_idx, SetIdx bottom_idx) noexcept
951
4.58M
    {
952
4.58M
        m_cost.MergeChunksBegin();
953
4.58M
        Assume(m_chunk_idxs[top_idx]);
954
4.58M
        Assume(m_chunk_idxs[bottom_idx]);
955
4.58M
        auto& top_chunk_info = m_set_info[top_idx];
956
4.58M
        auto& bottom_chunk_info = m_set_info[bottom_idx];
957
        // Count the number of dependencies between bottom_chunk and top_chunk.
958
4.58M
        unsigned num_deps{0};
959
39.0M
        for (auto tx_idx : top_chunk_info.transactions) {
960
39.0M
            auto& tx_data = m_tx_data[tx_idx];
961
39.0M
            num_deps += (tx_data.children & bottom_chunk_info.transactions).Count();
962
39.0M
        }
963
4.58M
        m_cost.MergeChunksMid(/*num_txns=*/top_chunk_info.transactions.Count());
964
4.58M
        Assume(num_deps > 0);
965
        // Uniformly randomly pick one of them and activate it.
966
4.58M
        unsigned pick = m_rng.randrange(num_deps);
967
4.58M
        unsigned num_steps = 0;
968
16.3M
        for (auto tx_idx : top_chunk_info.transactions) {
969
16.3M
            ++num_steps;
970
16.3M
            auto& tx_data = m_tx_data[tx_idx];
971
16.3M
            auto intersect = tx_data.children & bottom_chunk_info.transactions;
972
16.3M
            auto count = intersect.Count();
973
16.3M
            if (pick < count) {
974
6.00M
                for (auto child_idx : intersect) {
975
6.00M
                    if (pick == 0) {
976
4.58M
                        m_cost.MergeChunksEnd(/*num_steps=*/num_steps);
977
4.58M
                        return Activate(tx_idx, child_idx);
978
4.58M
                    }
979
1.41M
                    --pick;
980
1.41M
                }
981
0
                Assume(false);
982
0
                break;
983
4.58M
            }
984
11.7M
            pick -= count;
985
11.7M
        }
986
0
        Assume(false);
987
0
        return INVALID_SET_IDX;
988
4.58M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MergeChunks(unsigned char, unsigned char)
Line
Count
Source
951
1.31M
    {
952
1.31M
        m_cost.MergeChunksBegin();
953
1.31M
        Assume(m_chunk_idxs[top_idx]);
954
1.31M
        Assume(m_chunk_idxs[bottom_idx]);
955
1.31M
        auto& top_chunk_info = m_set_info[top_idx];
956
1.31M
        auto& bottom_chunk_info = m_set_info[bottom_idx];
957
        // Count the number of dependencies between bottom_chunk and top_chunk.
958
1.31M
        unsigned num_deps{0};
959
12.1M
        for (auto tx_idx : top_chunk_info.transactions) {
960
12.1M
            auto& tx_data = m_tx_data[tx_idx];
961
12.1M
            num_deps += (tx_data.children & bottom_chunk_info.transactions).Count();
962
12.1M
        }
963
1.31M
        m_cost.MergeChunksMid(/*num_txns=*/top_chunk_info.transactions.Count());
964
1.31M
        Assume(num_deps > 0);
965
        // Uniformly randomly pick one of them and activate it.
966
1.31M
        unsigned pick = m_rng.randrange(num_deps);
967
1.31M
        unsigned num_steps = 0;
968
5.31M
        for (auto tx_idx : top_chunk_info.transactions) {
969
5.31M
            ++num_steps;
970
5.31M
            auto& tx_data = m_tx_data[tx_idx];
971
5.31M
            auto intersect = tx_data.children & bottom_chunk_info.transactions;
972
5.31M
            auto count = intersect.Count();
973
5.31M
            if (pick < count) {
974
1.74M
                for (auto child_idx : intersect) {
975
1.74M
                    if (pick == 0) {
976
1.31M
                        m_cost.MergeChunksEnd(/*num_steps=*/num_steps);
977
1.31M
                        return Activate(tx_idx, child_idx);
978
1.31M
                    }
979
434k
                    --pick;
980
434k
                }
981
0
                Assume(false);
982
0
                break;
983
1.31M
            }
984
3.99M
            pick -= count;
985
3.99M
        }
986
0
        Assume(false);
987
0
        return INVALID_SET_IDX;
988
1.31M
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MergeChunks(unsigned char, unsigned char)
Line
Count
Source
951
1.25M
    {
952
1.25M
        m_cost.MergeChunksBegin();
953
1.25M
        Assume(m_chunk_idxs[top_idx]);
954
1.25M
        Assume(m_chunk_idxs[bottom_idx]);
955
1.25M
        auto& top_chunk_info = m_set_info[top_idx];
956
1.25M
        auto& bottom_chunk_info = m_set_info[bottom_idx];
957
        // Count the number of dependencies between bottom_chunk and top_chunk.
958
1.25M
        unsigned num_deps{0};
959
11.2M
        for (auto tx_idx : top_chunk_info.transactions) {
960
11.2M
            auto& tx_data = m_tx_data[tx_idx];
961
11.2M
            num_deps += (tx_data.children & bottom_chunk_info.transactions).Count();
962
11.2M
        }
963
1.25M
        m_cost.MergeChunksMid(/*num_txns=*/top_chunk_info.transactions.Count());
964
1.25M
        Assume(num_deps > 0);
965
        // Uniformly randomly pick one of them and activate it.
966
1.25M
        unsigned pick = m_rng.randrange(num_deps);
967
1.25M
        unsigned num_steps = 0;
968
4.53M
        for (auto tx_idx : top_chunk_info.transactions) {
969
4.53M
            ++num_steps;
970
4.53M
            auto& tx_data = m_tx_data[tx_idx];
971
4.53M
            auto intersect = tx_data.children & bottom_chunk_info.transactions;
972
4.53M
            auto count = intersect.Count();
973
4.53M
            if (pick < count) {
974
1.69M
                for (auto child_idx : intersect) {
975
1.69M
                    if (pick == 0) {
976
1.25M
                        m_cost.MergeChunksEnd(/*num_steps=*/num_steps);
977
1.25M
                        return Activate(tx_idx, child_idx);
978
1.25M
                    }
979
440k
                    --pick;
980
440k
                }
981
0
                Assume(false);
982
0
                break;
983
1.25M
            }
984
3.27M
            pick -= count;
985
3.27M
        }
986
0
        Assume(false);
987
0
        return INVALID_SET_IDX;
988
1.25M
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MergeChunks(unsigned char, unsigned char)
Line
Count
Source
951
1.25M
    {
952
1.25M
        m_cost.MergeChunksBegin();
953
1.25M
        Assume(m_chunk_idxs[top_idx]);
954
1.25M
        Assume(m_chunk_idxs[bottom_idx]);
955
1.25M
        auto& top_chunk_info = m_set_info[top_idx];
956
1.25M
        auto& bottom_chunk_info = m_set_info[bottom_idx];
957
        // Count the number of dependencies between bottom_chunk and top_chunk.
958
1.25M
        unsigned num_deps{0};
959
11.2M
        for (auto tx_idx : top_chunk_info.transactions) {
960
11.2M
            auto& tx_data = m_tx_data[tx_idx];
961
11.2M
            num_deps += (tx_data.children & bottom_chunk_info.transactions).Count();
962
11.2M
        }
963
1.25M
        m_cost.MergeChunksMid(/*num_txns=*/top_chunk_info.transactions.Count());
964
1.25M
        Assume(num_deps > 0);
965
        // Uniformly randomly pick one of them and activate it.
966
1.25M
        unsigned pick = m_rng.randrange(num_deps);
967
1.25M
        unsigned num_steps = 0;
968
4.54M
        for (auto tx_idx : top_chunk_info.transactions) {
969
4.54M
            ++num_steps;
970
4.54M
            auto& tx_data = m_tx_data[tx_idx];
971
4.54M
            auto intersect = tx_data.children & bottom_chunk_info.transactions;
972
4.54M
            auto count = intersect.Count();
973
4.54M
            if (pick < count) {
974
1.68M
                for (auto child_idx : intersect) {
975
1.68M
                    if (pick == 0) {
976
1.25M
                        m_cost.MergeChunksEnd(/*num_steps=*/num_steps);
977
1.25M
                        return Activate(tx_idx, child_idx);
978
1.25M
                    }
979
430k
                    --pick;
980
430k
                }
981
0
                Assume(false);
982
0
                break;
983
1.25M
            }
984
3.28M
            pick -= count;
985
3.28M
        }
986
0
        Assume(false);
987
0
        return INVALID_SET_IDX;
988
1.25M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MergeChunks(unsigned char, unsigned char)
Line
Count
Source
951
378k
    {
952
378k
        m_cost.MergeChunksBegin();
953
378k
        Assume(m_chunk_idxs[top_idx]);
954
378k
        Assume(m_chunk_idxs[bottom_idx]);
955
378k
        auto& top_chunk_info = m_set_info[top_idx];
956
378k
        auto& bottom_chunk_info = m_set_info[bottom_idx];
957
        // Count the number of dependencies between bottom_chunk and top_chunk.
958
378k
        unsigned num_deps{0};
959
2.25M
        for (auto tx_idx : top_chunk_info.transactions) {
960
2.25M
            auto& tx_data = m_tx_data[tx_idx];
961
2.25M
            num_deps += (tx_data.children & bottom_chunk_info.transactions).Count();
962
2.25M
        }
963
378k
        m_cost.MergeChunksMid(/*num_txns=*/top_chunk_info.transactions.Count());
964
378k
        Assume(num_deps > 0);
965
        // Uniformly randomly pick one of them and activate it.
966
378k
        unsigned pick = m_rng.randrange(num_deps);
967
378k
        unsigned num_steps = 0;
968
998k
        for (auto tx_idx : top_chunk_info.transactions) {
969
998k
            ++num_steps;
970
998k
            auto& tx_data = m_tx_data[tx_idx];
971
998k
            auto intersect = tx_data.children & bottom_chunk_info.transactions;
972
998k
            auto count = intersect.Count();
973
998k
            if (pick < count) {
974
434k
                for (auto child_idx : intersect) {
975
434k
                    if (pick == 0) {
976
378k
                        m_cost.MergeChunksEnd(/*num_steps=*/num_steps);
977
378k
                        return Activate(tx_idx, child_idx);
978
378k
                    }
979
55.9k
                    --pick;
980
55.9k
                }
981
0
                Assume(false);
982
0
                break;
983
378k
            }
984
619k
            pick -= count;
985
619k
        }
986
0
        Assume(false);
987
0
        return INVALID_SET_IDX;
988
378k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MergeChunks(unsigned char, unsigned char)
Line
Count
Source
951
378k
    {
952
378k
        m_cost.MergeChunksBegin();
953
378k
        Assume(m_chunk_idxs[top_idx]);
954
378k
        Assume(m_chunk_idxs[bottom_idx]);
955
378k
        auto& top_chunk_info = m_set_info[top_idx];
956
378k
        auto& bottom_chunk_info = m_set_info[bottom_idx];
957
        // Count the number of dependencies between bottom_chunk and top_chunk.
958
378k
        unsigned num_deps{0};
959
2.25M
        for (auto tx_idx : top_chunk_info.transactions) {
960
2.25M
            auto& tx_data = m_tx_data[tx_idx];
961
2.25M
            num_deps += (tx_data.children & bottom_chunk_info.transactions).Count();
962
2.25M
        }
963
378k
        m_cost.MergeChunksMid(/*num_txns=*/top_chunk_info.transactions.Count());
964
378k
        Assume(num_deps > 0);
965
        // Uniformly randomly pick one of them and activate it.
966
378k
        unsigned pick = m_rng.randrange(num_deps);
967
378k
        unsigned num_steps = 0;
968
997k
        for (auto tx_idx : top_chunk_info.transactions) {
969
997k
            ++num_steps;
970
997k
            auto& tx_data = m_tx_data[tx_idx];
971
997k
            auto intersect = tx_data.children & bottom_chunk_info.transactions;
972
997k
            auto count = intersect.Count();
973
997k
            if (pick < count) {
974
434k
                for (auto child_idx : intersect) {
975
434k
                    if (pick == 0) {
976
378k
                        m_cost.MergeChunksEnd(/*num_steps=*/num_steps);
977
378k
                        return Activate(tx_idx, child_idx);
978
378k
                    }
979
55.9k
                    --pick;
980
55.9k
                }
981
0
                Assume(false);
982
0
                break;
983
378k
            }
984
619k
            pick -= count;
985
619k
        }
986
0
        Assume(false);
987
0
        return INVALID_SET_IDX;
988
378k
    }
989
990
    /** Activate a dependency from chunk_idx to merge_chunk_idx (if !DownWard), or a dependency
991
     *  from merge_chunk_idx to chunk_idx (if DownWard). Return the index of the merged chunk. */
992
    template<bool DownWard>
993
    SetIdx MergeChunksDirected(SetIdx chunk_idx, SetIdx merge_chunk_idx) noexcept
994
3.78M
    {
995
3.78M
        if constexpr (DownWard) {
996
489k
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
3.29M
        } else {
998
3.29M
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
3.29M
        }
1000
3.78M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<false>(unsigned char, unsigned char)
Line
Count
Source
994
947k
    {
995
        if constexpr (DownWard) {
996
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
947k
        } else {
998
947k
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
947k
        }
1000
947k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<true>(unsigned char, unsigned char)
Line
Count
Source
994
137k
    {
995
137k
        if constexpr (DownWard) {
996
137k
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
        } else {
998
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
        }
1000
137k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<false>(unsigned char, unsigned char)
Line
Count
Source
994
892k
    {
995
        if constexpr (DownWard) {
996
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
892k
        } else {
998
892k
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
892k
        }
1000
892k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<true>(unsigned char, unsigned char)
Line
Count
Source
994
135k
    {
995
135k
        if constexpr (DownWard) {
996
135k
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
        } else {
998
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
        }
1000
135k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<false>(unsigned char, unsigned char)
Line
Count
Source
994
892k
    {
995
        if constexpr (DownWard) {
996
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
892k
        } else {
998
892k
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
892k
        }
1000
892k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<true>(unsigned char, unsigned char)
Line
Count
Source
994
135k
    {
995
135k
        if constexpr (DownWard) {
996
135k
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
        } else {
998
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
        }
1000
135k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<false>(unsigned char, unsigned char)
Line
Count
Source
994
281k
    {
995
        if constexpr (DownWard) {
996
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
281k
        } else {
998
281k
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
281k
        }
1000
281k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<true>(unsigned char, unsigned char)
Line
Count
Source
994
40.7k
    {
995
40.7k
        if constexpr (DownWard) {
996
40.7k
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
        } else {
998
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
        }
1000
40.7k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<false>(unsigned char, unsigned char)
Line
Count
Source
994
282k
    {
995
        if constexpr (DownWard) {
996
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
282k
        } else {
998
282k
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
282k
        }
1000
282k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MergeChunksDirected<true>(unsigned char, unsigned char)
Line
Count
Source
994
39.7k
    {
995
39.7k
        if constexpr (DownWard) {
996
39.7k
            return MergeChunks(chunk_idx, merge_chunk_idx);
997
        } else {
998
            return MergeChunks(merge_chunk_idx, chunk_idx);
999
        }
1000
39.7k
    }
1001
1002
    /** Determine which chunk to merge chunk_idx with, or INVALID_SET_IDX if none. */
1003
    template<bool DownWard>
1004
    SetIdx PickMergeCandidate(SetIdx chunk_idx) noexcept
1005
9.22M
    {
1006
9.22M
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
9.22M
        Assume(m_chunk_idxs[chunk_idx]);
1009
9.22M
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
9.22M
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
9.22M
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
9.22M
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
9.22M
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
9.22M
        unsigned steps = 0;
1028
35.3M
        while (todo.Any()) {
1029
26.1M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
26.1M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
26.1M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
26.1M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
26.1M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
26.1M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
26.1M
            if (cmp > 0) continue;
1038
6.97M
            uint64_t tiebreak = m_rng.rand64();
1039
6.97M
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
5.27M
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
5.27M
                best_other_chunk_idx = reached_chunk_idx;
1042
5.27M
                best_other_chunk_tiebreak = tiebreak;
1043
5.27M
            }
1044
6.97M
        }
1045
9.22M
        Assume(steps <= m_set_info.size());
1046
1047
9.22M
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
9.22M
        return best_other_chunk_idx;
1049
9.22M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<false>(unsigned char)
Line
Count
Source
1005
2.27M
    {
1006
2.27M
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
2.27M
        Assume(m_chunk_idxs[chunk_idx]);
1009
2.27M
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
2.27M
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
2.27M
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
2.27M
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
2.27M
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
2.27M
        unsigned steps = 0;
1028
8.60M
        while (todo.Any()) {
1029
6.32M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
6.32M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
6.32M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
6.32M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
6.32M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
6.32M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
6.32M
            if (cmp > 0) continue;
1038
1.29M
            uint64_t tiebreak = m_rng.rand64();
1039
1.29M
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
1.23M
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
1.23M
                best_other_chunk_idx = reached_chunk_idx;
1042
1.23M
                best_other_chunk_tiebreak = tiebreak;
1043
1.23M
            }
1044
1.29M
        }
1045
2.27M
        Assume(steps <= m_set_info.size());
1046
1047
2.27M
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
2.27M
        return best_other_chunk_idx;
1049
2.27M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<true>(unsigned char)
Line
Count
Source
1005
385k
    {
1006
385k
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
385k
        Assume(m_chunk_idxs[chunk_idx]);
1009
385k
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
385k
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
385k
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
385k
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
385k
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
385k
        unsigned steps = 0;
1028
1.94M
        while (todo.Any()) {
1029
1.55M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
1.55M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
1.55M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
1.55M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
1.55M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
1.55M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
1.55M
            if (cmp > 0) continue;
1038
712k
            uint64_t tiebreak = m_rng.rand64();
1039
712k
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
263k
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
263k
                best_other_chunk_idx = reached_chunk_idx;
1042
263k
                best_other_chunk_tiebreak = tiebreak;
1043
263k
            }
1044
712k
        }
1045
385k
        Assume(steps <= m_set_info.size());
1046
1047
385k
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
385k
        return best_other_chunk_idx;
1049
385k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<false>(unsigned char)
Line
Count
Source
1005
2.15M
    {
1006
2.15M
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
2.15M
        Assume(m_chunk_idxs[chunk_idx]);
1009
2.15M
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
2.15M
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
2.15M
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
2.15M
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
2.15M
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
2.15M
        unsigned steps = 0;
1028
8.42M
        while (todo.Any()) {
1029
6.27M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
6.27M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
6.27M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
6.27M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
6.27M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
6.27M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
6.27M
            if (cmp > 0) continue;
1038
1.23M
            uint64_t tiebreak = m_rng.rand64();
1039
1.23M
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
1.18M
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
1.18M
                best_other_chunk_idx = reached_chunk_idx;
1042
1.18M
                best_other_chunk_tiebreak = tiebreak;
1043
1.18M
            }
1044
1.23M
        }
1045
2.15M
        Assume(steps <= m_set_info.size());
1046
1047
2.15M
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
2.15M
        return best_other_chunk_idx;
1049
2.15M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<true>(unsigned char)
Line
Count
Source
1005
378k
    {
1006
378k
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
378k
        Assume(m_chunk_idxs[chunk_idx]);
1009
378k
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
378k
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
378k
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
378k
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
378k
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
378k
        unsigned steps = 0;
1028
1.92M
        while (todo.Any()) {
1029
1.54M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
1.54M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
1.54M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
1.54M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
1.54M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
1.54M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
1.54M
            if (cmp > 0) continue;
1038
730k
            uint64_t tiebreak = m_rng.rand64();
1039
730k
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
261k
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
261k
                best_other_chunk_idx = reached_chunk_idx;
1042
261k
                best_other_chunk_tiebreak = tiebreak;
1043
261k
            }
1044
730k
        }
1045
378k
        Assume(steps <= m_set_info.size());
1046
1047
378k
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
378k
        return best_other_chunk_idx;
1049
378k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<false>(unsigned char)
Line
Count
Source
1005
2.15M
    {
1006
2.15M
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
2.15M
        Assume(m_chunk_idxs[chunk_idx]);
1009
2.15M
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
2.15M
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
2.15M
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
2.15M
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
2.15M
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
2.15M
        unsigned steps = 0;
1028
8.42M
        while (todo.Any()) {
1029
6.27M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
6.27M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
6.27M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
6.27M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
6.27M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
6.27M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
6.27M
            if (cmp > 0) continue;
1038
1.23M
            uint64_t tiebreak = m_rng.rand64();
1039
1.23M
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
1.18M
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
1.18M
                best_other_chunk_idx = reached_chunk_idx;
1042
1.18M
                best_other_chunk_tiebreak = tiebreak;
1043
1.18M
            }
1044
1.23M
        }
1045
2.15M
        Assume(steps <= m_set_info.size());
1046
1047
2.15M
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
2.15M
        return best_other_chunk_idx;
1049
2.15M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<true>(unsigned char)
Line
Count
Source
1005
378k
    {
1006
378k
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
378k
        Assume(m_chunk_idxs[chunk_idx]);
1009
378k
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
378k
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
378k
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
378k
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
378k
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
378k
        unsigned steps = 0;
1028
1.93M
        while (todo.Any()) {
1029
1.55M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
1.55M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
1.55M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
1.55M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
1.55M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
1.55M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
1.55M
            if (cmp > 0) continue;
1038
735k
            uint64_t tiebreak = m_rng.rand64();
1039
735k
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
262k
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
262k
                best_other_chunk_idx = reached_chunk_idx;
1042
262k
                best_other_chunk_tiebreak = tiebreak;
1043
262k
            }
1044
735k
        }
1045
378k
        Assume(steps <= m_set_info.size());
1046
1047
378k
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
378k
        return best_other_chunk_idx;
1049
378k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<false>(unsigned char)
Line
Count
Source
1005
647k
    {
1006
647k
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
647k
        Assume(m_chunk_idxs[chunk_idx]);
1009
647k
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
647k
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
647k
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
647k
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
647k
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
647k
        unsigned steps = 0;
1028
1.68M
        while (todo.Any()) {
1029
1.03M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
1.03M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
1.03M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
1.03M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
1.03M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
1.03M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
1.03M
            if (cmp > 0) continue;
1038
376k
            uint64_t tiebreak = m_rng.rand64();
1039
376k
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
366k
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
366k
                best_other_chunk_idx = reached_chunk_idx;
1042
366k
                best_other_chunk_tiebreak = tiebreak;
1043
366k
            }
1044
376k
        }
1045
647k
        Assume(steps <= m_set_info.size());
1046
1047
647k
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
647k
        return best_other_chunk_idx;
1049
647k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<true>(unsigned char)
Line
Count
Source
1005
98.9k
    {
1006
98.9k
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
98.9k
        Assume(m_chunk_idxs[chunk_idx]);
1009
98.9k
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
98.9k
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
98.9k
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
98.9k
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
98.9k
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
98.9k
        unsigned steps = 0;
1028
363k
        while (todo.Any()) {
1029
264k
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
264k
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
264k
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
264k
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
264k
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
264k
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
264k
            if (cmp > 0) continue;
1038
142k
            uint64_t tiebreak = m_rng.rand64();
1039
142k
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
72.5k
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
72.5k
                best_other_chunk_idx = reached_chunk_idx;
1042
72.5k
                best_other_chunk_tiebreak = tiebreak;
1043
72.5k
            }
1044
142k
        }
1045
98.9k
        Assume(steps <= m_set_info.size());
1046
1047
98.9k
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
98.9k
        return best_other_chunk_idx;
1049
98.9k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<false>(unsigned char)
Line
Count
Source
1005
648k
    {
1006
648k
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
648k
        Assume(m_chunk_idxs[chunk_idx]);
1009
648k
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
648k
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
648k
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
648k
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
648k
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
648k
        unsigned steps = 0;
1028
1.68M
        while (todo.Any()) {
1029
1.03M
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
1.03M
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
1.03M
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
1.03M
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
1.03M
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
1.03M
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
1.03M
            if (cmp > 0) continue;
1038
377k
            uint64_t tiebreak = m_rng.rand64();
1039
377k
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
367k
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
367k
                best_other_chunk_idx = reached_chunk_idx;
1042
367k
                best_other_chunk_tiebreak = tiebreak;
1043
367k
            }
1044
377k
        }
1045
648k
        Assume(steps <= m_set_info.size());
1046
1047
648k
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
648k
        return best_other_chunk_idx;
1049
648k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::PickMergeCandidate<true>(unsigned char)
Line
Count
Source
1005
98.2k
    {
1006
98.2k
        m_cost.PickMergeCandidateBegin();
1007
        /** Information about the chunk. */
1008
98.2k
        Assume(m_chunk_idxs[chunk_idx]);
1009
98.2k
        auto& chunk_info = m_set_info[chunk_idx];
1010
        // Iterate over all chunks reachable from this one. For those depended-on chunks,
1011
        // remember the highest-feerate (if DownWard) or lowest-feerate (if !DownWard) one.
1012
        // If multiple equal-feerate candidate chunks to merge with exist, pick a random one
1013
        // among them.
1014
1015
        /** The minimum feerate (if downward) or maximum feerate (if upward) to consider when
1016
         *  looking for candidate chunks to merge with. Initially, this is the original chunk's
1017
         *  feerate, but is updated to be the current best candidate whenever one is found. */
1018
98.2k
        FeeFrac best_other_chunk_feerate = chunk_info.feerate;
1019
        /** The chunk index for the best candidate chunk to merge with. INVALID_SET_IDX if none. */
1020
98.2k
        SetIdx best_other_chunk_idx = INVALID_SET_IDX;
1021
        /** We generate random tiebreak values to pick between equal-feerate candidate chunks.
1022
         *  This variable stores the tiebreak of the current best candidate. */
1023
98.2k
        uint64_t best_other_chunk_tiebreak{0};
1024
1025
        /** Which parent/child transactions we still need to process the chunks for. */
1026
98.2k
        auto todo = DownWard ? m_reachable[chunk_idx].second : m_reachable[chunk_idx].first;
1027
98.2k
        unsigned steps = 0;
1028
361k
        while (todo.Any()) {
1029
263k
            ++steps;
1030
            // Find a chunk for a transaction in todo, and remove all its transactions from todo.
1031
263k
            auto reached_chunk_idx = m_tx_data[todo.First()].chunk_idx;
1032
263k
            auto& reached_chunk_info = m_set_info[reached_chunk_idx];
1033
263k
            todo -= reached_chunk_info.transactions;
1034
            // See if it has an acceptable feerate.
1035
263k
            auto cmp = DownWard ? ByRatio{best_other_chunk_feerate} <=> ByRatio{reached_chunk_info.feerate}
1036
263k
                                : ByRatio{reached_chunk_info.feerate} <=> ByRatio{best_other_chunk_feerate};
1037
263k
            if (cmp > 0) continue;
1038
139k
            uint64_t tiebreak = m_rng.rand64();
1039
139k
            if (cmp < 0 || tiebreak >= best_other_chunk_tiebreak) {
1040
71.5k
                best_other_chunk_feerate = reached_chunk_info.feerate;
1041
71.5k
                best_other_chunk_idx = reached_chunk_idx;
1042
71.5k
                best_other_chunk_tiebreak = tiebreak;
1043
71.5k
            }
1044
139k
        }
1045
98.2k
        Assume(steps <= m_set_info.size());
1046
1047
98.2k
        m_cost.PickMergeCandidateEnd(/*num_steps=*/steps);
1048
98.2k
        return best_other_chunk_idx;
1049
98.2k
    }
1050
1051
    /** Perform an upward or downward merge step, on the specified chunk. Returns the merged chunk,
1052
     *  or INVALID_SET_IDX if no merge took place. */
1053
    template<bool DownWard>
1054
    SetIdx MergeStep(SetIdx chunk_idx) noexcept
1055
9.22M
    {
1056
9.22M
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
9.22M
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
3.78M
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
3.78M
        Assume(chunk_idx != INVALID_SET_IDX);
1060
3.78M
        return chunk_idx;
1061
9.22M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MergeStep<false>(unsigned char)
Line
Count
Source
1055
2.27M
    {
1056
2.27M
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
2.27M
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
947k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
947k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
947k
        return chunk_idx;
1061
2.27M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MergeStep<true>(unsigned char)
Line
Count
Source
1055
385k
    {
1056
385k
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
385k
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
137k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
137k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
137k
        return chunk_idx;
1061
385k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MergeStep<false>(unsigned char)
Line
Count
Source
1055
2.15M
    {
1056
2.15M
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
2.15M
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
892k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
892k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
892k
        return chunk_idx;
1061
2.15M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MergeStep<true>(unsigned char)
Line
Count
Source
1055
378k
    {
1056
378k
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
378k
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
135k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
135k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
135k
        return chunk_idx;
1061
378k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MergeStep<false>(unsigned char)
Line
Count
Source
1055
2.15M
    {
1056
2.15M
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
2.15M
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
892k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
892k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
892k
        return chunk_idx;
1061
2.15M
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MergeStep<true>(unsigned char)
Line
Count
Source
1055
378k
    {
1056
378k
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
378k
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
135k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
135k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
135k
        return chunk_idx;
1061
378k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MergeStep<false>(unsigned char)
Line
Count
Source
1055
647k
    {
1056
647k
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
647k
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
281k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
281k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
281k
        return chunk_idx;
1061
647k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MergeStep<true>(unsigned char)
Line
Count
Source
1055
98.9k
    {
1056
98.9k
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
98.9k
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
40.7k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
40.7k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
40.7k
        return chunk_idx;
1061
98.9k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MergeStep<false>(unsigned char)
Line
Count
Source
1055
648k
    {
1056
648k
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
648k
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
282k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
282k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
282k
        return chunk_idx;
1061
648k
    }
unsigned char cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MergeStep<true>(unsigned char)
Line
Count
Source
1055
98.2k
    {
1056
98.2k
        auto merge_chunk_idx = PickMergeCandidate<DownWard>(chunk_idx);
1057
98.2k
        if (merge_chunk_idx == INVALID_SET_IDX) return INVALID_SET_IDX;
1058
39.7k
        chunk_idx = MergeChunksDirected<DownWard>(chunk_idx, merge_chunk_idx);
1059
39.7k
        Assume(chunk_idx != INVALID_SET_IDX);
1060
39.7k
        return chunk_idx;
1061
98.2k
    }
1062
1063
    /** Perform an upward or downward merge sequence on the specified chunk. */
1064
    template<bool DownWard>
1065
    void MergeSequence(SetIdx chunk_idx) noexcept
1066
438k
    {
1067
438k
        Assume(m_chunk_idxs[chunk_idx]);
1068
487k
        while (true) {
1069
487k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
487k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
49.5k
            chunk_idx = merged_chunk_idx;
1072
49.5k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
438k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
426k
            m_suboptimal_idxs.Set(chunk_idx);
1076
426k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
426k
        }
1078
438k
    }
void cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<false>(unsigned char)
Line
Count
Source
1066
67.2k
    {
1067
67.2k
        Assume(m_chunk_idxs[chunk_idx]);
1068
72.7k
        while (true) {
1069
72.7k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
72.7k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
5.45k
            chunk_idx = merged_chunk_idx;
1072
5.45k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
67.2k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
67.2k
            m_suboptimal_idxs.Set(chunk_idx);
1076
67.2k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
67.2k
        }
1078
67.2k
    }
void cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<true>(unsigned char)
Line
Count
Source
1066
67.2k
    {
1067
67.2k
        Assume(m_chunk_idxs[chunk_idx]);
1068
77.0k
        while (true) {
1069
77.0k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
77.0k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
9.77k
            chunk_idx = merged_chunk_idx;
1072
9.77k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
67.2k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
63.5k
            m_suboptimal_idxs.Set(chunk_idx);
1076
63.5k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
63.5k
        }
1078
67.2k
    }
void cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<false>(unsigned char)
Line
Count
Source
1066
68.5k
    {
1067
68.5k
        Assume(m_chunk_idxs[chunk_idx]);
1068
73.8k
        while (true) {
1069
73.8k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
73.8k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
5.34k
            chunk_idx = merged_chunk_idx;
1072
5.34k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
68.5k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
68.5k
            m_suboptimal_idxs.Set(chunk_idx);
1076
68.5k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
68.5k
        }
1078
68.5k
    }
void cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<true>(unsigned char)
Line
Count
Source
1066
68.5k
    {
1067
68.5k
        Assume(m_chunk_idxs[chunk_idx]);
1068
78.3k
        while (true) {
1069
78.3k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
78.3k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
9.85k
            chunk_idx = merged_chunk_idx;
1072
9.85k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
68.5k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
64.8k
            m_suboptimal_idxs.Set(chunk_idx);
1076
64.8k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
64.8k
        }
1078
68.5k
    }
void cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<false>(unsigned char)
Line
Count
Source
1066
67.6k
    {
1067
67.6k
        Assume(m_chunk_idxs[chunk_idx]);
1068
73.1k
        while (true) {
1069
73.1k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
73.1k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
5.47k
            chunk_idx = merged_chunk_idx;
1072
5.47k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
67.6k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
67.6k
            m_suboptimal_idxs.Set(chunk_idx);
1076
67.6k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
67.6k
        }
1078
67.6k
    }
void cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<true>(unsigned char)
Line
Count
Source
1066
67.6k
    {
1067
67.6k
        Assume(m_chunk_idxs[chunk_idx]);
1068
77.4k
        while (true) {
1069
77.4k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
77.4k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
9.78k
            chunk_idx = merged_chunk_idx;
1072
9.78k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
67.6k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
63.9k
            m_suboptimal_idxs.Set(chunk_idx);
1076
63.9k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
63.9k
        }
1078
67.6k
    }
void cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<false>(unsigned char)
Line
Count
Source
1066
7.78k
    {
1067
7.78k
        Assume(m_chunk_idxs[chunk_idx]);
1068
8.33k
        while (true) {
1069
8.33k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
8.33k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
551
            chunk_idx = merged_chunk_idx;
1072
551
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
7.78k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
7.78k
            m_suboptimal_idxs.Set(chunk_idx);
1076
7.78k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
7.78k
        }
1078
7.78k
    }
void cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<true>(unsigned char)
Line
Count
Source
1066
7.78k
    {
1067
7.78k
        Assume(m_chunk_idxs[chunk_idx]);
1068
9.11k
        while (true) {
1069
9.11k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
9.11k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
1.32k
            chunk_idx = merged_chunk_idx;
1072
1.32k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
7.78k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
7.35k
            m_suboptimal_idxs.Set(chunk_idx);
1076
7.35k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
7.35k
        }
1078
7.78k
    }
void cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<false>(unsigned char)
Line
Count
Source
1066
7.91k
    {
1067
7.91k
        Assume(m_chunk_idxs[chunk_idx]);
1068
8.49k
        while (true) {
1069
8.49k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
8.49k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
577
            chunk_idx = merged_chunk_idx;
1072
577
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
7.91k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
7.91k
            m_suboptimal_idxs.Set(chunk_idx);
1076
7.91k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
7.91k
        }
1078
7.91k
    }
void cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MergeSequence<true>(unsigned char)
Line
Count
Source
1066
7.91k
    {
1067
7.91k
        Assume(m_chunk_idxs[chunk_idx]);
1068
9.27k
        while (true) {
1069
9.27k
            auto merged_chunk_idx = MergeStep<DownWard>(chunk_idx);
1070
9.27k
            if (merged_chunk_idx == INVALID_SET_IDX) break;
1071
1.36k
            chunk_idx = merged_chunk_idx;
1072
1.36k
        }
1073
        // Add the chunk to the queue of improvable chunks, if it wasn't already there.
1074
7.91k
        if (!m_suboptimal_idxs[chunk_idx]) {
1075
7.47k
            m_suboptimal_idxs.Set(chunk_idx);
1076
7.47k
            m_suboptimal_chunks.push_back(chunk_idx);
1077
7.47k
        }
1078
7.91k
    }
1079
1080
    /** Split a chunk, and then merge the resulting two chunks to make the graph topological
1081
     *  again. */
1082
    void Improve(TxIdx parent_idx, TxIdx child_idx) noexcept
1083
1.02M
    {
1084
        // Deactivate the specified dependency, splitting it into two new chunks: a top containing
1085
        // the parent, and a bottom containing the child. The top should have a higher feerate.
1086
1.02M
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(parent_idx, child_idx);
1087
1088
        // At this point we have exactly two chunks which may violate topology constraints (the
1089
        // parent chunk and child chunk that were produced by deactivation). We can fix
1090
        // these using just merge sequences, one upwards and one downwards, avoiding the need for a
1091
        // full MakeTopological.
1092
1.02M
        const auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1093
1.02M
        const auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1094
1.02M
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1095
            // The parent chunk has a dependency on a transaction in the child chunk. In this case,
1096
            // the parent needs to merge back with the child chunk (a self-merge), and no other
1097
            // merges are needed. Special-case this, so the overhead of PickMergeCandidate and
1098
            // MergeSequence can be avoided.
1099
1100
            // In the self-merge, the roles reverse: the parent chunk (from the split) depends
1101
            // on the child chunk, so child_chunk_idx is the "top" and parent_chunk_idx is the
1102
            // "bottom" for MergeChunks.
1103
803k
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1104
803k
            if (!m_suboptimal_idxs[merged_chunk_idx]) {
1105
803k
                m_suboptimal_idxs.Set(merged_chunk_idx);
1106
803k
                m_suboptimal_chunks.push_back(merged_chunk_idx);
1107
803k
            }
1108
803k
        } else {
1109
            // Merge the top chunk with lower-feerate chunks it depends on.
1110
219k
            MergeSequence<false>(parent_chunk_idx);
1111
            // Merge the bottom chunk with higher-feerate chunks that depend on it.
1112
219k
            MergeSequence<true>(child_chunk_idx);
1113
219k
        }
1114
1.02M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::Improve(unsigned int, unsigned int)
Line
Count
Source
1083
297k
    {
1084
        // Deactivate the specified dependency, splitting it into two new chunks: a top containing
1085
        // the parent, and a bottom containing the child. The top should have a higher feerate.
1086
297k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(parent_idx, child_idx);
1087
1088
        // At this point we have exactly two chunks which may violate topology constraints (the
1089
        // parent chunk and child chunk that were produced by deactivation). We can fix
1090
        // these using just merge sequences, one upwards and one downwards, avoiding the need for a
1091
        // full MakeTopological.
1092
297k
        const auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1093
297k
        const auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1094
297k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1095
            // The parent chunk has a dependency on a transaction in the child chunk. In this case,
1096
            // the parent needs to merge back with the child chunk (a self-merge), and no other
1097
            // merges are needed. Special-case this, so the overhead of PickMergeCandidate and
1098
            // MergeSequence can be avoided.
1099
1100
            // In the self-merge, the roles reverse: the parent chunk (from the split) depends
1101
            // on the child chunk, so child_chunk_idx is the "top" and parent_chunk_idx is the
1102
            // "bottom" for MergeChunks.
1103
229k
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1104
229k
            if (!m_suboptimal_idxs[merged_chunk_idx]) {
1105
229k
                m_suboptimal_idxs.Set(merged_chunk_idx);
1106
229k
                m_suboptimal_chunks.push_back(merged_chunk_idx);
1107
229k
            }
1108
229k
        } else {
1109
            // Merge the top chunk with lower-feerate chunks it depends on.
1110
67.2k
            MergeSequence<false>(parent_chunk_idx);
1111
            // Merge the bottom chunk with higher-feerate chunks that depend on it.
1112
67.2k
            MergeSequence<true>(child_chunk_idx);
1113
67.2k
        }
1114
297k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::Improve(unsigned int, unsigned int)
Line
Count
Source
1083
299k
    {
1084
        // Deactivate the specified dependency, splitting it into two new chunks: a top containing
1085
        // the parent, and a bottom containing the child. The top should have a higher feerate.
1086
299k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(parent_idx, child_idx);
1087
1088
        // At this point we have exactly two chunks which may violate topology constraints (the
1089
        // parent chunk and child chunk that were produced by deactivation). We can fix
1090
        // these using just merge sequences, one upwards and one downwards, avoiding the need for a
1091
        // full MakeTopological.
1092
299k
        const auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1093
299k
        const auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1094
299k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1095
            // The parent chunk has a dependency on a transaction in the child chunk. In this case,
1096
            // the parent needs to merge back with the child chunk (a self-merge), and no other
1097
            // merges are needed. Special-case this, so the overhead of PickMergeCandidate and
1098
            // MergeSequence can be avoided.
1099
1100
            // In the self-merge, the roles reverse: the parent chunk (from the split) depends
1101
            // on the child chunk, so child_chunk_idx is the "top" and parent_chunk_idx is the
1102
            // "bottom" for MergeChunks.
1103
230k
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1104
230k
            if (!m_suboptimal_idxs[merged_chunk_idx]) {
1105
230k
                m_suboptimal_idxs.Set(merged_chunk_idx);
1106
230k
                m_suboptimal_chunks.push_back(merged_chunk_idx);
1107
230k
            }
1108
230k
        } else {
1109
            // Merge the top chunk with lower-feerate chunks it depends on.
1110
68.5k
            MergeSequence<false>(parent_chunk_idx);
1111
            // Merge the bottom chunk with higher-feerate chunks that depend on it.
1112
68.5k
            MergeSequence<true>(child_chunk_idx);
1113
68.5k
        }
1114
299k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::Improve(unsigned int, unsigned int)
Line
Count
Source
1083
297k
    {
1084
        // Deactivate the specified dependency, splitting it into two new chunks: a top containing
1085
        // the parent, and a bottom containing the child. The top should have a higher feerate.
1086
297k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(parent_idx, child_idx);
1087
1088
        // At this point we have exactly two chunks which may violate topology constraints (the
1089
        // parent chunk and child chunk that were produced by deactivation). We can fix
1090
        // these using just merge sequences, one upwards and one downwards, avoiding the need for a
1091
        // full MakeTopological.
1092
297k
        const auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1093
297k
        const auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1094
297k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1095
            // The parent chunk has a dependency on a transaction in the child chunk. In this case,
1096
            // the parent needs to merge back with the child chunk (a self-merge), and no other
1097
            // merges are needed. Special-case this, so the overhead of PickMergeCandidate and
1098
            // MergeSequence can be avoided.
1099
1100
            // In the self-merge, the roles reverse: the parent chunk (from the split) depends
1101
            // on the child chunk, so child_chunk_idx is the "top" and parent_chunk_idx is the
1102
            // "bottom" for MergeChunks.
1103
229k
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1104
229k
            if (!m_suboptimal_idxs[merged_chunk_idx]) {
1105
229k
                m_suboptimal_idxs.Set(merged_chunk_idx);
1106
229k
                m_suboptimal_chunks.push_back(merged_chunk_idx);
1107
229k
            }
1108
229k
        } else {
1109
            // Merge the top chunk with lower-feerate chunks it depends on.
1110
67.6k
            MergeSequence<false>(parent_chunk_idx);
1111
            // Merge the bottom chunk with higher-feerate chunks that depend on it.
1112
67.6k
            MergeSequence<true>(child_chunk_idx);
1113
67.6k
        }
1114
297k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::Improve(unsigned int, unsigned int)
Line
Count
Source
1083
64.3k
    {
1084
        // Deactivate the specified dependency, splitting it into two new chunks: a top containing
1085
        // the parent, and a bottom containing the child. The top should have a higher feerate.
1086
64.3k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(parent_idx, child_idx);
1087
1088
        // At this point we have exactly two chunks which may violate topology constraints (the
1089
        // parent chunk and child chunk that were produced by deactivation). We can fix
1090
        // these using just merge sequences, one upwards and one downwards, avoiding the need for a
1091
        // full MakeTopological.
1092
64.3k
        const auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1093
64.3k
        const auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1094
64.3k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1095
            // The parent chunk has a dependency on a transaction in the child chunk. In this case,
1096
            // the parent needs to merge back with the child chunk (a self-merge), and no other
1097
            // merges are needed. Special-case this, so the overhead of PickMergeCandidate and
1098
            // MergeSequence can be avoided.
1099
1100
            // In the self-merge, the roles reverse: the parent chunk (from the split) depends
1101
            // on the child chunk, so child_chunk_idx is the "top" and parent_chunk_idx is the
1102
            // "bottom" for MergeChunks.
1103
56.6k
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1104
56.6k
            if (!m_suboptimal_idxs[merged_chunk_idx]) {
1105
56.6k
                m_suboptimal_idxs.Set(merged_chunk_idx);
1106
56.6k
                m_suboptimal_chunks.push_back(merged_chunk_idx);
1107
56.6k
            }
1108
56.6k
        } else {
1109
            // Merge the top chunk with lower-feerate chunks it depends on.
1110
7.78k
            MergeSequence<false>(parent_chunk_idx);
1111
            // Merge the bottom chunk with higher-feerate chunks that depend on it.
1112
7.78k
            MergeSequence<true>(child_chunk_idx);
1113
7.78k
        }
1114
64.3k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::Improve(unsigned int, unsigned int)
Line
Count
Source
1083
64.0k
    {
1084
        // Deactivate the specified dependency, splitting it into two new chunks: a top containing
1085
        // the parent, and a bottom containing the child. The top should have a higher feerate.
1086
64.0k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(parent_idx, child_idx);
1087
1088
        // At this point we have exactly two chunks which may violate topology constraints (the
1089
        // parent chunk and child chunk that were produced by deactivation). We can fix
1090
        // these using just merge sequences, one upwards and one downwards, avoiding the need for a
1091
        // full MakeTopological.
1092
64.0k
        const auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1093
64.0k
        const auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1094
64.0k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1095
            // The parent chunk has a dependency on a transaction in the child chunk. In this case,
1096
            // the parent needs to merge back with the child chunk (a self-merge), and no other
1097
            // merges are needed. Special-case this, so the overhead of PickMergeCandidate and
1098
            // MergeSequence can be avoided.
1099
1100
            // In the self-merge, the roles reverse: the parent chunk (from the split) depends
1101
            // on the child chunk, so child_chunk_idx is the "top" and parent_chunk_idx is the
1102
            // "bottom" for MergeChunks.
1103
56.1k
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1104
56.1k
            if (!m_suboptimal_idxs[merged_chunk_idx]) {
1105
56.1k
                m_suboptimal_idxs.Set(merged_chunk_idx);
1106
56.1k
                m_suboptimal_chunks.push_back(merged_chunk_idx);
1107
56.1k
            }
1108
56.1k
        } else {
1109
            // Merge the top chunk with lower-feerate chunks it depends on.
1110
7.91k
            MergeSequence<false>(parent_chunk_idx);
1111
            // Merge the bottom chunk with higher-feerate chunks that depend on it.
1112
7.91k
            MergeSequence<true>(child_chunk_idx);
1113
7.91k
        }
1114
64.0k
    }
1115
1116
    /** Determine the next chunk to optimize, or INVALID_SET_IDX if none. */
1117
    SetIdx PickChunkToOptimize() noexcept
1118
2.55M
    {
1119
2.55M
        m_cost.PickChunkToOptimizeBegin();
1120
2.55M
        unsigned steps{0};
1121
2.56M
        while (!m_suboptimal_chunks.empty()) {
1122
2.56M
            ++steps;
1123
            // Pop an entry from the potentially-suboptimal chunk queue.
1124
2.56M
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1125
2.56M
            Assume(m_suboptimal_idxs[chunk_idx]);
1126
2.56M
            m_suboptimal_idxs.Reset(chunk_idx);
1127
2.56M
            m_suboptimal_chunks.pop_front();
1128
2.56M
            if (m_chunk_idxs[chunk_idx]) {
1129
2.55M
                m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1130
2.55M
                return chunk_idx;
1131
2.55M
            }
1132
            // If what was popped is not currently a chunk, continue. This may
1133
            // happen when a split chunk merges in Improve() with one or more existing chunks that
1134
            // are themselves on the suboptimal queue already.
1135
2.56M
        }
1136
0
        m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1137
0
        return INVALID_SET_IDX;
1138
2.55M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::PickChunkToOptimize()
Line
Count
Source
1118
744k
    {
1119
744k
        m_cost.PickChunkToOptimizeBegin();
1120
744k
        unsigned steps{0};
1121
746k
        while (!m_suboptimal_chunks.empty()) {
1122
746k
            ++steps;
1123
            // Pop an entry from the potentially-suboptimal chunk queue.
1124
746k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1125
746k
            Assume(m_suboptimal_idxs[chunk_idx]);
1126
746k
            m_suboptimal_idxs.Reset(chunk_idx);
1127
746k
            m_suboptimal_chunks.pop_front();
1128
746k
            if (m_chunk_idxs[chunk_idx]) {
1129
744k
                m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1130
744k
                return chunk_idx;
1131
744k
            }
1132
            // If what was popped is not currently a chunk, continue. This may
1133
            // happen when a split chunk merges in Improve() with one or more existing chunks that
1134
            // are themselves on the suboptimal queue already.
1135
746k
        }
1136
0
        m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1137
0
        return INVALID_SET_IDX;
1138
744k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::PickChunkToOptimize()
Line
Count
Source
1118
735k
    {
1119
735k
        m_cost.PickChunkToOptimizeBegin();
1120
735k
        unsigned steps{0};
1121
737k
        while (!m_suboptimal_chunks.empty()) {
1122
737k
            ++steps;
1123
            // Pop an entry from the potentially-suboptimal chunk queue.
1124
737k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1125
737k
            Assume(m_suboptimal_idxs[chunk_idx]);
1126
737k
            m_suboptimal_idxs.Reset(chunk_idx);
1127
737k
            m_suboptimal_chunks.pop_front();
1128
737k
            if (m_chunk_idxs[chunk_idx]) {
1129
735k
                m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1130
735k
                return chunk_idx;
1131
735k
            }
1132
            // If what was popped is not currently a chunk, continue. This may
1133
            // happen when a split chunk merges in Improve() with one or more existing chunks that
1134
            // are themselves on the suboptimal queue already.
1135
737k
        }
1136
0
        m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1137
0
        return INVALID_SET_IDX;
1138
735k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::PickChunkToOptimize()
Line
Count
Source
1118
733k
    {
1119
733k
        m_cost.PickChunkToOptimizeBegin();
1120
733k
        unsigned steps{0};
1121
736k
        while (!m_suboptimal_chunks.empty()) {
1122
736k
            ++steps;
1123
            // Pop an entry from the potentially-suboptimal chunk queue.
1124
736k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1125
736k
            Assume(m_suboptimal_idxs[chunk_idx]);
1126
736k
            m_suboptimal_idxs.Reset(chunk_idx);
1127
736k
            m_suboptimal_chunks.pop_front();
1128
736k
            if (m_chunk_idxs[chunk_idx]) {
1129
733k
                m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1130
733k
                return chunk_idx;
1131
733k
            }
1132
            // If what was popped is not currently a chunk, continue. This may
1133
            // happen when a split chunk merges in Improve() with one or more existing chunks that
1134
            // are themselves on the suboptimal queue already.
1135
736k
        }
1136
0
        m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1137
0
        return INVALID_SET_IDX;
1138
733k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::PickChunkToOptimize()
Line
Count
Source
1118
172k
    {
1119
172k
        m_cost.PickChunkToOptimizeBegin();
1120
172k
        unsigned steps{0};
1121
172k
        while (!m_suboptimal_chunks.empty()) {
1122
172k
            ++steps;
1123
            // Pop an entry from the potentially-suboptimal chunk queue.
1124
172k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1125
172k
            Assume(m_suboptimal_idxs[chunk_idx]);
1126
172k
            m_suboptimal_idxs.Reset(chunk_idx);
1127
172k
            m_suboptimal_chunks.pop_front();
1128
172k
            if (m_chunk_idxs[chunk_idx]) {
1129
172k
                m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1130
172k
                return chunk_idx;
1131
172k
            }
1132
            // If what was popped is not currently a chunk, continue. This may
1133
            // happen when a split chunk merges in Improve() with one or more existing chunks that
1134
            // are themselves on the suboptimal queue already.
1135
172k
        }
1136
0
        m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1137
0
        return INVALID_SET_IDX;
1138
172k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::PickChunkToOptimize()
Line
Count
Source
1118
172k
    {
1119
172k
        m_cost.PickChunkToOptimizeBegin();
1120
172k
        unsigned steps{0};
1121
172k
        while (!m_suboptimal_chunks.empty()) {
1122
172k
            ++steps;
1123
            // Pop an entry from the potentially-suboptimal chunk queue.
1124
172k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1125
172k
            Assume(m_suboptimal_idxs[chunk_idx]);
1126
172k
            m_suboptimal_idxs.Reset(chunk_idx);
1127
172k
            m_suboptimal_chunks.pop_front();
1128
172k
            if (m_chunk_idxs[chunk_idx]) {
1129
172k
                m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1130
172k
                return chunk_idx;
1131
172k
            }
1132
            // If what was popped is not currently a chunk, continue. This may
1133
            // happen when a split chunk merges in Improve() with one or more existing chunks that
1134
            // are themselves on the suboptimal queue already.
1135
172k
        }
1136
0
        m_cost.PickChunkToOptimizeEnd(/*num_steps=*/steps);
1137
0
        return INVALID_SET_IDX;
1138
172k
    }
1139
1140
    /** Find a (parent, child) dependency to deactivate in chunk_idx, or (-1, -1) if none. */
1141
    std::pair<TxIdx, TxIdx> PickDependencyToSplit(SetIdx chunk_idx) noexcept
1142
2.55M
    {
1143
2.55M
        m_cost.PickDependencyToSplitBegin();
1144
2.55M
        Assume(m_chunk_idxs[chunk_idx]);
1145
2.55M
        auto& chunk_info = m_set_info[chunk_idx];
1146
1147
        // Remember the best dependency {par, chl} seen so far.
1148
2.55M
        std::pair<TxIdx, TxIdx> candidate_dep = {TxIdx(-1), TxIdx(-1)};
1149
2.55M
        uint64_t candidate_tiebreak = 0;
1150
        // Iterate over all transactions.
1151
30.1M
        for (auto tx_idx : chunk_info.transactions) {
1152
30.1M
            const auto& tx_data = m_tx_data[tx_idx];
1153
            // Iterate over all active child dependencies of the transaction.
1154
30.1M
            for (auto child_idx : tx_data.active_children) {
1155
27.6M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1156
                // Skip if this dependency is ineligible (the top chunk that would be created
1157
                // does not have higher feerate than the chunk it is currently part of).
1158
27.6M
                auto cmp = ByRatio{dep_top_info.feerate} <=> ByRatio{chunk_info.feerate};
1159
27.6M
                if (cmp <= 0) continue;
1160
                // Generate a random tiebreak for this dependency, and reject it if its tiebreak
1161
                // is worse than the best so far. This means that among all eligible
1162
                // dependencies, a uniformly random one will be chosen.
1163
4.41M
                uint64_t tiebreak = m_rng.rand64();
1164
4.41M
                if (tiebreak < candidate_tiebreak) continue;
1165
                // Remember this as our (new) candidate dependency.
1166
1.97M
                candidate_dep = {tx_idx, child_idx};
1167
1.97M
                candidate_tiebreak = tiebreak;
1168
1.97M
            }
1169
30.1M
        }
1170
2.55M
        m_cost.PickDependencyToSplitEnd(/*num_txns=*/chunk_info.transactions.Count());
1171
2.55M
        return candidate_dep;
1172
2.55M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::PickDependencyToSplit(unsigned char)
Line
Count
Source
1142
744k
    {
1143
744k
        m_cost.PickDependencyToSplitBegin();
1144
744k
        Assume(m_chunk_idxs[chunk_idx]);
1145
744k
        auto& chunk_info = m_set_info[chunk_idx];
1146
1147
        // Remember the best dependency {par, chl} seen so far.
1148
744k
        std::pair<TxIdx, TxIdx> candidate_dep = {TxIdx(-1), TxIdx(-1)};
1149
744k
        uint64_t candidate_tiebreak = 0;
1150
        // Iterate over all transactions.
1151
9.19M
        for (auto tx_idx : chunk_info.transactions) {
1152
9.19M
            const auto& tx_data = m_tx_data[tx_idx];
1153
            // Iterate over all active child dependencies of the transaction.
1154
9.19M
            for (auto child_idx : tx_data.active_children) {
1155
8.44M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1156
                // Skip if this dependency is ineligible (the top chunk that would be created
1157
                // does not have higher feerate than the chunk it is currently part of).
1158
8.44M
                auto cmp = ByRatio{dep_top_info.feerate} <=> ByRatio{chunk_info.feerate};
1159
8.44M
                if (cmp <= 0) continue;
1160
                // Generate a random tiebreak for this dependency, and reject it if its tiebreak
1161
                // is worse than the best so far. This means that among all eligible
1162
                // dependencies, a uniformly random one will be chosen.
1163
1.36M
                uint64_t tiebreak = m_rng.rand64();
1164
1.36M
                if (tiebreak < candidate_tiebreak) continue;
1165
                // Remember this as our (new) candidate dependency.
1166
592k
                candidate_dep = {tx_idx, child_idx};
1167
592k
                candidate_tiebreak = tiebreak;
1168
592k
            }
1169
9.19M
        }
1170
744k
        m_cost.PickDependencyToSplitEnd(/*num_txns=*/chunk_info.transactions.Count());
1171
744k
        return candidate_dep;
1172
744k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::PickDependencyToSplit(unsigned char)
Line
Count
Source
1142
735k
    {
1143
735k
        m_cost.PickDependencyToSplitBegin();
1144
735k
        Assume(m_chunk_idxs[chunk_idx]);
1145
735k
        auto& chunk_info = m_set_info[chunk_idx];
1146
1147
        // Remember the best dependency {par, chl} seen so far.
1148
735k
        std::pair<TxIdx, TxIdx> candidate_dep = {TxIdx(-1), TxIdx(-1)};
1149
735k
        uint64_t candidate_tiebreak = 0;
1150
        // Iterate over all transactions.
1151
9.17M
        for (auto tx_idx : chunk_info.transactions) {
1152
9.17M
            const auto& tx_data = m_tx_data[tx_idx];
1153
            // Iterate over all active child dependencies of the transaction.
1154
9.17M
            for (auto child_idx : tx_data.active_children) {
1155
8.44M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1156
                // Skip if this dependency is ineligible (the top chunk that would be created
1157
                // does not have higher feerate than the chunk it is currently part of).
1158
8.44M
                auto cmp = ByRatio{dep_top_info.feerate} <=> ByRatio{chunk_info.feerate};
1159
8.44M
                if (cmp <= 0) continue;
1160
                // Generate a random tiebreak for this dependency, and reject it if its tiebreak
1161
                // is worse than the best so far. This means that among all eligible
1162
                // dependencies, a uniformly random one will be chosen.
1163
1.38M
                uint64_t tiebreak = m_rng.rand64();
1164
1.38M
                if (tiebreak < candidate_tiebreak) continue;
1165
                // Remember this as our (new) candidate dependency.
1166
596k
                candidate_dep = {tx_idx, child_idx};
1167
596k
                candidate_tiebreak = tiebreak;
1168
596k
            }
1169
9.17M
        }
1170
735k
        m_cost.PickDependencyToSplitEnd(/*num_txns=*/chunk_info.transactions.Count());
1171
735k
        return candidate_dep;
1172
735k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::PickDependencyToSplit(unsigned char)
Line
Count
Source
1142
733k
    {
1143
733k
        m_cost.PickDependencyToSplitBegin();
1144
733k
        Assume(m_chunk_idxs[chunk_idx]);
1145
733k
        auto& chunk_info = m_set_info[chunk_idx];
1146
1147
        // Remember the best dependency {par, chl} seen so far.
1148
733k
        std::pair<TxIdx, TxIdx> candidate_dep = {TxIdx(-1), TxIdx(-1)};
1149
733k
        uint64_t candidate_tiebreak = 0;
1150
        // Iterate over all transactions.
1151
9.15M
        for (auto tx_idx : chunk_info.transactions) {
1152
9.15M
            const auto& tx_data = m_tx_data[tx_idx];
1153
            // Iterate over all active child dependencies of the transaction.
1154
9.15M
            for (auto child_idx : tx_data.active_children) {
1155
8.41M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1156
                // Skip if this dependency is ineligible (the top chunk that would be created
1157
                // does not have higher feerate than the chunk it is currently part of).
1158
8.41M
                auto cmp = ByRatio{dep_top_info.feerate} <=> ByRatio{chunk_info.feerate};
1159
8.41M
                if (cmp <= 0) continue;
1160
                // Generate a random tiebreak for this dependency, and reject it if its tiebreak
1161
                // is worse than the best so far. This means that among all eligible
1162
                // dependencies, a uniformly random one will be chosen.
1163
1.37M
                uint64_t tiebreak = m_rng.rand64();
1164
1.37M
                if (tiebreak < candidate_tiebreak) continue;
1165
                // Remember this as our (new) candidate dependency.
1166
594k
                candidate_dep = {tx_idx, child_idx};
1167
594k
                candidate_tiebreak = tiebreak;
1168
594k
            }
1169
9.15M
        }
1170
733k
        m_cost.PickDependencyToSplitEnd(/*num_txns=*/chunk_info.transactions.Count());
1171
733k
        return candidate_dep;
1172
733k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::PickDependencyToSplit(unsigned char)
Line
Count
Source
1142
172k
    {
1143
172k
        m_cost.PickDependencyToSplitBegin();
1144
172k
        Assume(m_chunk_idxs[chunk_idx]);
1145
172k
        auto& chunk_info = m_set_info[chunk_idx];
1146
1147
        // Remember the best dependency {par, chl} seen so far.
1148
172k
        std::pair<TxIdx, TxIdx> candidate_dep = {TxIdx(-1), TxIdx(-1)};
1149
172k
        uint64_t candidate_tiebreak = 0;
1150
        // Iterate over all transactions.
1151
1.33M
        for (auto tx_idx : chunk_info.transactions) {
1152
1.33M
            const auto& tx_data = m_tx_data[tx_idx];
1153
            // Iterate over all active child dependencies of the transaction.
1154
1.33M
            for (auto child_idx : tx_data.active_children) {
1155
1.16M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1156
                // Skip if this dependency is ineligible (the top chunk that would be created
1157
                // does not have higher feerate than the chunk it is currently part of).
1158
1.16M
                auto cmp = ByRatio{dep_top_info.feerate} <=> ByRatio{chunk_info.feerate};
1159
1.16M
                if (cmp <= 0) continue;
1160
                // Generate a random tiebreak for this dependency, and reject it if its tiebreak
1161
                // is worse than the best so far. This means that among all eligible
1162
                // dependencies, a uniformly random one will be chosen.
1163
145k
                uint64_t tiebreak = m_rng.rand64();
1164
145k
                if (tiebreak < candidate_tiebreak) continue;
1165
                // Remember this as our (new) candidate dependency.
1166
95.9k
                candidate_dep = {tx_idx, child_idx};
1167
95.9k
                candidate_tiebreak = tiebreak;
1168
95.9k
            }
1169
1.33M
        }
1170
172k
        m_cost.PickDependencyToSplitEnd(/*num_txns=*/chunk_info.transactions.Count());
1171
172k
        return candidate_dep;
1172
172k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::PickDependencyToSplit(unsigned char)
Line
Count
Source
1142
172k
    {
1143
172k
        m_cost.PickDependencyToSplitBegin();
1144
172k
        Assume(m_chunk_idxs[chunk_idx]);
1145
172k
        auto& chunk_info = m_set_info[chunk_idx];
1146
1147
        // Remember the best dependency {par, chl} seen so far.
1148
172k
        std::pair<TxIdx, TxIdx> candidate_dep = {TxIdx(-1), TxIdx(-1)};
1149
172k
        uint64_t candidate_tiebreak = 0;
1150
        // Iterate over all transactions.
1151
1.33M
        for (auto tx_idx : chunk_info.transactions) {
1152
1.33M
            const auto& tx_data = m_tx_data[tx_idx];
1153
            // Iterate over all active child dependencies of the transaction.
1154
1.33M
            for (auto child_idx : tx_data.active_children) {
1155
1.16M
                auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1156
                // Skip if this dependency is ineligible (the top chunk that would be created
1157
                // does not have higher feerate than the chunk it is currently part of).
1158
1.16M
                auto cmp = ByRatio{dep_top_info.feerate} <=> ByRatio{chunk_info.feerate};
1159
1.16M
                if (cmp <= 0) continue;
1160
                // Generate a random tiebreak for this dependency, and reject it if its tiebreak
1161
                // is worse than the best so far. This means that among all eligible
1162
                // dependencies, a uniformly random one will be chosen.
1163
144k
                uint64_t tiebreak = m_rng.rand64();
1164
144k
                if (tiebreak < candidate_tiebreak) continue;
1165
                // Remember this as our (new) candidate dependency.
1166
95.4k
                candidate_dep = {tx_idx, child_idx};
1167
95.4k
                candidate_tiebreak = tiebreak;
1168
95.4k
            }
1169
1.33M
        }
1170
172k
        m_cost.PickDependencyToSplitEnd(/*num_txns=*/chunk_info.transactions.Count());
1171
172k
        return candidate_dep;
1172
172k
    }
1173
1174
public:
1175
    /** Construct a spanning forest for the given DepGraph, with every transaction in its own chunk
1176
     *  (not topological). */
1177
    explicit SpanningForestState(const DepGraph<SetType>& depgraph LIFETIMEBOUND, uint64_t rng_seed, const CostModel& cost = CostModel{}) noexcept :
1178
191k
        m_rng(rng_seed), m_depgraph(depgraph), m_cost(cost)
1179
191k
    {
1180
191k
        m_cost.InitializeBegin();
1181
191k
        m_transaction_idxs = depgraph.Positions();
1182
191k
        auto num_transactions = m_transaction_idxs.Count();
1183
191k
        m_tx_data.resize(depgraph.PositionRange());
1184
191k
        m_set_info.resize(num_transactions);
1185
191k
        m_reachable.resize(num_transactions);
1186
191k
        size_t num_chunks = 0;
1187
191k
        size_t num_deps = 0;
1188
5.07M
        for (auto tx_idx : m_transaction_idxs) {
1189
            // Fill in transaction data.
1190
5.07M
            auto& tx_data = m_tx_data[tx_idx];
1191
5.07M
            tx_data.parents = depgraph.GetReducedParents(tx_idx);
1192
15.0M
            for (auto parent_idx : tx_data.parents) {
1193
15.0M
                m_tx_data[parent_idx].children.Set(tx_idx);
1194
15.0M
            }
1195
5.07M
            num_deps += tx_data.parents.Count();
1196
            // Create a singleton chunk for it.
1197
5.07M
            tx_data.chunk_idx = num_chunks;
1198
5.07M
            m_set_info[num_chunks++] = SetInfo(depgraph, tx_idx);
1199
5.07M
        }
1200
        // Set the reachable transactions for each chunk to the transactions' parents and children.
1201
5.26M
        for (SetIdx chunk_idx = 0; chunk_idx < num_transactions; ++chunk_idx) {
1202
5.07M
            auto& tx_data = m_tx_data[m_set_info[chunk_idx].transactions.First()];
1203
5.07M
            m_reachable[chunk_idx].first = tx_data.parents;
1204
5.07M
            m_reachable[chunk_idx].second = tx_data.children;
1205
5.07M
        }
1206
191k
        Assume(num_chunks == num_transactions);
1207
        // Mark all chunk sets as chunks.
1208
191k
        m_chunk_idxs = SetType::Fill(num_chunks);
1209
191k
        m_cost.InitializeEnd(/*num_txns=*/num_chunks, /*num_deps=*/num_deps);
1210
191k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::SpanningForestState(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>> const&, unsigned long, cluster_linearize::SFLDefaultCostModel const&)
Line
Count
Source
1178
50.5k
        m_rng(rng_seed), m_depgraph(depgraph), m_cost(cost)
1179
50.5k
    {
1180
50.5k
        m_cost.InitializeBegin();
1181
50.5k
        m_transaction_idxs = depgraph.Positions();
1182
50.5k
        auto num_transactions = m_transaction_idxs.Count();
1183
50.5k
        m_tx_data.resize(depgraph.PositionRange());
1184
50.5k
        m_set_info.resize(num_transactions);
1185
50.5k
        m_reachable.resize(num_transactions);
1186
50.5k
        size_t num_chunks = 0;
1187
50.5k
        size_t num_deps = 0;
1188
1.45M
        for (auto tx_idx : m_transaction_idxs) {
1189
            // Fill in transaction data.
1190
1.45M
            auto& tx_data = m_tx_data[tx_idx];
1191
1.45M
            tx_data.parents = depgraph.GetReducedParents(tx_idx);
1192
4.49M
            for (auto parent_idx : tx_data.parents) {
1193
4.49M
                m_tx_data[parent_idx].children.Set(tx_idx);
1194
4.49M
            }
1195
1.45M
            num_deps += tx_data.parents.Count();
1196
            // Create a singleton chunk for it.
1197
1.45M
            tx_data.chunk_idx = num_chunks;
1198
1.45M
            m_set_info[num_chunks++] = SetInfo(depgraph, tx_idx);
1199
1.45M
        }
1200
        // Set the reachable transactions for each chunk to the transactions' parents and children.
1201
1.50M
        for (SetIdx chunk_idx = 0; chunk_idx < num_transactions; ++chunk_idx) {
1202
1.45M
            auto& tx_data = m_tx_data[m_set_info[chunk_idx].transactions.First()];
1203
1.45M
            m_reachable[chunk_idx].first = tx_data.parents;
1204
1.45M
            m_reachable[chunk_idx].second = tx_data.children;
1205
1.45M
        }
1206
50.5k
        Assume(num_chunks == num_transactions);
1207
        // Mark all chunk sets as chunks.
1208
50.5k
        m_chunk_idxs = SetType::Fill(num_chunks);
1209
50.5k
        m_cost.InitializeEnd(/*num_txns=*/num_chunks, /*num_deps=*/num_deps);
1210
50.5k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::SpanningForestState(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>> const&, unsigned long, cluster_linearize::SFLDefaultCostModel const&)
Line
Count
Source
1178
45.4k
        m_rng(rng_seed), m_depgraph(depgraph), m_cost(cost)
1179
45.4k
    {
1180
45.4k
        m_cost.InitializeBegin();
1181
45.4k
        m_transaction_idxs = depgraph.Positions();
1182
45.4k
        auto num_transactions = m_transaction_idxs.Count();
1183
45.4k
        m_tx_data.resize(depgraph.PositionRange());
1184
45.4k
        m_set_info.resize(num_transactions);
1185
45.4k
        m_reachable.resize(num_transactions);
1186
45.4k
        size_t num_chunks = 0;
1187
45.4k
        size_t num_deps = 0;
1188
1.38M
        for (auto tx_idx : m_transaction_idxs) {
1189
            // Fill in transaction data.
1190
1.38M
            auto& tx_data = m_tx_data[tx_idx];
1191
1.38M
            tx_data.parents = depgraph.GetReducedParents(tx_idx);
1192
4.42M
            for (auto parent_idx : tx_data.parents) {
1193
4.42M
                m_tx_data[parent_idx].children.Set(tx_idx);
1194
4.42M
            }
1195
1.38M
            num_deps += tx_data.parents.Count();
1196
            // Create a singleton chunk for it.
1197
1.38M
            tx_data.chunk_idx = num_chunks;
1198
1.38M
            m_set_info[num_chunks++] = SetInfo(depgraph, tx_idx);
1199
1.38M
        }
1200
        // Set the reachable transactions for each chunk to the transactions' parents and children.
1201
1.43M
        for (SetIdx chunk_idx = 0; chunk_idx < num_transactions; ++chunk_idx) {
1202
1.38M
            auto& tx_data = m_tx_data[m_set_info[chunk_idx].transactions.First()];
1203
1.38M
            m_reachable[chunk_idx].first = tx_data.parents;
1204
1.38M
            m_reachable[chunk_idx].second = tx_data.children;
1205
1.38M
        }
1206
45.4k
        Assume(num_chunks == num_transactions);
1207
        // Mark all chunk sets as chunks.
1208
45.4k
        m_chunk_idxs = SetType::Fill(num_chunks);
1209
45.4k
        m_cost.InitializeEnd(/*num_txns=*/num_chunks, /*num_deps=*/num_deps);
1210
45.4k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::SpanningForestState(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>> const&, unsigned long, cluster_linearize::SFLDefaultCostModel const&)
Line
Count
Source
1178
45.4k
        m_rng(rng_seed), m_depgraph(depgraph), m_cost(cost)
1179
45.4k
    {
1180
45.4k
        m_cost.InitializeBegin();
1181
45.4k
        m_transaction_idxs = depgraph.Positions();
1182
45.4k
        auto num_transactions = m_transaction_idxs.Count();
1183
45.4k
        m_tx_data.resize(depgraph.PositionRange());
1184
45.4k
        m_set_info.resize(num_transactions);
1185
45.4k
        m_reachable.resize(num_transactions);
1186
45.4k
        size_t num_chunks = 0;
1187
45.4k
        size_t num_deps = 0;
1188
1.38M
        for (auto tx_idx : m_transaction_idxs) {
1189
            // Fill in transaction data.
1190
1.38M
            auto& tx_data = m_tx_data[tx_idx];
1191
1.38M
            tx_data.parents = depgraph.GetReducedParents(tx_idx);
1192
4.42M
            for (auto parent_idx : tx_data.parents) {
1193
4.42M
                m_tx_data[parent_idx].children.Set(tx_idx);
1194
4.42M
            }
1195
1.38M
            num_deps += tx_data.parents.Count();
1196
            // Create a singleton chunk for it.
1197
1.38M
            tx_data.chunk_idx = num_chunks;
1198
1.38M
            m_set_info[num_chunks++] = SetInfo(depgraph, tx_idx);
1199
1.38M
        }
1200
        // Set the reachable transactions for each chunk to the transactions' parents and children.
1201
1.43M
        for (SetIdx chunk_idx = 0; chunk_idx < num_transactions; ++chunk_idx) {
1202
1.38M
            auto& tx_data = m_tx_data[m_set_info[chunk_idx].transactions.First()];
1203
1.38M
            m_reachable[chunk_idx].first = tx_data.parents;
1204
1.38M
            m_reachable[chunk_idx].second = tx_data.children;
1205
1.38M
        }
1206
45.4k
        Assume(num_chunks == num_transactions);
1207
        // Mark all chunk sets as chunks.
1208
45.4k
        m_chunk_idxs = SetType::Fill(num_chunks);
1209
45.4k
        m_cost.InitializeEnd(/*num_txns=*/num_chunks, /*num_deps=*/num_deps);
1210
45.4k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::SpanningForestState(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>> const&, unsigned long, cluster_linearize::SFLDefaultCostModel const&)
Line
Count
Source
1178
25.0k
        m_rng(rng_seed), m_depgraph(depgraph), m_cost(cost)
1179
25.0k
    {
1180
25.0k
        m_cost.InitializeBegin();
1181
25.0k
        m_transaction_idxs = depgraph.Positions();
1182
25.0k
        auto num_transactions = m_transaction_idxs.Count();
1183
25.0k
        m_tx_data.resize(depgraph.PositionRange());
1184
25.0k
        m_set_info.resize(num_transactions);
1185
25.0k
        m_reachable.resize(num_transactions);
1186
25.0k
        size_t num_chunks = 0;
1187
25.0k
        size_t num_deps = 0;
1188
421k
        for (auto tx_idx : m_transaction_idxs) {
1189
            // Fill in transaction data.
1190
421k
            auto& tx_data = m_tx_data[tx_idx];
1191
421k
            tx_data.parents = depgraph.GetReducedParents(tx_idx);
1192
844k
            for (auto parent_idx : tx_data.parents) {
1193
844k
                m_tx_data[parent_idx].children.Set(tx_idx);
1194
844k
            }
1195
421k
            num_deps += tx_data.parents.Count();
1196
            // Create a singleton chunk for it.
1197
421k
            tx_data.chunk_idx = num_chunks;
1198
421k
            m_set_info[num_chunks++] = SetInfo(depgraph, tx_idx);
1199
421k
        }
1200
        // Set the reachable transactions for each chunk to the transactions' parents and children.
1201
446k
        for (SetIdx chunk_idx = 0; chunk_idx < num_transactions; ++chunk_idx) {
1202
421k
            auto& tx_data = m_tx_data[m_set_info[chunk_idx].transactions.First()];
1203
421k
            m_reachable[chunk_idx].first = tx_data.parents;
1204
421k
            m_reachable[chunk_idx].second = tx_data.children;
1205
421k
        }
1206
25.0k
        Assume(num_chunks == num_transactions);
1207
        // Mark all chunk sets as chunks.
1208
25.0k
        m_chunk_idxs = SetType::Fill(num_chunks);
1209
25.0k
        m_cost.InitializeEnd(/*num_txns=*/num_chunks, /*num_deps=*/num_deps);
1210
25.0k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::SpanningForestState(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>> const&, unsigned long, cluster_linearize::SFLDefaultCostModel const&)
Line
Count
Source
1178
25.0k
        m_rng(rng_seed), m_depgraph(depgraph), m_cost(cost)
1179
25.0k
    {
1180
25.0k
        m_cost.InitializeBegin();
1181
25.0k
        m_transaction_idxs = depgraph.Positions();
1182
25.0k
        auto num_transactions = m_transaction_idxs.Count();
1183
25.0k
        m_tx_data.resize(depgraph.PositionRange());
1184
25.0k
        m_set_info.resize(num_transactions);
1185
25.0k
        m_reachable.resize(num_transactions);
1186
25.0k
        size_t num_chunks = 0;
1187
25.0k
        size_t num_deps = 0;
1188
421k
        for (auto tx_idx : m_transaction_idxs) {
1189
            // Fill in transaction data.
1190
421k
            auto& tx_data = m_tx_data[tx_idx];
1191
421k
            tx_data.parents = depgraph.GetReducedParents(tx_idx);
1192
844k
            for (auto parent_idx : tx_data.parents) {
1193
844k
                m_tx_data[parent_idx].children.Set(tx_idx);
1194
844k
            }
1195
421k
            num_deps += tx_data.parents.Count();
1196
            // Create a singleton chunk for it.
1197
421k
            tx_data.chunk_idx = num_chunks;
1198
421k
            m_set_info[num_chunks++] = SetInfo(depgraph, tx_idx);
1199
421k
        }
1200
        // Set the reachable transactions for each chunk to the transactions' parents and children.
1201
446k
        for (SetIdx chunk_idx = 0; chunk_idx < num_transactions; ++chunk_idx) {
1202
421k
            auto& tx_data = m_tx_data[m_set_info[chunk_idx].transactions.First()];
1203
421k
            m_reachable[chunk_idx].first = tx_data.parents;
1204
421k
            m_reachable[chunk_idx].second = tx_data.children;
1205
421k
        }
1206
25.0k
        Assume(num_chunks == num_transactions);
1207
        // Mark all chunk sets as chunks.
1208
25.0k
        m_chunk_idxs = SetType::Fill(num_chunks);
1209
25.0k
        m_cost.InitializeEnd(/*num_txns=*/num_chunks, /*num_deps=*/num_deps);
1210
25.0k
    }
1211
1212
    /** Load an existing linearization. Must be called immediately after constructor. The result is
1213
     *  topological if the linearization is valid. Otherwise, MakeTopological still needs to be
1214
     *  called. */
1215
    void LoadLinearization(std::span<const DepGraphIndex> old_linearization) noexcept
1216
144k
    {
1217
        // Add transactions one by one, in order of existing linearization.
1218
3.80M
        for (DepGraphIndex tx_idx : old_linearization) {
1219
3.80M
            auto chunk_idx = m_tx_data[tx_idx].chunk_idx;
1220
            // Merge the chunk upwards, as long as merging succeeds.
1221
6.58M
            while (true) {
1222
6.58M
                chunk_idx = MergeStep<false>(chunk_idx);
1223
6.58M
                if (chunk_idx == INVALID_SET_IDX) break;
1224
6.58M
            }
1225
3.80M
        }
1226
144k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::LoadLinearization(std::span<unsigned int const, 18446744073709551615ul>)
Line
Count
Source
1216
39.0k
    {
1217
        // Add transactions one by one, in order of existing linearization.
1218
1.10M
        for (DepGraphIndex tx_idx : old_linearization) {
1219
1.10M
            auto chunk_idx = m_tx_data[tx_idx].chunk_idx;
1220
            // Merge the chunk upwards, as long as merging succeeds.
1221
1.90M
            while (true) {
1222
1.90M
                chunk_idx = MergeStep<false>(chunk_idx);
1223
1.90M
                if (chunk_idx == INVALID_SET_IDX) break;
1224
1.90M
            }
1225
1.10M
        }
1226
39.0k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::LoadLinearization(std::span<unsigned int const, 18446744073709551615ul>)
Line
Count
Source
1216
33.8k
    {
1217
        // Add transactions one by one, in order of existing linearization.
1218
1.03M
        for (DepGraphIndex tx_idx : old_linearization) {
1219
1.03M
            auto chunk_idx = m_tx_data[tx_idx].chunk_idx;
1220
            // Merge the chunk upwards, as long as merging succeeds.
1221
1.78M
            while (true) {
1222
1.78M
                chunk_idx = MergeStep<false>(chunk_idx);
1223
1.78M
                if (chunk_idx == INVALID_SET_IDX) break;
1224
1.78M
            }
1225
1.03M
        }
1226
33.8k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::LoadLinearization(std::span<unsigned int const, 18446744073709551615ul>)
Line
Count
Source
1216
34.0k
    {
1217
        // Add transactions one by one, in order of existing linearization.
1218
1.03M
        for (DepGraphIndex tx_idx : old_linearization) {
1219
1.03M
            auto chunk_idx = m_tx_data[tx_idx].chunk_idx;
1220
            // Merge the chunk upwards, as long as merging succeeds.
1221
1.78M
            while (true) {
1222
1.78M
                chunk_idx = MergeStep<false>(chunk_idx);
1223
1.78M
                if (chunk_idx == INVALID_SET_IDX) break;
1224
1.78M
            }
1225
1.03M
        }
1226
34.0k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::LoadLinearization(std::span<unsigned int const, 18446744073709551615ul>)
Line
Count
Source
1216
18.5k
    {
1217
        // Add transactions one by one, in order of existing linearization.
1218
313k
        for (DepGraphIndex tx_idx : old_linearization) {
1219
313k
            auto chunk_idx = m_tx_data[tx_idx].chunk_idx;
1220
            // Merge the chunk upwards, as long as merging succeeds.
1221
550k
            while (true) {
1222
550k
                chunk_idx = MergeStep<false>(chunk_idx);
1223
550k
                if (chunk_idx == INVALID_SET_IDX) break;
1224
550k
            }
1225
313k
        }
1226
18.5k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::LoadLinearization(std::span<unsigned int const, 18446744073709551615ul>)
Line
Count
Source
1216
18.6k
    {
1217
        // Add transactions one by one, in order of existing linearization.
1218
313k
        for (DepGraphIndex tx_idx : old_linearization) {
1219
313k
            auto chunk_idx = m_tx_data[tx_idx].chunk_idx;
1220
            // Merge the chunk upwards, as long as merging succeeds.
1221
551k
            while (true) {
1222
551k
                chunk_idx = MergeStep<false>(chunk_idx);
1223
551k
                if (chunk_idx == INVALID_SET_IDX) break;
1224
551k
            }
1225
313k
        }
1226
18.6k
    }
1227
1228
    /** Make state topological. Can be called after constructing, or after LoadLinearization. */
1229
    void MakeTopological() noexcept
1230
98.7k
    {
1231
98.7k
        m_cost.MakeTopologicalBegin();
1232
98.7k
        Assume(m_suboptimal_chunks.empty());
1233
        /** What direction to initially merge chunks in; one of the two directions is enough. This
1234
         *  is sufficient because if a non-topological inactive dependency exists between two
1235
         *  chunks, at least one of the two chunks will eventually be processed in a direction that
1236
         *  discovers it - either the lower chunk tries upward, or the upper chunk tries downward.
1237
         *  Chunks that are the result of the merging are always tried in both directions. */
1238
98.7k
        unsigned init_dir = m_rng.randbool();
1239
        /** Which chunks are the result of merging, and thus need merge attempts in both
1240
         *  directions. */
1241
98.7k
        SetType merged_chunks;
1242
        // Mark chunks as suboptimal.
1243
98.7k
        m_suboptimal_idxs = m_chunk_idxs;
1244
1.60M
        for (auto chunk_idx : m_chunk_idxs) {
1245
1.60M
            m_suboptimal_chunks.emplace_back(chunk_idx);
1246
            // Randomize the initial order of suboptimal chunks in the queue.
1247
1.60M
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1248
1.60M
            if (j != m_suboptimal_chunks.size() - 1) {
1249
1.32M
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1250
1.32M
            }
1251
1.60M
        }
1252
98.7k
        unsigned chunks = m_chunk_idxs.Count();
1253
98.7k
        unsigned steps = 0;
1254
2.41M
        while (!m_suboptimal_chunks.empty()) {
1255
2.31M
            ++steps;
1256
            // Pop an entry from the potentially-suboptimal chunk queue.
1257
2.31M
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1258
2.31M
            m_suboptimal_chunks.pop_front();
1259
2.31M
            Assume(m_suboptimal_idxs[chunk_idx]);
1260
2.31M
            m_suboptimal_idxs.Reset(chunk_idx);
1261
            // If what was popped is not currently a chunk, continue. This may
1262
            // happen when it was merged with something else since being added.
1263
2.31M
            if (!m_chunk_idxs[chunk_idx]) continue;
1264
            /** What direction(s) to attempt merging in. 1=up, 2=down, 3=both. */
1265
1.90M
            unsigned direction = merged_chunks[chunk_idx] ? 3 : init_dir + 1;
1266
1.90M
            int flip = m_rng.randbool();
1267
4.20M
            for (int i = 0; i < 2; ++i) {
1268
3.26M
                if (i ^ flip) {
1269
1.64M
                    if (!(direction & 1)) continue;
1270
                    // Attempt to merge the chunk upwards.
1271
1.06M
                    auto result_up = MergeStep<false>(chunk_idx);
1272
1.06M
                    if (result_up != INVALID_SET_IDX) {
1273
504k
                        if (!m_suboptimal_idxs[result_up]) {
1274
504k
                            m_suboptimal_idxs.Set(result_up);
1275
504k
                            m_suboptimal_chunks.push_back(result_up);
1276
504k
                        }
1277
504k
                        merged_chunks.Set(result_up);
1278
504k
                        break;
1279
504k
                    }
1280
1.62M
                } else {
1281
1.62M
                    if (!(direction & 2)) continue;
1282
                    // Attempt to merge the chunk downwards.
1283
1.08M
                    auto result_down = MergeStep<true>(chunk_idx);
1284
1.08M
                    if (result_down != INVALID_SET_IDX) {
1285
457k
                        if (!m_suboptimal_idxs[result_down]) {
1286
207k
                            m_suboptimal_idxs.Set(result_down);
1287
207k
                            m_suboptimal_chunks.push_back(result_down);
1288
207k
                        }
1289
457k
                        merged_chunks.Set(result_down);
1290
457k
                        break;
1291
457k
                    }
1292
1.08M
                }
1293
3.26M
            }
1294
1.90M
        }
1295
98.7k
        m_cost.MakeTopologicalEnd(/*num_chunks=*/chunks, /*num_steps=*/steps);
1296
98.7k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MakeTopological()
Line
Count
Source
1230
27.9k
    {
1231
27.9k
        m_cost.MakeTopologicalBegin();
1232
27.9k
        Assume(m_suboptimal_chunks.empty());
1233
        /** What direction to initially merge chunks in; one of the two directions is enough. This
1234
         *  is sufficient because if a non-topological inactive dependency exists between two
1235
         *  chunks, at least one of the two chunks will eventually be processed in a direction that
1236
         *  discovers it - either the lower chunk tries upward, or the upper chunk tries downward.
1237
         *  Chunks that are the result of the merging are always tried in both directions. */
1238
27.9k
        unsigned init_dir = m_rng.randbool();
1239
        /** Which chunks are the result of merging, and thus need merge attempts in both
1240
         *  directions. */
1241
27.9k
        SetType merged_chunks;
1242
        // Mark chunks as suboptimal.
1243
27.9k
        m_suboptimal_idxs = m_chunk_idxs;
1244
455k
        for (auto chunk_idx : m_chunk_idxs) {
1245
455k
            m_suboptimal_chunks.emplace_back(chunk_idx);
1246
            // Randomize the initial order of suboptimal chunks in the queue.
1247
455k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1248
455k
            if (j != m_suboptimal_chunks.size() - 1) {
1249
381k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1250
381k
            }
1251
455k
        }
1252
27.9k
        unsigned chunks = m_chunk_idxs.Count();
1253
27.9k
        unsigned steps = 0;
1254
681k
        while (!m_suboptimal_chunks.empty()) {
1255
653k
            ++steps;
1256
            // Pop an entry from the potentially-suboptimal chunk queue.
1257
653k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1258
653k
            m_suboptimal_chunks.pop_front();
1259
653k
            Assume(m_suboptimal_idxs[chunk_idx]);
1260
653k
            m_suboptimal_idxs.Reset(chunk_idx);
1261
            // If what was popped is not currently a chunk, continue. This may
1262
            // happen when it was merged with something else since being added.
1263
653k
            if (!m_chunk_idxs[chunk_idx]) continue;
1264
            /** What direction(s) to attempt merging in. 1=up, 2=down, 3=both. */
1265
538k
            unsigned direction = merged_chunks[chunk_idx] ? 3 : init_dir + 1;
1266
538k
            int flip = m_rng.randbool();
1267
1.20M
            for (int i = 0; i < 2; ++i) {
1268
928k
                if (i ^ flip) {
1269
467k
                    if (!(direction & 1)) continue;
1270
                    // Attempt to merge the chunk upwards.
1271
299k
                    auto result_up = MergeStep<false>(chunk_idx);
1272
299k
                    if (result_up != INVALID_SET_IDX) {
1273
139k
                        if (!m_suboptimal_idxs[result_up]) {
1274
139k
                            m_suboptimal_idxs.Set(result_up);
1275
139k
                            m_suboptimal_chunks.push_back(result_up);
1276
139k
                        }
1277
139k
                        merged_chunks.Set(result_up);
1278
139k
                        break;
1279
139k
                    }
1280
461k
                } else {
1281
461k
                    if (!(direction & 2)) continue;
1282
                    // Attempt to merge the chunk downwards.
1283
308k
                    auto result_down = MergeStep<true>(chunk_idx);
1284
308k
                    if (result_down != INVALID_SET_IDX) {
1285
127k
                        if (!m_suboptimal_idxs[result_down]) {
1286
57.8k
                            m_suboptimal_idxs.Set(result_down);
1287
57.8k
                            m_suboptimal_chunks.push_back(result_down);
1288
57.8k
                        }
1289
127k
                        merged_chunks.Set(result_down);
1290
127k
                        break;
1291
127k
                    }
1292
308k
                }
1293
928k
            }
1294
538k
        }
1295
27.9k
        m_cost.MakeTopologicalEnd(/*num_chunks=*/chunks, /*num_steps=*/steps);
1296
27.9k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MakeTopological()
Line
Count
Source
1230
22.8k
    {
1231
22.8k
        m_cost.MakeTopologicalBegin();
1232
22.8k
        Assume(m_suboptimal_chunks.empty());
1233
        /** What direction to initially merge chunks in; one of the two directions is enough. This
1234
         *  is sufficient because if a non-topological inactive dependency exists between two
1235
         *  chunks, at least one of the two chunks will eventually be processed in a direction that
1236
         *  discovers it - either the lower chunk tries upward, or the upper chunk tries downward.
1237
         *  Chunks that are the result of the merging are always tried in both directions. */
1238
22.8k
        unsigned init_dir = m_rng.randbool();
1239
        /** Which chunks are the result of merging, and thus need merge attempts in both
1240
         *  directions. */
1241
22.8k
        SetType merged_chunks;
1242
        // Mark chunks as suboptimal.
1243
22.8k
        m_suboptimal_idxs = m_chunk_idxs;
1244
442k
        for (auto chunk_idx : m_chunk_idxs) {
1245
442k
            m_suboptimal_chunks.emplace_back(chunk_idx);
1246
            // Randomize the initial order of suboptimal chunks in the queue.
1247
442k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1248
442k
            if (j != m_suboptimal_chunks.size() - 1) {
1249
374k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1250
374k
            }
1251
442k
        }
1252
22.8k
        unsigned chunks = m_chunk_idxs.Count();
1253
22.8k
        unsigned steps = 0;
1254
663k
        while (!m_suboptimal_chunks.empty()) {
1255
641k
            ++steps;
1256
            // Pop an entry from the potentially-suboptimal chunk queue.
1257
641k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1258
641k
            m_suboptimal_chunks.pop_front();
1259
641k
            Assume(m_suboptimal_idxs[chunk_idx]);
1260
641k
            m_suboptimal_idxs.Reset(chunk_idx);
1261
            // If what was popped is not currently a chunk, continue. This may
1262
            // happen when it was merged with something else since being added.
1263
641k
            if (!m_chunk_idxs[chunk_idx]) continue;
1264
            /** What direction(s) to attempt merging in. 1=up, 2=down, 3=both. */
1265
526k
            unsigned direction = merged_chunks[chunk_idx] ? 3 : init_dir + 1;
1266
526k
            int flip = m_rng.randbool();
1267
1.16M
            for (int i = 0; i < 2; ++i) {
1268
905k
                if (i ^ flip) {
1269
456k
                    if (!(direction & 1)) continue;
1270
                    // Attempt to merge the chunk upwards.
1271
296k
                    auto result_up = MergeStep<false>(chunk_idx);
1272
296k
                    if (result_up != INVALID_SET_IDX) {
1273
140k
                        if (!m_suboptimal_idxs[result_up]) {
1274
140k
                            m_suboptimal_idxs.Set(result_up);
1275
140k
                            m_suboptimal_chunks.push_back(result_up);
1276
140k
                        }
1277
140k
                        merged_chunks.Set(result_up);
1278
140k
                        break;
1279
140k
                    }
1280
449k
                } else {
1281
449k
                    if (!(direction & 2)) continue;
1282
                    // Attempt to merge the chunk downwards.
1283
300k
                    auto result_down = MergeStep<true>(chunk_idx);
1284
300k
                    if (result_down != INVALID_SET_IDX) {
1285
125k
                        if (!m_suboptimal_idxs[result_down]) {
1286
57.9k
                            m_suboptimal_idxs.Set(result_down);
1287
57.9k
                            m_suboptimal_chunks.push_back(result_down);
1288
57.9k
                        }
1289
125k
                        merged_chunks.Set(result_down);
1290
125k
                        break;
1291
125k
                    }
1292
300k
                }
1293
905k
            }
1294
526k
        }
1295
22.8k
        m_cost.MakeTopologicalEnd(/*num_chunks=*/chunks, /*num_steps=*/steps);
1296
22.8k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MakeTopological()
Line
Count
Source
1230
22.8k
    {
1231
22.8k
        m_cost.MakeTopologicalBegin();
1232
22.8k
        Assume(m_suboptimal_chunks.empty());
1233
        /** What direction to initially merge chunks in; one of the two directions is enough. This
1234
         *  is sufficient because if a non-topological inactive dependency exists between two
1235
         *  chunks, at least one of the two chunks will eventually be processed in a direction that
1236
         *  discovers it - either the lower chunk tries upward, or the upper chunk tries downward.
1237
         *  Chunks that are the result of the merging are always tried in both directions. */
1238
22.8k
        unsigned init_dir = m_rng.randbool();
1239
        /** Which chunks are the result of merging, and thus need merge attempts in both
1240
         *  directions. */
1241
22.8k
        SetType merged_chunks;
1242
        // Mark chunks as suboptimal.
1243
22.8k
        m_suboptimal_idxs = m_chunk_idxs;
1244
439k
        for (auto chunk_idx : m_chunk_idxs) {
1245
439k
            m_suboptimal_chunks.emplace_back(chunk_idx);
1246
            // Randomize the initial order of suboptimal chunks in the queue.
1247
439k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1248
439k
            if (j != m_suboptimal_chunks.size() - 1) {
1249
371k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1250
371k
            }
1251
439k
        }
1252
22.8k
        unsigned chunks = m_chunk_idxs.Count();
1253
22.8k
        unsigned steps = 0;
1254
657k
        while (!m_suboptimal_chunks.empty()) {
1255
634k
            ++steps;
1256
            // Pop an entry from the potentially-suboptimal chunk queue.
1257
634k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1258
634k
            m_suboptimal_chunks.pop_front();
1259
634k
            Assume(m_suboptimal_idxs[chunk_idx]);
1260
634k
            m_suboptimal_idxs.Reset(chunk_idx);
1261
            // If what was popped is not currently a chunk, continue. This may
1262
            // happen when it was merged with something else since being added.
1263
634k
            if (!m_chunk_idxs[chunk_idx]) continue;
1264
            /** What direction(s) to attempt merging in. 1=up, 2=down, 3=both. */
1265
522k
            unsigned direction = merged_chunks[chunk_idx] ? 3 : init_dir + 1;
1266
522k
            int flip = m_rng.randbool();
1267
1.15M
            for (int i = 0; i < 2; ++i) {
1268
898k
                if (i ^ flip) {
1269
452k
                    if (!(direction & 1)) continue;
1270
                    // Attempt to merge the chunk upwards.
1271
290k
                    auto result_up = MergeStep<false>(chunk_idx);
1272
290k
                    if (result_up != INVALID_SET_IDX) {
1273
136k
                        if (!m_suboptimal_idxs[result_up]) {
1274
136k
                            m_suboptimal_idxs.Set(result_up);
1275
136k
                            m_suboptimal_chunks.push_back(result_up);
1276
136k
                        }
1277
136k
                        merged_chunks.Set(result_up);
1278
136k
                        break;
1279
136k
                    }
1280
446k
                } else {
1281
446k
                    if (!(direction & 2)) continue;
1282
                    // Attempt to merge the chunk downwards.
1283
300k
                    auto result_down = MergeStep<true>(chunk_idx);
1284
300k
                    if (result_down != INVALID_SET_IDX) {
1285
125k
                        if (!m_suboptimal_idxs[result_down]) {
1286
58.0k
                            m_suboptimal_idxs.Set(result_down);
1287
58.0k
                            m_suboptimal_chunks.push_back(result_down);
1288
58.0k
                        }
1289
125k
                        merged_chunks.Set(result_down);
1290
125k
                        break;
1291
125k
                    }
1292
300k
                }
1293
898k
            }
1294
522k
        }
1295
22.8k
        m_cost.MakeTopologicalEnd(/*num_chunks=*/chunks, /*num_steps=*/steps);
1296
22.8k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MakeTopological()
Line
Count
Source
1230
12.6k
    {
1231
12.6k
        m_cost.MakeTopologicalBegin();
1232
12.6k
        Assume(m_suboptimal_chunks.empty());
1233
        /** What direction to initially merge chunks in; one of the two directions is enough. This
1234
         *  is sufficient because if a non-topological inactive dependency exists between two
1235
         *  chunks, at least one of the two chunks will eventually be processed in a direction that
1236
         *  discovers it - either the lower chunk tries upward, or the upper chunk tries downward.
1237
         *  Chunks that are the result of the merging are always tried in both directions. */
1238
12.6k
        unsigned init_dir = m_rng.randbool();
1239
        /** Which chunks are the result of merging, and thus need merge attempts in both
1240
         *  directions. */
1241
12.6k
        SetType merged_chunks;
1242
        // Mark chunks as suboptimal.
1243
12.6k
        m_suboptimal_idxs = m_chunk_idxs;
1244
132k
        for (auto chunk_idx : m_chunk_idxs) {
1245
132k
            m_suboptimal_chunks.emplace_back(chunk_idx);
1246
            // Randomize the initial order of suboptimal chunks in the queue.
1247
132k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1248
132k
            if (j != m_suboptimal_chunks.size() - 1) {
1249
101k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1250
101k
            }
1251
132k
        }
1252
12.6k
        unsigned chunks = m_chunk_idxs.Count();
1253
12.6k
        unsigned steps = 0;
1254
205k
        while (!m_suboptimal_chunks.empty()) {
1255
193k
            ++steps;
1256
            // Pop an entry from the potentially-suboptimal chunk queue.
1257
193k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1258
193k
            m_suboptimal_chunks.pop_front();
1259
193k
            Assume(m_suboptimal_idxs[chunk_idx]);
1260
193k
            m_suboptimal_idxs.Reset(chunk_idx);
1261
            // If what was popped is not currently a chunk, continue. This may
1262
            // happen when it was merged with something else since being added.
1263
193k
            if (!m_chunk_idxs[chunk_idx]) continue;
1264
            /** What direction(s) to attempt merging in. 1=up, 2=down, 3=both. */
1265
157k
            unsigned direction = merged_chunks[chunk_idx] ? 3 : init_dir + 1;
1266
157k
            int flip = m_rng.randbool();
1267
341k
            for (int i = 0; i < 2; ++i) {
1268
267k
                if (i ^ flip) {
1269
134k
                    if (!(direction & 1)) continue;
1270
                    // Attempt to merge the chunk upwards.
1271
88.6k
                    auto result_up = MergeStep<false>(chunk_idx);
1272
88.6k
                    if (result_up != INVALID_SET_IDX) {
1273
43.7k
                        if (!m_suboptimal_idxs[result_up]) {
1274
43.7k
                            m_suboptimal_idxs.Set(result_up);
1275
43.7k
                            m_suboptimal_chunks.push_back(result_up);
1276
43.7k
                        }
1277
43.7k
                        merged_chunks.Set(result_up);
1278
43.7k
                        break;
1279
43.7k
                    }
1280
132k
                } else {
1281
132k
                    if (!(direction & 2)) continue;
1282
                    // Attempt to merge the chunk downwards.
1283
89.8k
                    auto result_down = MergeStep<true>(chunk_idx);
1284
89.8k
                    if (result_down != INVALID_SET_IDX) {
1285
39.4k
                        if (!m_suboptimal_idxs[result_down]) {
1286
16.7k
                            m_suboptimal_idxs.Set(result_down);
1287
16.7k
                            m_suboptimal_chunks.push_back(result_down);
1288
16.7k
                        }
1289
39.4k
                        merged_chunks.Set(result_down);
1290
39.4k
                        break;
1291
39.4k
                    }
1292
89.8k
                }
1293
267k
            }
1294
157k
        }
1295
12.6k
        m_cost.MakeTopologicalEnd(/*num_chunks=*/chunks, /*num_steps=*/steps);
1296
12.6k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MakeTopological()
Line
Count
Source
1230
12.5k
    {
1231
12.5k
        m_cost.MakeTopologicalBegin();
1232
12.5k
        Assume(m_suboptimal_chunks.empty());
1233
        /** What direction to initially merge chunks in; one of the two directions is enough. This
1234
         *  is sufficient because if a non-topological inactive dependency exists between two
1235
         *  chunks, at least one of the two chunks will eventually be processed in a direction that
1236
         *  discovers it - either the lower chunk tries upward, or the upper chunk tries downward.
1237
         *  Chunks that are the result of the merging are always tried in both directions. */
1238
12.5k
        unsigned init_dir = m_rng.randbool();
1239
        /** Which chunks are the result of merging, and thus need merge attempts in both
1240
         *  directions. */
1241
12.5k
        SetType merged_chunks;
1242
        // Mark chunks as suboptimal.
1243
12.5k
        m_suboptimal_idxs = m_chunk_idxs;
1244
132k
        for (auto chunk_idx : m_chunk_idxs) {
1245
132k
            m_suboptimal_chunks.emplace_back(chunk_idx);
1246
            // Randomize the initial order of suboptimal chunks in the queue.
1247
132k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1248
132k
            if (j != m_suboptimal_chunks.size() - 1) {
1249
100k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1250
100k
            }
1251
132k
        }
1252
12.5k
        unsigned chunks = m_chunk_idxs.Count();
1253
12.5k
        unsigned steps = 0;
1254
205k
        while (!m_suboptimal_chunks.empty()) {
1255
192k
            ++steps;
1256
            // Pop an entry from the potentially-suboptimal chunk queue.
1257
192k
            SetIdx chunk_idx = m_suboptimal_chunks.front();
1258
192k
            m_suboptimal_chunks.pop_front();
1259
192k
            Assume(m_suboptimal_idxs[chunk_idx]);
1260
192k
            m_suboptimal_idxs.Reset(chunk_idx);
1261
            // If what was popped is not currently a chunk, continue. This may
1262
            // happen when it was merged with something else since being added.
1263
192k
            if (!m_chunk_idxs[chunk_idx]) continue;
1264
            /** What direction(s) to attempt merging in. 1=up, 2=down, 3=both. */
1265
156k
            unsigned direction = merged_chunks[chunk_idx] ? 3 : init_dir + 1;
1266
156k
            int flip = m_rng.randbool();
1267
341k
            for (int i = 0; i < 2; ++i) {
1268
267k
                if (i ^ flip) {
1269
134k
                    if (!(direction & 1)) continue;
1270
                    // Attempt to merge the chunk upwards.
1271
88.8k
                    auto result_up = MergeStep<false>(chunk_idx);
1272
88.8k
                    if (result_up != INVALID_SET_IDX) {
1273
43.6k
                        if (!m_suboptimal_idxs[result_up]) {
1274
43.6k
                            m_suboptimal_idxs.Set(result_up);
1275
43.6k
                            m_suboptimal_chunks.push_back(result_up);
1276
43.6k
                        }
1277
43.6k
                        merged_chunks.Set(result_up);
1278
43.6k
                        break;
1279
43.6k
                    }
1280
132k
                } else {
1281
132k
                    if (!(direction & 2)) continue;
1282
                    // Attempt to merge the chunk downwards.
1283
89.0k
                    auto result_down = MergeStep<true>(chunk_idx);
1284
89.0k
                    if (result_down != INVALID_SET_IDX) {
1285
38.4k
                        if (!m_suboptimal_idxs[result_down]) {
1286
16.5k
                            m_suboptimal_idxs.Set(result_down);
1287
16.5k
                            m_suboptimal_chunks.push_back(result_down);
1288
16.5k
                        }
1289
38.4k
                        merged_chunks.Set(result_down);
1290
38.4k
                        break;
1291
38.4k
                    }
1292
89.0k
                }
1293
267k
            }
1294
156k
        }
1295
12.5k
        m_cost.MakeTopologicalEnd(/*num_chunks=*/chunks, /*num_steps=*/steps);
1296
12.5k
    }
1297
1298
    /** Initialize the data structure for optimization. It must be topological already. */
1299
    void StartOptimizing() noexcept
1300
191k
    {
1301
191k
        m_cost.StartOptimizingBegin();
1302
191k
        Assume(m_suboptimal_chunks.empty());
1303
        // Mark chunks suboptimal.
1304
191k
        m_suboptimal_idxs = m_chunk_idxs;
1305
1.33M
        for (auto chunk_idx : m_chunk_idxs) {
1306
1.33M
            m_suboptimal_chunks.push_back(chunk_idx);
1307
            // Randomize the initial order of suboptimal chunks in the queue.
1308
1.33M
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1309
1.33M
            if (j != m_suboptimal_chunks.size() - 1) {
1310
950k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1311
950k
            }
1312
1.33M
        }
1313
191k
        m_cost.StartOptimizingEnd(/*num_chunks=*/m_suboptimal_chunks.size());
1314
191k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::StartOptimizing()
Line
Count
Source
1300
50.5k
    {
1301
50.5k
        m_cost.StartOptimizingBegin();
1302
50.5k
        Assume(m_suboptimal_chunks.empty());
1303
        // Mark chunks suboptimal.
1304
50.5k
        m_suboptimal_idxs = m_chunk_idxs;
1305
385k
        for (auto chunk_idx : m_chunk_idxs) {
1306
385k
            m_suboptimal_chunks.push_back(chunk_idx);
1307
            // Randomize the initial order of suboptimal chunks in the queue.
1308
385k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1309
385k
            if (j != m_suboptimal_chunks.size() - 1) {
1310
280k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1311
280k
            }
1312
385k
        }
1313
50.5k
        m_cost.StartOptimizingEnd(/*num_chunks=*/m_suboptimal_chunks.size());
1314
50.5k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::StartOptimizing()
Line
Count
Source
1300
45.4k
    {
1301
45.4k
        m_cost.StartOptimizingBegin();
1302
45.4k
        Assume(m_suboptimal_chunks.empty());
1303
        // Mark chunks suboptimal.
1304
45.4k
        m_suboptimal_idxs = m_chunk_idxs;
1305
373k
        for (auto chunk_idx : m_chunk_idxs) {
1306
373k
            m_suboptimal_chunks.push_back(chunk_idx);
1307
            // Randomize the initial order of suboptimal chunks in the queue.
1308
373k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1309
373k
            if (j != m_suboptimal_chunks.size() - 1) {
1310
274k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1311
274k
            }
1312
373k
        }
1313
45.4k
        m_cost.StartOptimizingEnd(/*num_chunks=*/m_suboptimal_chunks.size());
1314
45.4k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::StartOptimizing()
Line
Count
Source
1300
45.4k
    {
1301
45.4k
        m_cost.StartOptimizingBegin();
1302
45.4k
        Assume(m_suboptimal_chunks.empty());
1303
        // Mark chunks suboptimal.
1304
45.4k
        m_suboptimal_idxs = m_chunk_idxs;
1305
374k
        for (auto chunk_idx : m_chunk_idxs) {
1306
374k
            m_suboptimal_chunks.push_back(chunk_idx);
1307
            // Randomize the initial order of suboptimal chunks in the queue.
1308
374k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1309
374k
            if (j != m_suboptimal_chunks.size() - 1) {
1310
276k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1311
276k
            }
1312
374k
        }
1313
45.4k
        m_cost.StartOptimizingEnd(/*num_chunks=*/m_suboptimal_chunks.size());
1314
45.4k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::StartOptimizing()
Line
Count
Source
1300
25.0k
    {
1301
25.0k
        m_cost.StartOptimizingBegin();
1302
25.0k
        Assume(m_suboptimal_chunks.empty());
1303
        // Mark chunks suboptimal.
1304
25.0k
        m_suboptimal_idxs = m_chunk_idxs;
1305
100k
        for (auto chunk_idx : m_chunk_idxs) {
1306
100k
            m_suboptimal_chunks.push_back(chunk_idx);
1307
            // Randomize the initial order of suboptimal chunks in the queue.
1308
100k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1309
100k
            if (j != m_suboptimal_chunks.size() - 1) {
1310
59.4k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1311
59.4k
            }
1312
100k
        }
1313
25.0k
        m_cost.StartOptimizingEnd(/*num_chunks=*/m_suboptimal_chunks.size());
1314
25.0k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::StartOptimizing()
Line
Count
Source
1300
25.0k
    {
1301
25.0k
        m_cost.StartOptimizingBegin();
1302
25.0k
        Assume(m_suboptimal_chunks.empty());
1303
        // Mark chunks suboptimal.
1304
25.0k
        m_suboptimal_idxs = m_chunk_idxs;
1305
100k
        for (auto chunk_idx : m_chunk_idxs) {
1306
100k
            m_suboptimal_chunks.push_back(chunk_idx);
1307
            // Randomize the initial order of suboptimal chunks in the queue.
1308
100k
            SetIdx j = m_rng.randrange<SetIdx>(m_suboptimal_chunks.size());
1309
100k
            if (j != m_suboptimal_chunks.size() - 1) {
1310
59.3k
                std::swap(m_suboptimal_chunks.back(), m_suboptimal_chunks[j]);
1311
59.3k
            }
1312
100k
        }
1313
25.0k
        m_cost.StartOptimizingEnd(/*num_chunks=*/m_suboptimal_chunks.size());
1314
25.0k
    }
1315
1316
    /** Try to improve the forest. Returns false if it is optimal, true otherwise. */
1317
    bool OptimizeStep() noexcept
1318
2.55M
    {
1319
2.55M
        auto chunk_idx = PickChunkToOptimize();
1320
2.55M
        if (chunk_idx == INVALID_SET_IDX) {
1321
            // No improvable chunk was found, we are done.
1322
0
            return false;
1323
0
        }
1324
2.55M
        auto [parent_idx, child_idx] = PickDependencyToSplit(chunk_idx);
1325
2.55M
        if (parent_idx == TxIdx(-1)) {
1326
            // Nothing to improve in chunk_idx. Need to continue with other chunks, if any.
1327
1.53M
            return !m_suboptimal_chunks.empty();
1328
1.53M
        }
1329
        // Deactivate the found dependency and then make the state topological again with a
1330
        // sequence of merges.
1331
1.02M
        Improve(parent_idx, child_idx);
1332
1.02M
        return true;
1333
2.55M
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::OptimizeStep()
Line
Count
Source
1318
744k
    {
1319
744k
        auto chunk_idx = PickChunkToOptimize();
1320
744k
        if (chunk_idx == INVALID_SET_IDX) {
1321
            // No improvable chunk was found, we are done.
1322
0
            return false;
1323
0
        }
1324
744k
        auto [parent_idx, child_idx] = PickDependencyToSplit(chunk_idx);
1325
744k
        if (parent_idx == TxIdx(-1)) {
1326
            // Nothing to improve in chunk_idx. Need to continue with other chunks, if any.
1327
447k
            return !m_suboptimal_chunks.empty();
1328
447k
        }
1329
        // Deactivate the found dependency and then make the state topological again with a
1330
        // sequence of merges.
1331
297k
        Improve(parent_idx, child_idx);
1332
297k
        return true;
1333
744k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::OptimizeStep()
Line
Count
Source
1318
735k
    {
1319
735k
        auto chunk_idx = PickChunkToOptimize();
1320
735k
        if (chunk_idx == INVALID_SET_IDX) {
1321
            // No improvable chunk was found, we are done.
1322
0
            return false;
1323
0
        }
1324
735k
        auto [parent_idx, child_idx] = PickDependencyToSplit(chunk_idx);
1325
735k
        if (parent_idx == TxIdx(-1)) {
1326
            // Nothing to improve in chunk_idx. Need to continue with other chunks, if any.
1327
436k
            return !m_suboptimal_chunks.empty();
1328
436k
        }
1329
        // Deactivate the found dependency and then make the state topological again with a
1330
        // sequence of merges.
1331
299k
        Improve(parent_idx, child_idx);
1332
299k
        return true;
1333
735k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::OptimizeStep()
Line
Count
Source
1318
733k
    {
1319
733k
        auto chunk_idx = PickChunkToOptimize();
1320
733k
        if (chunk_idx == INVALID_SET_IDX) {
1321
            // No improvable chunk was found, we are done.
1322
0
            return false;
1323
0
        }
1324
733k
        auto [parent_idx, child_idx] = PickDependencyToSplit(chunk_idx);
1325
733k
        if (parent_idx == TxIdx(-1)) {
1326
            // Nothing to improve in chunk_idx. Need to continue with other chunks, if any.
1327
436k
            return !m_suboptimal_chunks.empty();
1328
436k
        }
1329
        // Deactivate the found dependency and then make the state topological again with a
1330
        // sequence of merges.
1331
297k
        Improve(parent_idx, child_idx);
1332
297k
        return true;
1333
733k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::OptimizeStep()
Line
Count
Source
1318
172k
    {
1319
172k
        auto chunk_idx = PickChunkToOptimize();
1320
172k
        if (chunk_idx == INVALID_SET_IDX) {
1321
            // No improvable chunk was found, we are done.
1322
0
            return false;
1323
0
        }
1324
172k
        auto [parent_idx, child_idx] = PickDependencyToSplit(chunk_idx);
1325
172k
        if (parent_idx == TxIdx(-1)) {
1326
            // Nothing to improve in chunk_idx. Need to continue with other chunks, if any.
1327
107k
            return !m_suboptimal_chunks.empty();
1328
107k
        }
1329
        // Deactivate the found dependency and then make the state topological again with a
1330
        // sequence of merges.
1331
64.3k
        Improve(parent_idx, child_idx);
1332
64.3k
        return true;
1333
172k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::OptimizeStep()
Line
Count
Source
1318
172k
    {
1319
172k
        auto chunk_idx = PickChunkToOptimize();
1320
172k
        if (chunk_idx == INVALID_SET_IDX) {
1321
            // No improvable chunk was found, we are done.
1322
0
            return false;
1323
0
        }
1324
172k
        auto [parent_idx, child_idx] = PickDependencyToSplit(chunk_idx);
1325
172k
        if (parent_idx == TxIdx(-1)) {
1326
            // Nothing to improve in chunk_idx. Need to continue with other chunks, if any.
1327
108k
            return !m_suboptimal_chunks.empty();
1328
108k
        }
1329
        // Deactivate the found dependency and then make the state topological again with a
1330
        // sequence of merges.
1331
64.0k
        Improve(parent_idx, child_idx);
1332
64.0k
        return true;
1333
172k
    }
1334
1335
    /** Initialize data structure for minimizing the chunks. Can only be called if state is known
1336
     *  to be optimal. OptimizeStep() cannot be called anymore afterwards. */
1337
    void StartMinimizing() noexcept
1338
191k
    {
1339
191k
        m_cost.StartMinimizingBegin();
1340
191k
        m_nonminimal_chunks.clear();
1341
191k
        m_nonminimal_chunks.reserve(m_transaction_idxs.Count());
1342
        // Gather all chunks, and for each, add it with a random pivot in it, and a random initial
1343
        // direction, to m_nonminimal_chunks.
1344
1.50M
        for (auto chunk_idx : m_chunk_idxs) {
1345
1.50M
            TxIdx pivot_idx = PickRandomTx(m_set_info[chunk_idx].transactions);
1346
1.50M
            m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, m_rng.randbits<1>());
1347
            // Randomize the initial order of nonminimal chunks in the queue.
1348
1.50M
            SetIdx j = m_rng.randrange<SetIdx>(m_nonminimal_chunks.size());
1349
1.50M
            if (j != m_nonminimal_chunks.size() - 1) {
1350
1.10M
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[j]);
1351
1.10M
            }
1352
1.50M
        }
1353
191k
        m_cost.StartMinimizingEnd(/*num_chunks=*/m_nonminimal_chunks.size());
1354
191k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::StartMinimizing()
Line
Count
Source
1338
50.5k
    {
1339
50.5k
        m_cost.StartMinimizingBegin();
1340
50.5k
        m_nonminimal_chunks.clear();
1341
50.5k
        m_nonminimal_chunks.reserve(m_transaction_idxs.Count());
1342
        // Gather all chunks, and for each, add it with a random pivot in it, and a random initial
1343
        // direction, to m_nonminimal_chunks.
1344
437k
        for (auto chunk_idx : m_chunk_idxs) {
1345
437k
            TxIdx pivot_idx = PickRandomTx(m_set_info[chunk_idx].transactions);
1346
437k
            m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, m_rng.randbits<1>());
1347
            // Randomize the initial order of nonminimal chunks in the queue.
1348
437k
            SetIdx j = m_rng.randrange<SetIdx>(m_nonminimal_chunks.size());
1349
437k
            if (j != m_nonminimal_chunks.size() - 1) {
1350
328k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[j]);
1351
328k
            }
1352
437k
        }
1353
50.5k
        m_cost.StartMinimizingEnd(/*num_chunks=*/m_nonminimal_chunks.size());
1354
50.5k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::StartMinimizing()
Line
Count
Source
1338
45.4k
    {
1339
45.4k
        m_cost.StartMinimizingBegin();
1340
45.4k
        m_nonminimal_chunks.clear();
1341
45.4k
        m_nonminimal_chunks.reserve(m_transaction_idxs.Count());
1342
        // Gather all chunks, and for each, add it with a random pivot in it, and a random initial
1343
        // direction, to m_nonminimal_chunks.
1344
427k
        for (auto chunk_idx : m_chunk_idxs) {
1345
427k
            TxIdx pivot_idx = PickRandomTx(m_set_info[chunk_idx].transactions);
1346
427k
            m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, m_rng.randbits<1>());
1347
            // Randomize the initial order of nonminimal chunks in the queue.
1348
427k
            SetIdx j = m_rng.randrange<SetIdx>(m_nonminimal_chunks.size());
1349
427k
            if (j != m_nonminimal_chunks.size() - 1) {
1350
323k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[j]);
1351
323k
            }
1352
427k
        }
1353
45.4k
        m_cost.StartMinimizingEnd(/*num_chunks=*/m_nonminimal_chunks.size());
1354
45.4k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::StartMinimizing()
Line
Count
Source
1338
45.4k
    {
1339
45.4k
        m_cost.StartMinimizingBegin();
1340
45.4k
        m_nonminimal_chunks.clear();
1341
45.4k
        m_nonminimal_chunks.reserve(m_transaction_idxs.Count());
1342
        // Gather all chunks, and for each, add it with a random pivot in it, and a random initial
1343
        // direction, to m_nonminimal_chunks.
1344
427k
        for (auto chunk_idx : m_chunk_idxs) {
1345
427k
            TxIdx pivot_idx = PickRandomTx(m_set_info[chunk_idx].transactions);
1346
427k
            m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, m_rng.randbits<1>());
1347
            // Randomize the initial order of nonminimal chunks in the queue.
1348
427k
            SetIdx j = m_rng.randrange<SetIdx>(m_nonminimal_chunks.size());
1349
427k
            if (j != m_nonminimal_chunks.size() - 1) {
1350
323k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[j]);
1351
323k
            }
1352
427k
        }
1353
45.4k
        m_cost.StartMinimizingEnd(/*num_chunks=*/m_nonminimal_chunks.size());
1354
45.4k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::StartMinimizing()
Line
Count
Source
1338
25.0k
    {
1339
25.0k
        m_cost.StartMinimizingBegin();
1340
25.0k
        m_nonminimal_chunks.clear();
1341
25.0k
        m_nonminimal_chunks.reserve(m_transaction_idxs.Count());
1342
        // Gather all chunks, and for each, add it with a random pivot in it, and a random initial
1343
        // direction, to m_nonminimal_chunks.
1344
106k
        for (auto chunk_idx : m_chunk_idxs) {
1345
106k
            TxIdx pivot_idx = PickRandomTx(m_set_info[chunk_idx].transactions);
1346
106k
            m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, m_rng.randbits<1>());
1347
            // Randomize the initial order of nonminimal chunks in the queue.
1348
106k
            SetIdx j = m_rng.randrange<SetIdx>(m_nonminimal_chunks.size());
1349
106k
            if (j != m_nonminimal_chunks.size() - 1) {
1350
64.4k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[j]);
1351
64.4k
            }
1352
106k
        }
1353
25.0k
        m_cost.StartMinimizingEnd(/*num_chunks=*/m_nonminimal_chunks.size());
1354
25.0k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::StartMinimizing()
Line
Count
Source
1338
25.0k
    {
1339
25.0k
        m_cost.StartMinimizingBegin();
1340
25.0k
        m_nonminimal_chunks.clear();
1341
25.0k
        m_nonminimal_chunks.reserve(m_transaction_idxs.Count());
1342
        // Gather all chunks, and for each, add it with a random pivot in it, and a random initial
1343
        // direction, to m_nonminimal_chunks.
1344
106k
        for (auto chunk_idx : m_chunk_idxs) {
1345
106k
            TxIdx pivot_idx = PickRandomTx(m_set_info[chunk_idx].transactions);
1346
106k
            m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, m_rng.randbits<1>());
1347
            // Randomize the initial order of nonminimal chunks in the queue.
1348
106k
            SetIdx j = m_rng.randrange<SetIdx>(m_nonminimal_chunks.size());
1349
106k
            if (j != m_nonminimal_chunks.size() - 1) {
1350
64.4k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[j]);
1351
64.4k
            }
1352
106k
        }
1353
25.0k
        m_cost.StartMinimizingEnd(/*num_chunks=*/m_nonminimal_chunks.size());
1354
25.0k
    }
1355
1356
    /** Try to reduce a chunk's size. Returns false if all chunks are minimal, true otherwise. */
1357
    bool MinimizeStep() noexcept
1358
2.32M
    {
1359
        // If the queue of potentially-non-minimal chunks is empty, we are done.
1360
2.32M
        if (m_nonminimal_chunks.empty()) return false;
1361
2.12M
        m_cost.MinimizeStepBegin();
1362
        // Pop an entry from the potentially-non-minimal chunk queue.
1363
2.12M
        auto [chunk_idx, pivot_idx, flags] = m_nonminimal_chunks.front();
1364
2.12M
        m_nonminimal_chunks.pop_front();
1365
2.12M
        auto& chunk_info = m_set_info[chunk_idx];
1366
        /** Whether to move the pivot down rather than up. */
1367
2.12M
        bool move_pivot_down = flags & 1;
1368
        /** Whether this is already the second stage. */
1369
2.12M
        bool second_stage = flags & 2;
1370
1371
        // Find a random dependency whose top and bottom set feerates are equal, and which has
1372
        // pivot in bottom set (if move_pivot_down) or in top set (if !move_pivot_down).
1373
2.12M
        std::pair<TxIdx, TxIdx> candidate_dep;
1374
2.12M
        uint64_t candidate_tiebreak{0};
1375
2.12M
        bool have_any = false;
1376
        // Iterate over all transactions.
1377
8.46M
        for (auto tx_idx : chunk_info.transactions) {
1378
8.46M
            const auto& tx_data = m_tx_data[tx_idx];
1379
            // Iterate over all active child dependencies of the transaction.
1380
8.46M
            for (auto child_idx : tx_data.active_children) {
1381
6.33M
                const auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1382
                // Skip if this dependency does not have equal top and bottom set feerates. Note
1383
                // that the top cannot have higher feerate than the bottom, or OptimizeSteps would
1384
                // have dealt with it.
1385
6.33M
                if (ByRatio{dep_top_info.feerate} < ByRatio{chunk_info.feerate}) continue;
1386
3.05M
                have_any = true;
1387
                // Skip if this dependency does not have pivot in the right place.
1388
3.05M
                if (move_pivot_down == dep_top_info.transactions[pivot_idx]) continue;
1389
                // Remember this as our chosen dependency if it has a better tiebreak.
1390
2.40M
                uint64_t tiebreak = m_rng.rand64() | 1;
1391
2.40M
                if (tiebreak > candidate_tiebreak) {
1392
621k
                    candidate_tiebreak = tiebreak;
1393
621k
                    candidate_dep = {tx_idx, child_idx};
1394
621k
                }
1395
2.40M
            }
1396
8.46M
        }
1397
2.12M
        m_cost.MinimizeStepMid(/*num_txns=*/chunk_info.transactions.Count());
1398
        // If no dependencies have equal top and bottom set feerate, this chunk is minimal.
1399
2.12M
        if (!have_any) return true;
1400
        // If all found dependencies have the pivot in the wrong place, try moving it in the other
1401
        // direction. If this was the second stage already, we are done.
1402
337k
        if (candidate_tiebreak == 0) {
1403
            // Switch to other direction, and to second phase.
1404
51.1k
            flags ^= 3;
1405
51.1k
            if (!second_stage) m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, flags);
1406
51.1k
            return true;
1407
51.1k
        }
1408
1409
        // Otherwise, deactivate the dependency that was found.
1410
286k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(candidate_dep.first, candidate_dep.second);
1411
        // Determine if there is a dependency from the new bottom to the new top (opposite from the
1412
        // dependency that was just deactivated).
1413
286k
        auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1414
286k
        auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1415
286k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1416
            // A self-merge is needed. Note that the child_chunk_idx is the top, and
1417
            // parent_chunk_idx is the bottom, because we activate a dependency in the reverse
1418
            // direction compared to the deactivation above.
1419
674
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1420
            // Re-insert the chunk into the queue, in the same direction. Note that the chunk_idx
1421
            // will have changed.
1422
674
            m_nonminimal_chunks.emplace_back(merged_chunk_idx, pivot_idx, flags);
1423
674
            m_cost.MinimizeStepEnd(/*split=*/false);
1424
286k
        } else {
1425
            // No self-merge happens, and thus we have found a way to split the chunk. Create two
1426
            // smaller chunks, and add them to the queue. The one that contains the current pivot
1427
            // gets to continue with it in the same direction, to minimize the number of times we
1428
            // alternate direction. If we were in the second phase already, the newly created chunk
1429
            // inherits that too, because we know no split with the pivot on the other side is
1430
            // possible already. The new chunk without the current pivot gets a new randomly-chosen
1431
            // one.
1432
286k
            if (move_pivot_down) {
1433
80.6k
                auto parent_pivot_idx = PickRandomTx(m_set_info[parent_chunk_idx].transactions);
1434
80.6k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, parent_pivot_idx, m_rng.randbits<1>());
1435
80.6k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, pivot_idx, flags);
1436
205k
            } else {
1437
205k
                auto child_pivot_idx = PickRandomTx(m_set_info[child_chunk_idx].transactions);
1438
205k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, pivot_idx, flags);
1439
205k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, child_pivot_idx, m_rng.randbits<1>());
1440
205k
            }
1441
286k
            if (m_rng.randbool()) {
1442
142k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[m_nonminimal_chunks.size() - 2]);
1443
142k
            }
1444
286k
            m_cost.MinimizeStepEnd(/*split=*/true);
1445
286k
        }
1446
286k
        return true;
1447
337k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::MinimizeStep()
Line
Count
Source
1358
727k
    {
1359
        // If the queue of potentially-non-minimal chunks is empty, we are done.
1360
727k
        if (m_nonminimal_chunks.empty()) return false;
1361
676k
        m_cost.MinimizeStepBegin();
1362
        // Pop an entry from the potentially-non-minimal chunk queue.
1363
676k
        auto [chunk_idx, pivot_idx, flags] = m_nonminimal_chunks.front();
1364
676k
        m_nonminimal_chunks.pop_front();
1365
676k
        auto& chunk_info = m_set_info[chunk_idx];
1366
        /** Whether to move the pivot down rather than up. */
1367
676k
        bool move_pivot_down = flags & 1;
1368
        /** Whether this is already the second stage. */
1369
676k
        bool second_stage = flags & 2;
1370
1371
        // Find a random dependency whose top and bottom set feerates are equal, and which has
1372
        // pivot in bottom set (if move_pivot_down) or in top set (if !move_pivot_down).
1373
676k
        std::pair<TxIdx, TxIdx> candidate_dep;
1374
676k
        uint64_t candidate_tiebreak{0};
1375
676k
        bool have_any = false;
1376
        // Iterate over all transactions.
1377
2.68M
        for (auto tx_idx : chunk_info.transactions) {
1378
2.68M
            const auto& tx_data = m_tx_data[tx_idx];
1379
            // Iterate over all active child dependencies of the transaction.
1380
2.68M
            for (auto child_idx : tx_data.active_children) {
1381
2.00M
                const auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1382
                // Skip if this dependency does not have equal top and bottom set feerates. Note
1383
                // that the top cannot have higher feerate than the bottom, or OptimizeSteps would
1384
                // have dealt with it.
1385
2.00M
                if (ByRatio{dep_top_info.feerate} < ByRatio{chunk_info.feerate}) continue;
1386
1.09M
                have_any = true;
1387
                // Skip if this dependency does not have pivot in the right place.
1388
1.09M
                if (move_pivot_down == dep_top_info.transactions[pivot_idx]) continue;
1389
                // Remember this as our chosen dependency if it has a better tiebreak.
1390
825k
                uint64_t tiebreak = m_rng.rand64() | 1;
1391
825k
                if (tiebreak > candidate_tiebreak) {
1392
220k
                    candidate_tiebreak = tiebreak;
1393
220k
                    candidate_dep = {tx_idx, child_idx};
1394
220k
                }
1395
825k
            }
1396
2.68M
        }
1397
676k
        m_cost.MinimizeStepMid(/*num_txns=*/chunk_info.transactions.Count());
1398
        // If no dependencies have equal top and bottom set feerate, this chunk is minimal.
1399
676k
        if (!have_any) return true;
1400
        // If all found dependencies have the pivot in the wrong place, try moving it in the other
1401
        // direction. If this was the second stage already, we are done.
1402
130k
        if (candidate_tiebreak == 0) {
1403
            // Switch to other direction, and to second phase.
1404
23.0k
            flags ^= 3;
1405
23.0k
            if (!second_stage) m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, flags);
1406
23.0k
            return true;
1407
23.0k
        }
1408
1409
        // Otherwise, deactivate the dependency that was found.
1410
107k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(candidate_dep.first, candidate_dep.second);
1411
        // Determine if there is a dependency from the new bottom to the new top (opposite from the
1412
        // dependency that was just deactivated).
1413
107k
        auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1414
107k
        auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1415
107k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1416
            // A self-merge is needed. Note that the child_chunk_idx is the top, and
1417
            // parent_chunk_idx is the bottom, because we activate a dependency in the reverse
1418
            // direction compared to the deactivation above.
1419
218
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1420
            // Re-insert the chunk into the queue, in the same direction. Note that the chunk_idx
1421
            // will have changed.
1422
218
            m_nonminimal_chunks.emplace_back(merged_chunk_idx, pivot_idx, flags);
1423
218
            m_cost.MinimizeStepEnd(/*split=*/false);
1424
107k
        } else {
1425
            // No self-merge happens, and thus we have found a way to split the chunk. Create two
1426
            // smaller chunks, and add them to the queue. The one that contains the current pivot
1427
            // gets to continue with it in the same direction, to minimize the number of times we
1428
            // alternate direction. If we were in the second phase already, the newly created chunk
1429
            // inherits that too, because we know no split with the pivot on the other side is
1430
            // possible already. The new chunk without the current pivot gets a new randomly-chosen
1431
            // one.
1432
107k
            if (move_pivot_down) {
1433
36.7k
                auto parent_pivot_idx = PickRandomTx(m_set_info[parent_chunk_idx].transactions);
1434
36.7k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, parent_pivot_idx, m_rng.randbits<1>());
1435
36.7k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, pivot_idx, flags);
1436
71.0k
            } else {
1437
71.0k
                auto child_pivot_idx = PickRandomTx(m_set_info[child_chunk_idx].transactions);
1438
71.0k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, pivot_idx, flags);
1439
71.0k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, child_pivot_idx, m_rng.randbits<1>());
1440
71.0k
            }
1441
107k
            if (m_rng.randbool()) {
1442
53.6k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[m_nonminimal_chunks.size() - 2]);
1443
53.6k
            }
1444
107k
            m_cost.MinimizeStepEnd(/*split=*/true);
1445
107k
        }
1446
107k
        return true;
1447
130k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::MinimizeStep()
Line
Count
Source
1358
596k
    {
1359
        // If the queue of potentially-non-minimal chunks is empty, we are done.
1360
596k
        if (m_nonminimal_chunks.empty()) return false;
1361
550k
        m_cost.MinimizeStepBegin();
1362
        // Pop an entry from the potentially-non-minimal chunk queue.
1363
550k
        auto [chunk_idx, pivot_idx, flags] = m_nonminimal_chunks.front();
1364
550k
        m_nonminimal_chunks.pop_front();
1365
550k
        auto& chunk_info = m_set_info[chunk_idx];
1366
        /** Whether to move the pivot down rather than up. */
1367
550k
        bool move_pivot_down = flags & 1;
1368
        /** Whether this is already the second stage. */
1369
550k
        bool second_stage = flags & 2;
1370
1371
        // Find a random dependency whose top and bottom set feerates are equal, and which has
1372
        // pivot in bottom set (if move_pivot_down) or in top set (if !move_pivot_down).
1373
550k
        std::pair<TxIdx, TxIdx> candidate_dep;
1374
550k
        uint64_t candidate_tiebreak{0};
1375
550k
        bool have_any = false;
1376
        // Iterate over all transactions.
1377
2.17M
        for (auto tx_idx : chunk_info.transactions) {
1378
2.17M
            const auto& tx_data = m_tx_data[tx_idx];
1379
            // Iterate over all active child dependencies of the transaction.
1380
2.17M
            for (auto child_idx : tx_data.active_children) {
1381
1.62M
                const auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1382
                // Skip if this dependency does not have equal top and bottom set feerates. Note
1383
                // that the top cannot have higher feerate than the bottom, or OptimizeSteps would
1384
                // have dealt with it.
1385
1.62M
                if (ByRatio{dep_top_info.feerate} < ByRatio{chunk_info.feerate}) continue;
1386
718k
                have_any = true;
1387
                // Skip if this dependency does not have pivot in the right place.
1388
718k
                if (move_pivot_down == dep_top_info.transactions[pivot_idx]) continue;
1389
                // Remember this as our chosen dependency if it has a better tiebreak.
1390
601k
                uint64_t tiebreak = m_rng.rand64() | 1;
1391
601k
                if (tiebreak > candidate_tiebreak) {
1392
135k
                    candidate_tiebreak = tiebreak;
1393
135k
                    candidate_dep = {tx_idx, child_idx};
1394
135k
                }
1395
601k
            }
1396
2.17M
        }
1397
550k
        m_cost.MinimizeStepMid(/*num_txns=*/chunk_info.transactions.Count());
1398
        // If no dependencies have equal top and bottom set feerate, this chunk is minimal.
1399
550k
        if (!have_any) return true;
1400
        // If all found dependencies have the pivot in the wrong place, try moving it in the other
1401
        // direction. If this was the second stage already, we are done.
1402
66.3k
        if (candidate_tiebreak == 0) {
1403
            // Switch to other direction, and to second phase.
1404
8.70k
            flags ^= 3;
1405
8.70k
            if (!second_stage) m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, flags);
1406
8.70k
            return true;
1407
8.70k
        }
1408
1409
        // Otherwise, deactivate the dependency that was found.
1410
57.6k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(candidate_dep.first, candidate_dep.second);
1411
        // Determine if there is a dependency from the new bottom to the new top (opposite from the
1412
        // dependency that was just deactivated).
1413
57.6k
        auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1414
57.6k
        auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1415
57.6k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1416
            // A self-merge is needed. Note that the child_chunk_idx is the top, and
1417
            // parent_chunk_idx is the bottom, because we activate a dependency in the reverse
1418
            // direction compared to the deactivation above.
1419
229
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1420
            // Re-insert the chunk into the queue, in the same direction. Note that the chunk_idx
1421
            // will have changed.
1422
229
            m_nonminimal_chunks.emplace_back(merged_chunk_idx, pivot_idx, flags);
1423
229
            m_cost.MinimizeStepEnd(/*split=*/false);
1424
57.4k
        } else {
1425
            // No self-merge happens, and thus we have found a way to split the chunk. Create two
1426
            // smaller chunks, and add them to the queue. The one that contains the current pivot
1427
            // gets to continue with it in the same direction, to minimize the number of times we
1428
            // alternate direction. If we were in the second phase already, the newly created chunk
1429
            // inherits that too, because we know no split with the pivot on the other side is
1430
            // possible already. The new chunk without the current pivot gets a new randomly-chosen
1431
            // one.
1432
57.4k
            if (move_pivot_down) {
1433
13.1k
                auto parent_pivot_idx = PickRandomTx(m_set_info[parent_chunk_idx].transactions);
1434
13.1k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, parent_pivot_idx, m_rng.randbits<1>());
1435
13.1k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, pivot_idx, flags);
1436
44.2k
            } else {
1437
44.2k
                auto child_pivot_idx = PickRandomTx(m_set_info[child_chunk_idx].transactions);
1438
44.2k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, pivot_idx, flags);
1439
44.2k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, child_pivot_idx, m_rng.randbits<1>());
1440
44.2k
            }
1441
57.4k
            if (m_rng.randbool()) {
1442
28.6k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[m_nonminimal_chunks.size() - 2]);
1443
28.6k
            }
1444
57.4k
            m_cost.MinimizeStepEnd(/*split=*/true);
1445
57.4k
        }
1446
57.6k
        return true;
1447
66.3k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::MinimizeStep()
Line
Count
Source
1358
596k
    {
1359
        // If the queue of potentially-non-minimal chunks is empty, we are done.
1360
596k
        if (m_nonminimal_chunks.empty()) return false;
1361
550k
        m_cost.MinimizeStepBegin();
1362
        // Pop an entry from the potentially-non-minimal chunk queue.
1363
550k
        auto [chunk_idx, pivot_idx, flags] = m_nonminimal_chunks.front();
1364
550k
        m_nonminimal_chunks.pop_front();
1365
550k
        auto& chunk_info = m_set_info[chunk_idx];
1366
        /** Whether to move the pivot down rather than up. */
1367
550k
        bool move_pivot_down = flags & 1;
1368
        /** Whether this is already the second stage. */
1369
550k
        bool second_stage = flags & 2;
1370
1371
        // Find a random dependency whose top and bottom set feerates are equal, and which has
1372
        // pivot in bottom set (if move_pivot_down) or in top set (if !move_pivot_down).
1373
550k
        std::pair<TxIdx, TxIdx> candidate_dep;
1374
550k
        uint64_t candidate_tiebreak{0};
1375
550k
        bool have_any = false;
1376
        // Iterate over all transactions.
1377
2.17M
        for (auto tx_idx : chunk_info.transactions) {
1378
2.17M
            const auto& tx_data = m_tx_data[tx_idx];
1379
            // Iterate over all active child dependencies of the transaction.
1380
2.17M
            for (auto child_idx : tx_data.active_children) {
1381
1.62M
                const auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1382
                // Skip if this dependency does not have equal top and bottom set feerates. Note
1383
                // that the top cannot have higher feerate than the bottom, or OptimizeSteps would
1384
                // have dealt with it.
1385
1.62M
                if (ByRatio{dep_top_info.feerate} < ByRatio{chunk_info.feerate}) continue;
1386
718k
                have_any = true;
1387
                // Skip if this dependency does not have pivot in the right place.
1388
718k
                if (move_pivot_down == dep_top_info.transactions[pivot_idx]) continue;
1389
                // Remember this as our chosen dependency if it has a better tiebreak.
1390
598k
                uint64_t tiebreak = m_rng.rand64() | 1;
1391
598k
                if (tiebreak > candidate_tiebreak) {
1392
135k
                    candidate_tiebreak = tiebreak;
1393
135k
                    candidate_dep = {tx_idx, child_idx};
1394
135k
                }
1395
598k
            }
1396
2.17M
        }
1397
550k
        m_cost.MinimizeStepMid(/*num_txns=*/chunk_info.transactions.Count());
1398
        // If no dependencies have equal top and bottom set feerate, this chunk is minimal.
1399
550k
        if (!have_any) return true;
1400
        // If all found dependencies have the pivot in the wrong place, try moving it in the other
1401
        // direction. If this was the second stage already, we are done.
1402
66.3k
        if (candidate_tiebreak == 0) {
1403
            // Switch to other direction, and to second phase.
1404
8.75k
            flags ^= 3;
1405
8.75k
            if (!second_stage) m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, flags);
1406
8.75k
            return true;
1407
8.75k
        }
1408
1409
        // Otherwise, deactivate the dependency that was found.
1410
57.6k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(candidate_dep.first, candidate_dep.second);
1411
        // Determine if there is a dependency from the new bottom to the new top (opposite from the
1412
        // dependency that was just deactivated).
1413
57.6k
        auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1414
57.6k
        auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1415
57.6k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1416
            // A self-merge is needed. Note that the child_chunk_idx is the top, and
1417
            // parent_chunk_idx is the bottom, because we activate a dependency in the reverse
1418
            // direction compared to the deactivation above.
1419
227
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1420
            // Re-insert the chunk into the queue, in the same direction. Note that the chunk_idx
1421
            // will have changed.
1422
227
            m_nonminimal_chunks.emplace_back(merged_chunk_idx, pivot_idx, flags);
1423
227
            m_cost.MinimizeStepEnd(/*split=*/false);
1424
57.4k
        } else {
1425
            // No self-merge happens, and thus we have found a way to split the chunk. Create two
1426
            // smaller chunks, and add them to the queue. The one that contains the current pivot
1427
            // gets to continue with it in the same direction, to minimize the number of times we
1428
            // alternate direction. If we were in the second phase already, the newly created chunk
1429
            // inherits that too, because we know no split with the pivot on the other side is
1430
            // possible already. The new chunk without the current pivot gets a new randomly-chosen
1431
            // one.
1432
57.4k
            if (move_pivot_down) {
1433
13.3k
                auto parent_pivot_idx = PickRandomTx(m_set_info[parent_chunk_idx].transactions);
1434
13.3k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, parent_pivot_idx, m_rng.randbits<1>());
1435
13.3k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, pivot_idx, flags);
1436
44.0k
            } else {
1437
44.0k
                auto child_pivot_idx = PickRandomTx(m_set_info[child_chunk_idx].transactions);
1438
44.0k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, pivot_idx, flags);
1439
44.0k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, child_pivot_idx, m_rng.randbits<1>());
1440
44.0k
            }
1441
57.4k
            if (m_rng.randbool()) {
1442
28.5k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[m_nonminimal_chunks.size() - 2]);
1443
28.5k
            }
1444
57.4k
            m_cost.MinimizeStepEnd(/*split=*/true);
1445
57.4k
        }
1446
57.6k
        return true;
1447
66.3k
    }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::MinimizeStep()
Line
Count
Source
1358
200k
    {
1359
        // If the queue of potentially-non-minimal chunks is empty, we are done.
1360
200k
        if (m_nonminimal_chunks.empty()) return false;
1361
175k
        m_cost.MinimizeStepBegin();
1362
        // Pop an entry from the potentially-non-minimal chunk queue.
1363
175k
        auto [chunk_idx, pivot_idx, flags] = m_nonminimal_chunks.front();
1364
175k
        m_nonminimal_chunks.pop_front();
1365
175k
        auto& chunk_info = m_set_info[chunk_idx];
1366
        /** Whether to move the pivot down rather than up. */
1367
175k
        bool move_pivot_down = flags & 1;
1368
        /** Whether this is already the second stage. */
1369
175k
        bool second_stage = flags & 2;
1370
1371
        // Find a random dependency whose top and bottom set feerates are equal, and which has
1372
        // pivot in bottom set (if move_pivot_down) or in top set (if !move_pivot_down).
1373
175k
        std::pair<TxIdx, TxIdx> candidate_dep;
1374
175k
        uint64_t candidate_tiebreak{0};
1375
175k
        bool have_any = false;
1376
        // Iterate over all transactions.
1377
719k
        for (auto tx_idx : chunk_info.transactions) {
1378
719k
            const auto& tx_data = m_tx_data[tx_idx];
1379
            // Iterate over all active child dependencies of the transaction.
1380
719k
            for (auto child_idx : tx_data.active_children) {
1381
543k
                const auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1382
                // Skip if this dependency does not have equal top and bottom set feerates. Note
1383
                // that the top cannot have higher feerate than the bottom, or OptimizeSteps would
1384
                // have dealt with it.
1385
543k
                if (ByRatio{dep_top_info.feerate} < ByRatio{chunk_info.feerate}) continue;
1386
261k
                have_any = true;
1387
                // Skip if this dependency does not have pivot in the right place.
1388
261k
                if (move_pivot_down == dep_top_info.transactions[pivot_idx]) continue;
1389
                // Remember this as our chosen dependency if it has a better tiebreak.
1390
188k
                uint64_t tiebreak = m_rng.rand64() | 1;
1391
188k
                if (tiebreak > candidate_tiebreak) {
1392
64.5k
                    candidate_tiebreak = tiebreak;
1393
64.5k
                    candidate_dep = {tx_idx, child_idx};
1394
64.5k
                }
1395
188k
            }
1396
719k
        }
1397
175k
        m_cost.MinimizeStepMid(/*num_txns=*/chunk_info.transactions.Count());
1398
        // If no dependencies have equal top and bottom set feerate, this chunk is minimal.
1399
175k
        if (!have_any) return true;
1400
        // If all found dependencies have the pivot in the wrong place, try moving it in the other
1401
        // direction. If this was the second stage already, we are done.
1402
37.0k
        if (candidate_tiebreak == 0) {
1403
            // Switch to other direction, and to second phase.
1404
5.29k
            flags ^= 3;
1405
5.29k
            if (!second_stage) m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, flags);
1406
5.29k
            return true;
1407
5.29k
        }
1408
1409
        // Otherwise, deactivate the dependency that was found.
1410
31.8k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(candidate_dep.first, candidate_dep.second);
1411
        // Determine if there is a dependency from the new bottom to the new top (opposite from the
1412
        // dependency that was just deactivated).
1413
31.8k
        auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1414
31.8k
        auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1415
31.8k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1416
            // A self-merge is needed. Note that the child_chunk_idx is the top, and
1417
            // parent_chunk_idx is the bottom, because we activate a dependency in the reverse
1418
            // direction compared to the deactivation above.
1419
0
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1420
            // Re-insert the chunk into the queue, in the same direction. Note that the chunk_idx
1421
            // will have changed.
1422
0
            m_nonminimal_chunks.emplace_back(merged_chunk_idx, pivot_idx, flags);
1423
0
            m_cost.MinimizeStepEnd(/*split=*/false);
1424
31.8k
        } else {
1425
            // No self-merge happens, and thus we have found a way to split the chunk. Create two
1426
            // smaller chunks, and add them to the queue. The one that contains the current pivot
1427
            // gets to continue with it in the same direction, to minimize the number of times we
1428
            // alternate direction. If we were in the second phase already, the newly created chunk
1429
            // inherits that too, because we know no split with the pivot on the other side is
1430
            // possible already. The new chunk without the current pivot gets a new randomly-chosen
1431
            // one.
1432
31.8k
            if (move_pivot_down) {
1433
8.68k
                auto parent_pivot_idx = PickRandomTx(m_set_info[parent_chunk_idx].transactions);
1434
8.68k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, parent_pivot_idx, m_rng.randbits<1>());
1435
8.68k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, pivot_idx, flags);
1436
23.1k
            } else {
1437
23.1k
                auto child_pivot_idx = PickRandomTx(m_set_info[child_chunk_idx].transactions);
1438
23.1k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, pivot_idx, flags);
1439
23.1k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, child_pivot_idx, m_rng.randbits<1>());
1440
23.1k
            }
1441
31.8k
            if (m_rng.randbool()) {
1442
15.7k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[m_nonminimal_chunks.size() - 2]);
1443
15.7k
            }
1444
31.8k
            m_cost.MinimizeStepEnd(/*split=*/true);
1445
31.8k
        }
1446
31.8k
        return true;
1447
37.0k
    }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::MinimizeStep()
Line
Count
Source
1358
200k
    {
1359
        // If the queue of potentially-non-minimal chunks is empty, we are done.
1360
200k
        if (m_nonminimal_chunks.empty()) return false;
1361
175k
        m_cost.MinimizeStepBegin();
1362
        // Pop an entry from the potentially-non-minimal chunk queue.
1363
175k
        auto [chunk_idx, pivot_idx, flags] = m_nonminimal_chunks.front();
1364
175k
        m_nonminimal_chunks.pop_front();
1365
175k
        auto& chunk_info = m_set_info[chunk_idx];
1366
        /** Whether to move the pivot down rather than up. */
1367
175k
        bool move_pivot_down = flags & 1;
1368
        /** Whether this is already the second stage. */
1369
175k
        bool second_stage = flags & 2;
1370
1371
        // Find a random dependency whose top and bottom set feerates are equal, and which has
1372
        // pivot in bottom set (if move_pivot_down) or in top set (if !move_pivot_down).
1373
175k
        std::pair<TxIdx, TxIdx> candidate_dep;
1374
175k
        uint64_t candidate_tiebreak{0};
1375
175k
        bool have_any = false;
1376
        // Iterate over all transactions.
1377
717k
        for (auto tx_idx : chunk_info.transactions) {
1378
717k
            const auto& tx_data = m_tx_data[tx_idx];
1379
            // Iterate over all active child dependencies of the transaction.
1380
717k
            for (auto child_idx : tx_data.active_children) {
1381
542k
                const auto& dep_top_info = m_set_info[tx_data.dep_top_idx[child_idx]];
1382
                // Skip if this dependency does not have equal top and bottom set feerates. Note
1383
                // that the top cannot have higher feerate than the bottom, or OptimizeSteps would
1384
                // have dealt with it.
1385
542k
                if (ByRatio{dep_top_info.feerate} < ByRatio{chunk_info.feerate}) continue;
1386
259k
                have_any = true;
1387
                // Skip if this dependency does not have pivot in the right place.
1388
259k
                if (move_pivot_down == dep_top_info.transactions[pivot_idx]) continue;
1389
                // Remember this as our chosen dependency if it has a better tiebreak.
1390
187k
                uint64_t tiebreak = m_rng.rand64() | 1;
1391
187k
                if (tiebreak > candidate_tiebreak) {
1392
64.6k
                    candidate_tiebreak = tiebreak;
1393
64.6k
                    candidate_dep = {tx_idx, child_idx};
1394
64.6k
                }
1395
187k
            }
1396
717k
        }
1397
175k
        m_cost.MinimizeStepMid(/*num_txns=*/chunk_info.transactions.Count());
1398
        // If no dependencies have equal top and bottom set feerate, this chunk is minimal.
1399
175k
        if (!have_any) return true;
1400
        // If all found dependencies have the pivot in the wrong place, try moving it in the other
1401
        // direction. If this was the second stage already, we are done.
1402
37.1k
        if (candidate_tiebreak == 0) {
1403
            // Switch to other direction, and to second phase.
1404
5.35k
            flags ^= 3;
1405
5.35k
            if (!second_stage) m_nonminimal_chunks.emplace_back(chunk_idx, pivot_idx, flags);
1406
5.35k
            return true;
1407
5.35k
        }
1408
1409
        // Otherwise, deactivate the dependency that was found.
1410
31.8k
        auto [parent_chunk_idx, child_chunk_idx] = Deactivate(candidate_dep.first, candidate_dep.second);
1411
        // Determine if there is a dependency from the new bottom to the new top (opposite from the
1412
        // dependency that was just deactivated).
1413
31.8k
        auto& parent_reachable = m_reachable[parent_chunk_idx].first;
1414
31.8k
        auto& child_chunk_txn = m_set_info[child_chunk_idx].transactions;
1415
31.8k
        if (parent_reachable.Overlaps(child_chunk_txn)) {
1416
            // A self-merge is needed. Note that the child_chunk_idx is the top, and
1417
            // parent_chunk_idx is the bottom, because we activate a dependency in the reverse
1418
            // direction compared to the deactivation above.
1419
0
            auto merged_chunk_idx = MergeChunks(child_chunk_idx, parent_chunk_idx);
1420
            // Re-insert the chunk into the queue, in the same direction. Note that the chunk_idx
1421
            // will have changed.
1422
0
            m_nonminimal_chunks.emplace_back(merged_chunk_idx, pivot_idx, flags);
1423
0
            m_cost.MinimizeStepEnd(/*split=*/false);
1424
31.8k
        } else {
1425
            // No self-merge happens, and thus we have found a way to split the chunk. Create two
1426
            // smaller chunks, and add them to the queue. The one that contains the current pivot
1427
            // gets to continue with it in the same direction, to minimize the number of times we
1428
            // alternate direction. If we were in the second phase already, the newly created chunk
1429
            // inherits that too, because we know no split with the pivot on the other side is
1430
            // possible already. The new chunk without the current pivot gets a new randomly-chosen
1431
            // one.
1432
31.8k
            if (move_pivot_down) {
1433
8.72k
                auto parent_pivot_idx = PickRandomTx(m_set_info[parent_chunk_idx].transactions);
1434
8.72k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, parent_pivot_idx, m_rng.randbits<1>());
1435
8.72k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, pivot_idx, flags);
1436
23.0k
            } else {
1437
23.0k
                auto child_pivot_idx = PickRandomTx(m_set_info[child_chunk_idx].transactions);
1438
23.0k
                m_nonminimal_chunks.emplace_back(parent_chunk_idx, pivot_idx, flags);
1439
23.0k
                m_nonminimal_chunks.emplace_back(child_chunk_idx, child_pivot_idx, m_rng.randbits<1>());
1440
23.0k
            }
1441
31.8k
            if (m_rng.randbool()) {
1442
15.9k
                std::swap(m_nonminimal_chunks.back(), m_nonminimal_chunks[m_nonminimal_chunks.size() - 2]);
1443
15.9k
            }
1444
31.8k
            m_cost.MinimizeStepEnd(/*split=*/true);
1445
31.8k
        }
1446
31.8k
        return true;
1447
37.1k
    }
1448
1449
    /** Construct a topologically-valid linearization from the current forest state. Must be
1450
     *  topological. fallback_order is a comparator that defines a strong order for DepGraphIndexes
1451
     *  in this cluster, used to order equal-feerate transactions and chunks.
1452
     *
1453
     * Specifically, the resulting order consists of:
1454
     * - The chunks of the current SFL state, sorted by (in decreasing order of priority):
1455
     *   - topology (parents before children)
1456
     *   - highest chunk feerate first
1457
     *   - smallest chunk size first
1458
     *   - the chunk with the lowest maximum transaction, by fallback_order, first
1459
     * - The transactions within a chunk, sorted by (in decreasing order of priority):
1460
     *   - topology (parents before children)
1461
     *   - highest tx feerate first
1462
     *   - smallest tx size first
1463
     *   - the lowest transaction, by fallback_order, first
1464
     */
1465
    std::vector<DepGraphIndex> GetLinearization(const StrongComparator<DepGraphIndex> auto& fallback_order) noexcept
1466
191k
    {
1467
191k
        m_cost.GetLinearizationBegin();
1468
        /** The output linearization. */
1469
191k
        std::vector<DepGraphIndex> ret;
1470
191k
        ret.reserve(m_set_info.size());
1471
        /** A heap with all chunks (by set index) that can currently be included, sorted by
1472
         *  chunk feerate (high to low), chunk size (small to large), and by least maximum element
1473
         *  according to the fallback order (which is the second pair element). */
1474
191k
        std::vector<std::pair<SetIdx, TxIdx>> ready_chunks;
1475
        /** For every chunk, indexed by SetIdx, the number of unmet dependencies the chunk has on
1476
         *  other chunks (not including dependencies within the chunk itself). */
1477
191k
        std::vector<TxIdx> chunk_deps(m_set_info.size(), 0);
1478
        /** For every transaction, indexed by TxIdx, the number of unmet dependencies the
1479
         *  transaction has. */
1480
191k
        std::vector<TxIdx> tx_deps(m_tx_data.size(), 0);
1481
        /** A heap with all transactions within the current chunk that can be included, sorted by
1482
         *  tx feerate (high to low), tx size (small to large), and fallback order. */
1483
191k
        std::vector<TxIdx> ready_tx;
1484
        // Populate chunk_deps and tx_deps.
1485
191k
        unsigned num_deps{0};
1486
5.07M
        for (TxIdx chl_idx : m_transaction_idxs) {
1487
5.07M
            const auto& chl_data = m_tx_data[chl_idx];
1488
5.07M
            tx_deps[chl_idx] = chl_data.parents.Count();
1489
5.07M
            num_deps += tx_deps[chl_idx];
1490
5.07M
            auto chl_chunk_idx = chl_data.chunk_idx;
1491
5.07M
            auto& chl_chunk_info = m_set_info[chl_chunk_idx];
1492
5.07M
            chunk_deps[chl_chunk_idx] += (chl_data.parents - chl_chunk_info.transactions).Count();
1493
5.07M
        }
1494
        /** Function to compute the highest element of a chunk, by fallback_order. */
1495
1.79M
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
1.79M
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
1.79M
            auto it = chunk.begin();
1498
1.79M
            DepGraphIndex ret = *it;
1499
1.79M
            ++it;
1500
5.07M
            while (it != chunk.end()) {
1501
3.28M
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
3.28M
                ++it;
1503
3.28M
            }
1504
1.79M
            return ret;
1505
1.79M
        };
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(unsigned char)::operator()(unsigned char) const
Line
Count
Source
1495
484k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
484k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
484k
            auto it = chunk.begin();
1498
484k
            DepGraphIndex ret = *it;
1499
484k
            ++it;
1500
1.38M
            while (it != chunk.end()) {
1501
902k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
902k
                ++it;
1503
902k
            }
1504
484k
            return ret;
1505
484k
        };
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(unsigned char)::operator()(unsigned char) const
Line
Count
Source
1495
484k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
484k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
484k
            auto it = chunk.begin();
1498
484k
            DepGraphIndex ret = *it;
1499
484k
            ++it;
1500
1.38M
            while (it != chunk.end()) {
1501
902k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
902k
                ++it;
1503
902k
            }
1504
484k
            return ret;
1505
484k
        };
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(unsigned char)::operator()(unsigned char) const
Line
Count
Source
1495
484k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
484k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
484k
            auto it = chunk.begin();
1498
484k
            DepGraphIndex ret = *it;
1499
484k
            ++it;
1500
1.38M
            while (it != chunk.end()) {
1501
902k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
902k
                ++it;
1503
902k
            }
1504
484k
            return ret;
1505
484k
        };
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(unsigned char)::operator()(unsigned char) const
Line
Count
Source
1495
138k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
138k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
138k
            auto it = chunk.begin();
1498
138k
            DepGraphIndex ret = *it;
1499
138k
            ++it;
1500
421k
            while (it != chunk.end()) {
1501
282k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
282k
                ++it;
1503
282k
            }
1504
138k
            return ret;
1505
138k
        };
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(unsigned char)::operator()(unsigned char) const
Line
Count
Source
1495
138k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
138k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
138k
            auto it = chunk.begin();
1498
138k
            DepGraphIndex ret = *it;
1499
138k
            ++it;
1500
421k
            while (it != chunk.end()) {
1501
282k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
282k
                ++it;
1503
282k
            }
1504
138k
            return ret;
1505
138k
        };
txgraph.cpp:std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<(anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0>((anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&)::'lambda'(unsigned char)::operator()(unsigned char) const
Line
Count
Source
1495
61.2k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
61.2k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
61.2k
            auto it = chunk.begin();
1498
61.2k
            DepGraphIndex ret = *it;
1499
61.2k
            ++it;
1500
68.7k
            while (it != chunk.end()) {
1501
7.49k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
7.49k
                ++it;
1503
7.49k
            }
1504
61.2k
            return ret;
1505
61.2k
        };
1506
        /** Comparison function for the transaction heap. Note that it is a max-heap, so
1507
         *  tx_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1508
8.58M
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
8.58M
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
8.58M
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
8.58M
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
8.58M
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
8.58M
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
3.46M
            if (a_feerate.size != b_feerate.size) {
1518
3.40k
                return a_feerate.size > b_feerate.size;
1519
3.40k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
3.46M
            auto fallback_cmp = fallback_order(a, b);
1522
3.46M
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
0
            Assume(false);
1525
0
            return a < b;
1526
3.46M
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(std::compare_three_way const&, auto const&)::operator()<unsigned int, unsigned int>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1508
2.41M
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
2.41M
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
2.41M
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
2.41M
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
2.41M
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
2.41M
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
1.00M
            if (a_feerate.size != b_feerate.size) {
1518
1.00k
                return a_feerate.size > b_feerate.size;
1519
1.00k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
999k
            auto fallback_cmp = fallback_order(a, b);
1522
999k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
0
            Assume(false);
1525
0
            return a < b;
1526
999k
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(std::compare_three_way const&, auto const&)::operator()<unsigned int, unsigned int>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1508
2.41M
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
2.41M
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
2.41M
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
2.41M
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
2.41M
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
2.41M
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
1.00M
            if (a_feerate.size != b_feerate.size) {
1518
1.00k
                return a_feerate.size > b_feerate.size;
1519
1.00k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
999k
            auto fallback_cmp = fallback_order(a, b);
1522
999k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
0
            Assume(false);
1525
0
            return a < b;
1526
999k
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(std::compare_three_way const&, auto const&)::operator()<unsigned int, unsigned int>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1508
2.41M
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
2.41M
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
2.41M
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
2.41M
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
2.41M
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
2.41M
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
1.00M
            if (a_feerate.size != b_feerate.size) {
1518
1.00k
                return a_feerate.size > b_feerate.size;
1519
1.00k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
999k
            auto fallback_cmp = fallback_order(a, b);
1522
999k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
0
            Assume(false);
1525
0
            return a < b;
1526
999k
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(std::compare_three_way const&, auto const&)::operator()<unsigned int, unsigned int>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1508
663k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
663k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
663k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
663k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
663k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
663k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
221k
            if (a_feerate.size != b_feerate.size) {
1518
200
                return a_feerate.size > b_feerate.size;
1519
200
            }
1520
            // Tie-break by decreasing fallback_order.
1521
221k
            auto fallback_cmp = fallback_order(a, b);
1522
221k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
0
            Assume(false);
1525
0
            return a < b;
1526
221k
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda'(std::compare_three_way const&, auto const&)::operator()<unsigned int, unsigned int>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1508
663k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
663k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
663k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
663k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
663k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
663k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
221k
            if (a_feerate.size != b_feerate.size) {
1518
200
                return a_feerate.size > b_feerate.size;
1519
200
            }
1520
            // Tie-break by decreasing fallback_order.
1521
221k
            auto fallback_cmp = fallback_order(a, b);
1522
221k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
0
            Assume(false);
1525
0
            return a < b;
1526
221k
        };
txgraph.cpp:auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<(anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0>((anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&)::'lambda'((anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&, auto const&)::operator()<unsigned int, unsigned int>((anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&, auto const&) const
Line
Count
Source
1508
22.5k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
22.5k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
22.5k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
22.5k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
22.5k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
22.5k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
22.4k
            if (a_feerate.size != b_feerate.size) {
1518
0
                return a_feerate.size > b_feerate.size;
1519
0
            }
1520
            // Tie-break by decreasing fallback_order.
1521
22.4k
            auto fallback_cmp = fallback_order(a, b);
1522
22.4k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
0
            Assume(false);
1525
0
            return a < b;
1526
22.4k
        };
1527
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1528
        /** Comparison function for the chunk heap. Note that it is a max-heap, so
1529
         *  chunk_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1530
5.23M
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
5.23M
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
5.23M
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
5.23M
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
5.23M
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
5.23M
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
1.84M
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
69.0k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
69.0k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
1.77M
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
1.77M
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
0
            Assume(false);
1547
0
            return a.second < b.second;
1548
1.77M
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda0'(std::compare_three_way const&, auto const&)::operator()<std::pair<unsigned char, unsigned int>, std::pair<unsigned char, unsigned int>>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1530
1.53M
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
1.53M
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
1.53M
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
1.53M
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
1.53M
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
1.53M
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
479k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
15.0k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
15.0k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
464k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
464k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
0
            Assume(false);
1547
0
            return a.second < b.second;
1548
464k
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda0'(std::compare_three_way const&, auto const&)::operator()<std::pair<unsigned char, unsigned int>, std::pair<unsigned char, unsigned int>>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1530
1.53M
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
1.53M
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
1.53M
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
1.53M
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
1.53M
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
1.53M
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
479k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
15.0k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
15.0k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
464k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
464k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
0
            Assume(false);
1547
0
            return a.second < b.second;
1548
464k
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda0'(std::compare_three_way const&, auto const&)::operator()<std::pair<unsigned char, unsigned int>, std::pair<unsigned char, unsigned int>>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1530
1.53M
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
1.53M
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
1.53M
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
1.53M
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
1.53M
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
1.53M
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
479k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
15.0k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
15.0k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
464k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
464k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
0
            Assume(false);
1547
0
            return a.second < b.second;
1548
464k
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda0'(std::compare_three_way const&, auto const&)::operator()<std::pair<unsigned char, unsigned int>, std::pair<unsigned char, unsigned int>>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1530
291k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
291k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
291k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
291k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
291k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
291k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
180k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
12.0k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
12.0k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
168k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
168k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
0
            Assume(false);
1547
0
            return a.second < b.second;
1548
168k
        };
auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)::'lambda0'(std::compare_three_way const&, auto const&)::operator()<std::pair<unsigned char, unsigned int>, std::pair<unsigned char, unsigned int>>(std::compare_three_way const&, auto const&) const
Line
Count
Source
1530
291k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
291k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
291k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
291k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
291k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
291k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
180k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
12.0k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
12.0k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
168k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
168k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
0
            Assume(false);
1547
0
            return a.second < b.second;
1548
168k
        };
txgraph.cpp:auto std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<(anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0>((anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&)::'lambda0'((anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&, auto const&)::operator()<std::pair<unsigned char, unsigned int>, std::pair<unsigned char, unsigned int>>((anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&, auto const&) const
Line
Count
Source
1530
42.5k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
42.5k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
42.5k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
42.5k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
42.5k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
42.5k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
41.8k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
0
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
0
            }
1542
            // Tie-break by decreasing fallback_order.
1543
41.8k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
41.8k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
0
            Assume(false);
1547
0
            return a.second < b.second;
1548
41.8k
        };
1549
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1550
1.79M
        for (SetIdx chunk_idx : m_chunk_idxs) {
1551
1.79M
            if (chunk_deps[chunk_idx] == 0) {
1552
460k
                ready_chunks.emplace_back(chunk_idx, max_fallback_fn(chunk_idx));
1553
460k
            }
1554
1.79M
        }
1555
191k
        std::make_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1556
        // Pop chunks off the heap.
1557
1.98M
        while (!ready_chunks.empty()) {
1558
1.79M
            auto [chunk_idx, _rnd] = ready_chunks.front();
1559
1.79M
            std::pop_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1560
1.79M
            ready_chunks.pop_back();
1561
1.79M
            Assume(chunk_deps[chunk_idx] == 0);
1562
1.79M
            const auto& chunk_txn = m_set_info[chunk_idx].transactions;
1563
            // Build heap of all includable transactions in chunk.
1564
1.79M
            Assume(ready_tx.empty());
1565
5.07M
            for (TxIdx tx_idx : chunk_txn) {
1566
5.07M
                if (tx_deps[tx_idx] == 0) ready_tx.push_back(tx_idx);
1567
5.07M
            }
1568
1.79M
            Assume(!ready_tx.empty());
1569
1.79M
            std::make_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1570
            // Pick transactions from the ready heap, append them to linearization, and decrement
1571
            // dependency counts.
1572
6.86M
            while (!ready_tx.empty()) {
1573
                // Pop an element from the tx_ready heap.
1574
5.07M
                auto tx_idx = ready_tx.front();
1575
5.07M
                std::pop_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1576
5.07M
                ready_tx.pop_back();
1577
                // Append to linearization.
1578
5.07M
                ret.push_back(tx_idx);
1579
                // Decrement dependency counts.
1580
5.07M
                auto& tx_data = m_tx_data[tx_idx];
1581
15.0M
                for (TxIdx chl_idx : tx_data.children) {
1582
15.0M
                    auto& chl_data = m_tx_data[chl_idx];
1583
                    // Decrement tx dependency count.
1584
15.0M
                    Assume(tx_deps[chl_idx] > 0);
1585
15.0M
                    if (--tx_deps[chl_idx] == 0 && chunk_txn[chl_idx]) {
1586
                        // Child tx has no dependencies left, and is in this chunk. Add it to the tx heap.
1587
2.66M
                        ready_tx.push_back(chl_idx);
1588
2.66M
                        std::push_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1589
2.66M
                    }
1590
                    // Decrement chunk dependency count if this is out-of-chunk dependency.
1591
15.0M
                    if (chl_data.chunk_idx != chunk_idx) {
1592
8.14M
                        Assume(chunk_deps[chl_data.chunk_idx] > 0);
1593
8.14M
                        if (--chunk_deps[chl_data.chunk_idx] == 0) {
1594
                            // Child chunk has no dependencies left. Add it to the chunk heap.
1595
1.33M
                            ready_chunks.emplace_back(chl_data.chunk_idx, max_fallback_fn(chl_data.chunk_idx));
1596
1.33M
                            std::push_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1597
1.33M
                        }
1598
8.14M
                    }
1599
15.0M
                }
1600
5.07M
            }
1601
1.79M
        }
1602
191k
        Assume(ret.size() == m_set_info.size());
1603
191k
        m_cost.GetLinearizationEnd(/*num_txns=*/m_set_info.size(), /*num_deps=*/num_deps);
1604
191k
        return ret;
1605
191k
    }
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)
Line
Count
Source
1466
45.4k
    {
1467
45.4k
        m_cost.GetLinearizationBegin();
1468
        /** The output linearization. */
1469
45.4k
        std::vector<DepGraphIndex> ret;
1470
45.4k
        ret.reserve(m_set_info.size());
1471
        /** A heap with all chunks (by set index) that can currently be included, sorted by
1472
         *  chunk feerate (high to low), chunk size (small to large), and by least maximum element
1473
         *  according to the fallback order (which is the second pair element). */
1474
45.4k
        std::vector<std::pair<SetIdx, TxIdx>> ready_chunks;
1475
        /** For every chunk, indexed by SetIdx, the number of unmet dependencies the chunk has on
1476
         *  other chunks (not including dependencies within the chunk itself). */
1477
45.4k
        std::vector<TxIdx> chunk_deps(m_set_info.size(), 0);
1478
        /** For every transaction, indexed by TxIdx, the number of unmet dependencies the
1479
         *  transaction has. */
1480
45.4k
        std::vector<TxIdx> tx_deps(m_tx_data.size(), 0);
1481
        /** A heap with all transactions within the current chunk that can be included, sorted by
1482
         *  tx feerate (high to low), tx size (small to large), and fallback order. */
1483
45.4k
        std::vector<TxIdx> ready_tx;
1484
        // Populate chunk_deps and tx_deps.
1485
45.4k
        unsigned num_deps{0};
1486
1.38M
        for (TxIdx chl_idx : m_transaction_idxs) {
1487
1.38M
            const auto& chl_data = m_tx_data[chl_idx];
1488
1.38M
            tx_deps[chl_idx] = chl_data.parents.Count();
1489
1.38M
            num_deps += tx_deps[chl_idx];
1490
1.38M
            auto chl_chunk_idx = chl_data.chunk_idx;
1491
1.38M
            auto& chl_chunk_info = m_set_info[chl_chunk_idx];
1492
1.38M
            chunk_deps[chl_chunk_idx] += (chl_data.parents - chl_chunk_info.transactions).Count();
1493
1.38M
        }
1494
        /** Function to compute the highest element of a chunk, by fallback_order. */
1495
45.4k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
45.4k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
45.4k
            auto it = chunk.begin();
1498
45.4k
            DepGraphIndex ret = *it;
1499
45.4k
            ++it;
1500
45.4k
            while (it != chunk.end()) {
1501
45.4k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
45.4k
                ++it;
1503
45.4k
            }
1504
45.4k
            return ret;
1505
45.4k
        };
1506
        /** Comparison function for the transaction heap. Note that it is a max-heap, so
1507
         *  tx_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1508
45.4k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
45.4k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
45.4k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
45.4k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
45.4k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
45.4k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
45.4k
            if (a_feerate.size != b_feerate.size) {
1518
45.4k
                return a_feerate.size > b_feerate.size;
1519
45.4k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
45.4k
            auto fallback_cmp = fallback_order(a, b);
1522
45.4k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
45.4k
            Assume(false);
1525
45.4k
            return a < b;
1526
45.4k
        };
1527
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1528
        /** Comparison function for the chunk heap. Note that it is a max-heap, so
1529
         *  chunk_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1530
45.4k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
45.4k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
45.4k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
45.4k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
45.4k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
45.4k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
45.4k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
45.4k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
45.4k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
45.4k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
45.4k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
45.4k
            Assume(false);
1547
45.4k
            return a.second < b.second;
1548
45.4k
        };
1549
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1550
484k
        for (SetIdx chunk_idx : m_chunk_idxs) {
1551
484k
            if (chunk_deps[chunk_idx] == 0) {
1552
128k
                ready_chunks.emplace_back(chunk_idx, max_fallback_fn(chunk_idx));
1553
128k
            }
1554
484k
        }
1555
45.4k
        std::make_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1556
        // Pop chunks off the heap.
1557
529k
        while (!ready_chunks.empty()) {
1558
484k
            auto [chunk_idx, _rnd] = ready_chunks.front();
1559
484k
            std::pop_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1560
484k
            ready_chunks.pop_back();
1561
484k
            Assume(chunk_deps[chunk_idx] == 0);
1562
484k
            const auto& chunk_txn = m_set_info[chunk_idx].transactions;
1563
            // Build heap of all includable transactions in chunk.
1564
484k
            Assume(ready_tx.empty());
1565
1.38M
            for (TxIdx tx_idx : chunk_txn) {
1566
1.38M
                if (tx_deps[tx_idx] == 0) ready_tx.push_back(tx_idx);
1567
1.38M
            }
1568
484k
            Assume(!ready_tx.empty());
1569
484k
            std::make_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1570
            // Pick transactions from the ready heap, append them to linearization, and decrement
1571
            // dependency counts.
1572
1.87M
            while (!ready_tx.empty()) {
1573
                // Pop an element from the tx_ready heap.
1574
1.38M
                auto tx_idx = ready_tx.front();
1575
1.38M
                std::pop_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1576
1.38M
                ready_tx.pop_back();
1577
                // Append to linearization.
1578
1.38M
                ret.push_back(tx_idx);
1579
                // Decrement dependency counts.
1580
1.38M
                auto& tx_data = m_tx_data[tx_idx];
1581
4.42M
                for (TxIdx chl_idx : tx_data.children) {
1582
4.42M
                    auto& chl_data = m_tx_data[chl_idx];
1583
                    // Decrement tx dependency count.
1584
4.42M
                    Assume(tx_deps[chl_idx] > 0);
1585
4.42M
                    if (--tx_deps[chl_idx] == 0 && chunk_txn[chl_idx]) {
1586
                        // Child tx has no dependencies left, and is in this chunk. Add it to the tx heap.
1587
736k
                        ready_tx.push_back(chl_idx);
1588
736k
                        std::push_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1589
736k
                    }
1590
                    // Decrement chunk dependency count if this is out-of-chunk dependency.
1591
4.42M
                    if (chl_data.chunk_idx != chunk_idx) {
1592
2.51M
                        Assume(chunk_deps[chl_data.chunk_idx] > 0);
1593
2.51M
                        if (--chunk_deps[chl_data.chunk_idx] == 0) {
1594
                            // Child chunk has no dependencies left. Add it to the chunk heap.
1595
356k
                            ready_chunks.emplace_back(chl_data.chunk_idx, max_fallback_fn(chl_data.chunk_idx));
1596
356k
                            std::push_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1597
356k
                        }
1598
2.51M
                    }
1599
4.42M
                }
1600
1.38M
            }
1601
484k
        }
1602
45.4k
        Assume(ret.size() == m_set_info.size());
1603
45.4k
        m_cost.GetLinearizationEnd(/*num_txns=*/m_set_info.size(), /*num_deps=*/num_deps);
1604
45.4k
        return ret;
1605
45.4k
    }
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)
Line
Count
Source
1466
45.4k
    {
1467
45.4k
        m_cost.GetLinearizationBegin();
1468
        /** The output linearization. */
1469
45.4k
        std::vector<DepGraphIndex> ret;
1470
45.4k
        ret.reserve(m_set_info.size());
1471
        /** A heap with all chunks (by set index) that can currently be included, sorted by
1472
         *  chunk feerate (high to low), chunk size (small to large), and by least maximum element
1473
         *  according to the fallback order (which is the second pair element). */
1474
45.4k
        std::vector<std::pair<SetIdx, TxIdx>> ready_chunks;
1475
        /** For every chunk, indexed by SetIdx, the number of unmet dependencies the chunk has on
1476
         *  other chunks (not including dependencies within the chunk itself). */
1477
45.4k
        std::vector<TxIdx> chunk_deps(m_set_info.size(), 0);
1478
        /** For every transaction, indexed by TxIdx, the number of unmet dependencies the
1479
         *  transaction has. */
1480
45.4k
        std::vector<TxIdx> tx_deps(m_tx_data.size(), 0);
1481
        /** A heap with all transactions within the current chunk that can be included, sorted by
1482
         *  tx feerate (high to low), tx size (small to large), and fallback order. */
1483
45.4k
        std::vector<TxIdx> ready_tx;
1484
        // Populate chunk_deps and tx_deps.
1485
45.4k
        unsigned num_deps{0};
1486
1.38M
        for (TxIdx chl_idx : m_transaction_idxs) {
1487
1.38M
            const auto& chl_data = m_tx_data[chl_idx];
1488
1.38M
            tx_deps[chl_idx] = chl_data.parents.Count();
1489
1.38M
            num_deps += tx_deps[chl_idx];
1490
1.38M
            auto chl_chunk_idx = chl_data.chunk_idx;
1491
1.38M
            auto& chl_chunk_info = m_set_info[chl_chunk_idx];
1492
1.38M
            chunk_deps[chl_chunk_idx] += (chl_data.parents - chl_chunk_info.transactions).Count();
1493
1.38M
        }
1494
        /** Function to compute the highest element of a chunk, by fallback_order. */
1495
45.4k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
45.4k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
45.4k
            auto it = chunk.begin();
1498
45.4k
            DepGraphIndex ret = *it;
1499
45.4k
            ++it;
1500
45.4k
            while (it != chunk.end()) {
1501
45.4k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
45.4k
                ++it;
1503
45.4k
            }
1504
45.4k
            return ret;
1505
45.4k
        };
1506
        /** Comparison function for the transaction heap. Note that it is a max-heap, so
1507
         *  tx_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1508
45.4k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
45.4k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
45.4k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
45.4k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
45.4k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
45.4k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
45.4k
            if (a_feerate.size != b_feerate.size) {
1518
45.4k
                return a_feerate.size > b_feerate.size;
1519
45.4k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
45.4k
            auto fallback_cmp = fallback_order(a, b);
1522
45.4k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
45.4k
            Assume(false);
1525
45.4k
            return a < b;
1526
45.4k
        };
1527
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1528
        /** Comparison function for the chunk heap. Note that it is a max-heap, so
1529
         *  chunk_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1530
45.4k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
45.4k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
45.4k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
45.4k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
45.4k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
45.4k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
45.4k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
45.4k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
45.4k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
45.4k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
45.4k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
45.4k
            Assume(false);
1547
45.4k
            return a.second < b.second;
1548
45.4k
        };
1549
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1550
484k
        for (SetIdx chunk_idx : m_chunk_idxs) {
1551
484k
            if (chunk_deps[chunk_idx] == 0) {
1552
128k
                ready_chunks.emplace_back(chunk_idx, max_fallback_fn(chunk_idx));
1553
128k
            }
1554
484k
        }
1555
45.4k
        std::make_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1556
        // Pop chunks off the heap.
1557
529k
        while (!ready_chunks.empty()) {
1558
484k
            auto [chunk_idx, _rnd] = ready_chunks.front();
1559
484k
            std::pop_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1560
484k
            ready_chunks.pop_back();
1561
484k
            Assume(chunk_deps[chunk_idx] == 0);
1562
484k
            const auto& chunk_txn = m_set_info[chunk_idx].transactions;
1563
            // Build heap of all includable transactions in chunk.
1564
484k
            Assume(ready_tx.empty());
1565
1.38M
            for (TxIdx tx_idx : chunk_txn) {
1566
1.38M
                if (tx_deps[tx_idx] == 0) ready_tx.push_back(tx_idx);
1567
1.38M
            }
1568
484k
            Assume(!ready_tx.empty());
1569
484k
            std::make_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1570
            // Pick transactions from the ready heap, append them to linearization, and decrement
1571
            // dependency counts.
1572
1.87M
            while (!ready_tx.empty()) {
1573
                // Pop an element from the tx_ready heap.
1574
1.38M
                auto tx_idx = ready_tx.front();
1575
1.38M
                std::pop_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1576
1.38M
                ready_tx.pop_back();
1577
                // Append to linearization.
1578
1.38M
                ret.push_back(tx_idx);
1579
                // Decrement dependency counts.
1580
1.38M
                auto& tx_data = m_tx_data[tx_idx];
1581
4.42M
                for (TxIdx chl_idx : tx_data.children) {
1582
4.42M
                    auto& chl_data = m_tx_data[chl_idx];
1583
                    // Decrement tx dependency count.
1584
4.42M
                    Assume(tx_deps[chl_idx] > 0);
1585
4.42M
                    if (--tx_deps[chl_idx] == 0 && chunk_txn[chl_idx]) {
1586
                        // Child tx has no dependencies left, and is in this chunk. Add it to the tx heap.
1587
736k
                        ready_tx.push_back(chl_idx);
1588
736k
                        std::push_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1589
736k
                    }
1590
                    // Decrement chunk dependency count if this is out-of-chunk dependency.
1591
4.42M
                    if (chl_data.chunk_idx != chunk_idx) {
1592
2.51M
                        Assume(chunk_deps[chl_data.chunk_idx] > 0);
1593
2.51M
                        if (--chunk_deps[chl_data.chunk_idx] == 0) {
1594
                            // Child chunk has no dependencies left. Add it to the chunk heap.
1595
356k
                            ready_chunks.emplace_back(chl_data.chunk_idx, max_fallback_fn(chl_data.chunk_idx));
1596
356k
                            std::push_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1597
356k
                        }
1598
2.51M
                    }
1599
4.42M
                }
1600
1.38M
            }
1601
484k
        }
1602
45.4k
        Assume(ret.size() == m_set_info.size());
1603
45.4k
        m_cost.GetLinearizationEnd(/*num_txns=*/m_set_info.size(), /*num_deps=*/num_deps);
1604
45.4k
        return ret;
1605
45.4k
    }
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)
Line
Count
Source
1466
45.4k
    {
1467
45.4k
        m_cost.GetLinearizationBegin();
1468
        /** The output linearization. */
1469
45.4k
        std::vector<DepGraphIndex> ret;
1470
45.4k
        ret.reserve(m_set_info.size());
1471
        /** A heap with all chunks (by set index) that can currently be included, sorted by
1472
         *  chunk feerate (high to low), chunk size (small to large), and by least maximum element
1473
         *  according to the fallback order (which is the second pair element). */
1474
45.4k
        std::vector<std::pair<SetIdx, TxIdx>> ready_chunks;
1475
        /** For every chunk, indexed by SetIdx, the number of unmet dependencies the chunk has on
1476
         *  other chunks (not including dependencies within the chunk itself). */
1477
45.4k
        std::vector<TxIdx> chunk_deps(m_set_info.size(), 0);
1478
        /** For every transaction, indexed by TxIdx, the number of unmet dependencies the
1479
         *  transaction has. */
1480
45.4k
        std::vector<TxIdx> tx_deps(m_tx_data.size(), 0);
1481
        /** A heap with all transactions within the current chunk that can be included, sorted by
1482
         *  tx feerate (high to low), tx size (small to large), and fallback order. */
1483
45.4k
        std::vector<TxIdx> ready_tx;
1484
        // Populate chunk_deps and tx_deps.
1485
45.4k
        unsigned num_deps{0};
1486
1.38M
        for (TxIdx chl_idx : m_transaction_idxs) {
1487
1.38M
            const auto& chl_data = m_tx_data[chl_idx];
1488
1.38M
            tx_deps[chl_idx] = chl_data.parents.Count();
1489
1.38M
            num_deps += tx_deps[chl_idx];
1490
1.38M
            auto chl_chunk_idx = chl_data.chunk_idx;
1491
1.38M
            auto& chl_chunk_info = m_set_info[chl_chunk_idx];
1492
1.38M
            chunk_deps[chl_chunk_idx] += (chl_data.parents - chl_chunk_info.transactions).Count();
1493
1.38M
        }
1494
        /** Function to compute the highest element of a chunk, by fallback_order. */
1495
45.4k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
45.4k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
45.4k
            auto it = chunk.begin();
1498
45.4k
            DepGraphIndex ret = *it;
1499
45.4k
            ++it;
1500
45.4k
            while (it != chunk.end()) {
1501
45.4k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
45.4k
                ++it;
1503
45.4k
            }
1504
45.4k
            return ret;
1505
45.4k
        };
1506
        /** Comparison function for the transaction heap. Note that it is a max-heap, so
1507
         *  tx_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1508
45.4k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
45.4k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
45.4k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
45.4k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
45.4k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
45.4k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
45.4k
            if (a_feerate.size != b_feerate.size) {
1518
45.4k
                return a_feerate.size > b_feerate.size;
1519
45.4k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
45.4k
            auto fallback_cmp = fallback_order(a, b);
1522
45.4k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
45.4k
            Assume(false);
1525
45.4k
            return a < b;
1526
45.4k
        };
1527
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1528
        /** Comparison function for the chunk heap. Note that it is a max-heap, so
1529
         *  chunk_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1530
45.4k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
45.4k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
45.4k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
45.4k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
45.4k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
45.4k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
45.4k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
45.4k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
45.4k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
45.4k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
45.4k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
45.4k
            Assume(false);
1547
45.4k
            return a.second < b.second;
1548
45.4k
        };
1549
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1550
484k
        for (SetIdx chunk_idx : m_chunk_idxs) {
1551
484k
            if (chunk_deps[chunk_idx] == 0) {
1552
128k
                ready_chunks.emplace_back(chunk_idx, max_fallback_fn(chunk_idx));
1553
128k
            }
1554
484k
        }
1555
45.4k
        std::make_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1556
        // Pop chunks off the heap.
1557
529k
        while (!ready_chunks.empty()) {
1558
484k
            auto [chunk_idx, _rnd] = ready_chunks.front();
1559
484k
            std::pop_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1560
484k
            ready_chunks.pop_back();
1561
484k
            Assume(chunk_deps[chunk_idx] == 0);
1562
484k
            const auto& chunk_txn = m_set_info[chunk_idx].transactions;
1563
            // Build heap of all includable transactions in chunk.
1564
484k
            Assume(ready_tx.empty());
1565
1.38M
            for (TxIdx tx_idx : chunk_txn) {
1566
1.38M
                if (tx_deps[tx_idx] == 0) ready_tx.push_back(tx_idx);
1567
1.38M
            }
1568
484k
            Assume(!ready_tx.empty());
1569
484k
            std::make_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1570
            // Pick transactions from the ready heap, append them to linearization, and decrement
1571
            // dependency counts.
1572
1.87M
            while (!ready_tx.empty()) {
1573
                // Pop an element from the tx_ready heap.
1574
1.38M
                auto tx_idx = ready_tx.front();
1575
1.38M
                std::pop_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1576
1.38M
                ready_tx.pop_back();
1577
                // Append to linearization.
1578
1.38M
                ret.push_back(tx_idx);
1579
                // Decrement dependency counts.
1580
1.38M
                auto& tx_data = m_tx_data[tx_idx];
1581
4.42M
                for (TxIdx chl_idx : tx_data.children) {
1582
4.42M
                    auto& chl_data = m_tx_data[chl_idx];
1583
                    // Decrement tx dependency count.
1584
4.42M
                    Assume(tx_deps[chl_idx] > 0);
1585
4.42M
                    if (--tx_deps[chl_idx] == 0 && chunk_txn[chl_idx]) {
1586
                        // Child tx has no dependencies left, and is in this chunk. Add it to the tx heap.
1587
736k
                        ready_tx.push_back(chl_idx);
1588
736k
                        std::push_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1589
736k
                    }
1590
                    // Decrement chunk dependency count if this is out-of-chunk dependency.
1591
4.42M
                    if (chl_data.chunk_idx != chunk_idx) {
1592
2.51M
                        Assume(chunk_deps[chl_data.chunk_idx] > 0);
1593
2.51M
                        if (--chunk_deps[chl_data.chunk_idx] == 0) {
1594
                            // Child chunk has no dependencies left. Add it to the chunk heap.
1595
356k
                            ready_chunks.emplace_back(chl_data.chunk_idx, max_fallback_fn(chl_data.chunk_idx));
1596
356k
                            std::push_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1597
356k
                        }
1598
2.51M
                    }
1599
4.42M
                }
1600
1.38M
            }
1601
484k
        }
1602
45.4k
        Assume(ret.size() == m_set_info.size());
1603
45.4k
        m_cost.GetLinearizationEnd(/*num_txns=*/m_set_info.size(), /*num_deps=*/num_deps);
1604
45.4k
        return ret;
1605
45.4k
    }
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)
Line
Count
Source
1466
25.0k
    {
1467
25.0k
        m_cost.GetLinearizationBegin();
1468
        /** The output linearization. */
1469
25.0k
        std::vector<DepGraphIndex> ret;
1470
25.0k
        ret.reserve(m_set_info.size());
1471
        /** A heap with all chunks (by set index) that can currently be included, sorted by
1472
         *  chunk feerate (high to low), chunk size (small to large), and by least maximum element
1473
         *  according to the fallback order (which is the second pair element). */
1474
25.0k
        std::vector<std::pair<SetIdx, TxIdx>> ready_chunks;
1475
        /** For every chunk, indexed by SetIdx, the number of unmet dependencies the chunk has on
1476
         *  other chunks (not including dependencies within the chunk itself). */
1477
25.0k
        std::vector<TxIdx> chunk_deps(m_set_info.size(), 0);
1478
        /** For every transaction, indexed by TxIdx, the number of unmet dependencies the
1479
         *  transaction has. */
1480
25.0k
        std::vector<TxIdx> tx_deps(m_tx_data.size(), 0);
1481
        /** A heap with all transactions within the current chunk that can be included, sorted by
1482
         *  tx feerate (high to low), tx size (small to large), and fallback order. */
1483
25.0k
        std::vector<TxIdx> ready_tx;
1484
        // Populate chunk_deps and tx_deps.
1485
25.0k
        unsigned num_deps{0};
1486
421k
        for (TxIdx chl_idx : m_transaction_idxs) {
1487
421k
            const auto& chl_data = m_tx_data[chl_idx];
1488
421k
            tx_deps[chl_idx] = chl_data.parents.Count();
1489
421k
            num_deps += tx_deps[chl_idx];
1490
421k
            auto chl_chunk_idx = chl_data.chunk_idx;
1491
421k
            auto& chl_chunk_info = m_set_info[chl_chunk_idx];
1492
421k
            chunk_deps[chl_chunk_idx] += (chl_data.parents - chl_chunk_info.transactions).Count();
1493
421k
        }
1494
        /** Function to compute the highest element of a chunk, by fallback_order. */
1495
25.0k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
25.0k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
25.0k
            auto it = chunk.begin();
1498
25.0k
            DepGraphIndex ret = *it;
1499
25.0k
            ++it;
1500
25.0k
            while (it != chunk.end()) {
1501
25.0k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
25.0k
                ++it;
1503
25.0k
            }
1504
25.0k
            return ret;
1505
25.0k
        };
1506
        /** Comparison function for the transaction heap. Note that it is a max-heap, so
1507
         *  tx_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1508
25.0k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
25.0k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
25.0k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
25.0k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
25.0k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
25.0k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
25.0k
            if (a_feerate.size != b_feerate.size) {
1518
25.0k
                return a_feerate.size > b_feerate.size;
1519
25.0k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
25.0k
            auto fallback_cmp = fallback_order(a, b);
1522
25.0k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
25.0k
            Assume(false);
1525
25.0k
            return a < b;
1526
25.0k
        };
1527
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1528
        /** Comparison function for the chunk heap. Note that it is a max-heap, so
1529
         *  chunk_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1530
25.0k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
25.0k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
25.0k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
25.0k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
25.0k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
25.0k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
25.0k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
25.0k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
25.0k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
25.0k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
25.0k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
25.0k
            Assume(false);
1547
25.0k
            return a.second < b.second;
1548
25.0k
        };
1549
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1550
138k
        for (SetIdx chunk_idx : m_chunk_idxs) {
1551
138k
            if (chunk_deps[chunk_idx] == 0) {
1552
35.4k
                ready_chunks.emplace_back(chunk_idx, max_fallback_fn(chunk_idx));
1553
35.4k
            }
1554
138k
        }
1555
25.0k
        std::make_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1556
        // Pop chunks off the heap.
1557
163k
        while (!ready_chunks.empty()) {
1558
138k
            auto [chunk_idx, _rnd] = ready_chunks.front();
1559
138k
            std::pop_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1560
138k
            ready_chunks.pop_back();
1561
138k
            Assume(chunk_deps[chunk_idx] == 0);
1562
138k
            const auto& chunk_txn = m_set_info[chunk_idx].transactions;
1563
            // Build heap of all includable transactions in chunk.
1564
138k
            Assume(ready_tx.empty());
1565
421k
            for (TxIdx tx_idx : chunk_txn) {
1566
421k
                if (tx_deps[tx_idx] == 0) ready_tx.push_back(tx_idx);
1567
421k
            }
1568
138k
            Assume(!ready_tx.empty());
1569
138k
            std::make_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1570
            // Pick transactions from the ready heap, append them to linearization, and decrement
1571
            // dependency counts.
1572
559k
            while (!ready_tx.empty()) {
1573
                // Pop an element from the tx_ready heap.
1574
421k
                auto tx_idx = ready_tx.front();
1575
421k
                std::pop_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1576
421k
                ready_tx.pop_back();
1577
                // Append to linearization.
1578
421k
                ret.push_back(tx_idx);
1579
                // Decrement dependency counts.
1580
421k
                auto& tx_data = m_tx_data[tx_idx];
1581
844k
                for (TxIdx chl_idx : tx_data.children) {
1582
844k
                    auto& chl_data = m_tx_data[chl_idx];
1583
                    // Decrement tx dependency count.
1584
844k
                    Assume(tx_deps[chl_idx] > 0);
1585
844k
                    if (--tx_deps[chl_idx] == 0 && chunk_txn[chl_idx]) {
1586
                        // Child tx has no dependencies left, and is in this chunk. Add it to the tx heap.
1587
225k
                        ready_tx.push_back(chl_idx);
1588
225k
                        std::push_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1589
225k
                    }
1590
                    // Decrement chunk dependency count if this is out-of-chunk dependency.
1591
844k
                    if (chl_data.chunk_idx != chunk_idx) {
1592
270k
                        Assume(chunk_deps[chl_data.chunk_idx] > 0);
1593
270k
                        if (--chunk_deps[chl_data.chunk_idx] == 0) {
1594
                            // Child chunk has no dependencies left. Add it to the chunk heap.
1595
103k
                            ready_chunks.emplace_back(chl_data.chunk_idx, max_fallback_fn(chl_data.chunk_idx));
1596
103k
                            std::push_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1597
103k
                        }
1598
270k
                    }
1599
844k
                }
1600
421k
            }
1601
138k
        }
1602
25.0k
        Assume(ret.size() == m_set_info.size());
1603
25.0k
        m_cost.GetLinearizationEnd(/*num_txns=*/m_set_info.size(), /*num_deps=*/num_deps);
1604
25.0k
        return ret;
1605
25.0k
    }
std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<std::compare_three_way>(std::compare_three_way const&)
Line
Count
Source
1466
25.0k
    {
1467
25.0k
        m_cost.GetLinearizationBegin();
1468
        /** The output linearization. */
1469
25.0k
        std::vector<DepGraphIndex> ret;
1470
25.0k
        ret.reserve(m_set_info.size());
1471
        /** A heap with all chunks (by set index) that can currently be included, sorted by
1472
         *  chunk feerate (high to low), chunk size (small to large), and by least maximum element
1473
         *  according to the fallback order (which is the second pair element). */
1474
25.0k
        std::vector<std::pair<SetIdx, TxIdx>> ready_chunks;
1475
        /** For every chunk, indexed by SetIdx, the number of unmet dependencies the chunk has on
1476
         *  other chunks (not including dependencies within the chunk itself). */
1477
25.0k
        std::vector<TxIdx> chunk_deps(m_set_info.size(), 0);
1478
        /** For every transaction, indexed by TxIdx, the number of unmet dependencies the
1479
         *  transaction has. */
1480
25.0k
        std::vector<TxIdx> tx_deps(m_tx_data.size(), 0);
1481
        /** A heap with all transactions within the current chunk that can be included, sorted by
1482
         *  tx feerate (high to low), tx size (small to large), and fallback order. */
1483
25.0k
        std::vector<TxIdx> ready_tx;
1484
        // Populate chunk_deps and tx_deps.
1485
25.0k
        unsigned num_deps{0};
1486
421k
        for (TxIdx chl_idx : m_transaction_idxs) {
1487
421k
            const auto& chl_data = m_tx_data[chl_idx];
1488
421k
            tx_deps[chl_idx] = chl_data.parents.Count();
1489
421k
            num_deps += tx_deps[chl_idx];
1490
421k
            auto chl_chunk_idx = chl_data.chunk_idx;
1491
421k
            auto& chl_chunk_info = m_set_info[chl_chunk_idx];
1492
421k
            chunk_deps[chl_chunk_idx] += (chl_data.parents - chl_chunk_info.transactions).Count();
1493
421k
        }
1494
        /** Function to compute the highest element of a chunk, by fallback_order. */
1495
25.0k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
25.0k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
25.0k
            auto it = chunk.begin();
1498
25.0k
            DepGraphIndex ret = *it;
1499
25.0k
            ++it;
1500
25.0k
            while (it != chunk.end()) {
1501
25.0k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
25.0k
                ++it;
1503
25.0k
            }
1504
25.0k
            return ret;
1505
25.0k
        };
1506
        /** Comparison function for the transaction heap. Note that it is a max-heap, so
1507
         *  tx_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1508
25.0k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
25.0k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
25.0k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
25.0k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
25.0k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
25.0k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
25.0k
            if (a_feerate.size != b_feerate.size) {
1518
25.0k
                return a_feerate.size > b_feerate.size;
1519
25.0k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
25.0k
            auto fallback_cmp = fallback_order(a, b);
1522
25.0k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
25.0k
            Assume(false);
1525
25.0k
            return a < b;
1526
25.0k
        };
1527
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1528
        /** Comparison function for the chunk heap. Note that it is a max-heap, so
1529
         *  chunk_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1530
25.0k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
25.0k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
25.0k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
25.0k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
25.0k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
25.0k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
25.0k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
25.0k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
25.0k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
25.0k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
25.0k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
25.0k
            Assume(false);
1547
25.0k
            return a.second < b.second;
1548
25.0k
        };
1549
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1550
138k
        for (SetIdx chunk_idx : m_chunk_idxs) {
1551
138k
            if (chunk_deps[chunk_idx] == 0) {
1552
35.4k
                ready_chunks.emplace_back(chunk_idx, max_fallback_fn(chunk_idx));
1553
35.4k
            }
1554
138k
        }
1555
25.0k
        std::make_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1556
        // Pop chunks off the heap.
1557
163k
        while (!ready_chunks.empty()) {
1558
138k
            auto [chunk_idx, _rnd] = ready_chunks.front();
1559
138k
            std::pop_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1560
138k
            ready_chunks.pop_back();
1561
138k
            Assume(chunk_deps[chunk_idx] == 0);
1562
138k
            const auto& chunk_txn = m_set_info[chunk_idx].transactions;
1563
            // Build heap of all includable transactions in chunk.
1564
138k
            Assume(ready_tx.empty());
1565
421k
            for (TxIdx tx_idx : chunk_txn) {
1566
421k
                if (tx_deps[tx_idx] == 0) ready_tx.push_back(tx_idx);
1567
421k
            }
1568
138k
            Assume(!ready_tx.empty());
1569
138k
            std::make_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1570
            // Pick transactions from the ready heap, append them to linearization, and decrement
1571
            // dependency counts.
1572
559k
            while (!ready_tx.empty()) {
1573
                // Pop an element from the tx_ready heap.
1574
421k
                auto tx_idx = ready_tx.front();
1575
421k
                std::pop_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1576
421k
                ready_tx.pop_back();
1577
                // Append to linearization.
1578
421k
                ret.push_back(tx_idx);
1579
                // Decrement dependency counts.
1580
421k
                auto& tx_data = m_tx_data[tx_idx];
1581
844k
                for (TxIdx chl_idx : tx_data.children) {
1582
844k
                    auto& chl_data = m_tx_data[chl_idx];
1583
                    // Decrement tx dependency count.
1584
844k
                    Assume(tx_deps[chl_idx] > 0);
1585
844k
                    if (--tx_deps[chl_idx] == 0 && chunk_txn[chl_idx]) {
1586
                        // Child tx has no dependencies left, and is in this chunk. Add it to the tx heap.
1587
225k
                        ready_tx.push_back(chl_idx);
1588
225k
                        std::push_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1589
225k
                    }
1590
                    // Decrement chunk dependency count if this is out-of-chunk dependency.
1591
844k
                    if (chl_data.chunk_idx != chunk_idx) {
1592
270k
                        Assume(chunk_deps[chl_data.chunk_idx] > 0);
1593
270k
                        if (--chunk_deps[chl_data.chunk_idx] == 0) {
1594
                            // Child chunk has no dependencies left. Add it to the chunk heap.
1595
103k
                            ready_chunks.emplace_back(chl_data.chunk_idx, max_fallback_fn(chl_data.chunk_idx));
1596
103k
                            std::push_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1597
103k
                        }
1598
270k
                    }
1599
844k
                }
1600
421k
            }
1601
138k
        }
1602
25.0k
        Assume(ret.size() == m_set_info.size());
1603
25.0k
        m_cost.GetLinearizationEnd(/*num_txns=*/m_set_info.size(), /*num_deps=*/num_deps);
1604
25.0k
        return ret;
1605
25.0k
    }
txgraph.cpp:std::vector<unsigned int, std::allocator<unsigned int>> cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetLinearization<(anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0>((anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&)
Line
Count
Source
1466
5.18k
    {
1467
5.18k
        m_cost.GetLinearizationBegin();
1468
        /** The output linearization. */
1469
5.18k
        std::vector<DepGraphIndex> ret;
1470
5.18k
        ret.reserve(m_set_info.size());
1471
        /** A heap with all chunks (by set index) that can currently be included, sorted by
1472
         *  chunk feerate (high to low), chunk size (small to large), and by least maximum element
1473
         *  according to the fallback order (which is the second pair element). */
1474
5.18k
        std::vector<std::pair<SetIdx, TxIdx>> ready_chunks;
1475
        /** For every chunk, indexed by SetIdx, the number of unmet dependencies the chunk has on
1476
         *  other chunks (not including dependencies within the chunk itself). */
1477
5.18k
        std::vector<TxIdx> chunk_deps(m_set_info.size(), 0);
1478
        /** For every transaction, indexed by TxIdx, the number of unmet dependencies the
1479
         *  transaction has. */
1480
5.18k
        std::vector<TxIdx> tx_deps(m_tx_data.size(), 0);
1481
        /** A heap with all transactions within the current chunk that can be included, sorted by
1482
         *  tx feerate (high to low), tx size (small to large), and fallback order. */
1483
5.18k
        std::vector<TxIdx> ready_tx;
1484
        // Populate chunk_deps and tx_deps.
1485
5.18k
        unsigned num_deps{0};
1486
68.7k
        for (TxIdx chl_idx : m_transaction_idxs) {
1487
68.7k
            const auto& chl_data = m_tx_data[chl_idx];
1488
68.7k
            tx_deps[chl_idx] = chl_data.parents.Count();
1489
68.7k
            num_deps += tx_deps[chl_idx];
1490
68.7k
            auto chl_chunk_idx = chl_data.chunk_idx;
1491
68.7k
            auto& chl_chunk_info = m_set_info[chl_chunk_idx];
1492
68.7k
            chunk_deps[chl_chunk_idx] += (chl_data.parents - chl_chunk_info.transactions).Count();
1493
68.7k
        }
1494
        /** Function to compute the highest element of a chunk, by fallback_order. */
1495
5.18k
        auto max_fallback_fn = [&](SetIdx chunk_idx) noexcept {
1496
5.18k
            auto& chunk = m_set_info[chunk_idx].transactions;
1497
5.18k
            auto it = chunk.begin();
1498
5.18k
            DepGraphIndex ret = *it;
1499
5.18k
            ++it;
1500
5.18k
            while (it != chunk.end()) {
1501
5.18k
                if (fallback_order(*it, ret) > 0) ret = *it;
1502
5.18k
                ++it;
1503
5.18k
            }
1504
5.18k
            return ret;
1505
5.18k
        };
1506
        /** Comparison function for the transaction heap. Note that it is a max-heap, so
1507
         *  tx_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1508
5.18k
        auto tx_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1509
            // Bail out for identical transactions.
1510
5.18k
            if (a == b) return false;
1511
            // First sort by increasing transaction feerate.
1512
5.18k
            auto& a_feerate = m_depgraph.FeeRate(a);
1513
5.18k
            auto& b_feerate = m_depgraph.FeeRate(b);
1514
5.18k
            auto feerate_cmp = ByRatio{a_feerate} <=> ByRatio{b_feerate};
1515
5.18k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1516
            // Then by decreasing transaction size.
1517
5.18k
            if (a_feerate.size != b_feerate.size) {
1518
5.18k
                return a_feerate.size > b_feerate.size;
1519
5.18k
            }
1520
            // Tie-break by decreasing fallback_order.
1521
5.18k
            auto fallback_cmp = fallback_order(a, b);
1522
5.18k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1523
            // This should not be hit, because fallback_order defines a strong ordering.
1524
5.18k
            Assume(false);
1525
5.18k
            return a < b;
1526
5.18k
        };
1527
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1528
        /** Comparison function for the chunk heap. Note that it is a max-heap, so
1529
         *  chunk_cmp_fn(a, b) == true means "a appears after b in the linearization". */
1530
5.18k
        auto chunk_cmp_fn = [&](const auto& a, const auto& b) noexcept {
1531
            // Bail out for identical chunks.
1532
5.18k
            if (a.first == b.first) return false;
1533
            // First sort by increasing chunk feerate.
1534
5.18k
            auto& chunk_feerate_a = m_set_info[a.first].feerate;
1535
5.18k
            auto& chunk_feerate_b = m_set_info[b.first].feerate;
1536
5.18k
            auto feerate_cmp = ByRatio{chunk_feerate_a} <=> ByRatio{chunk_feerate_b};
1537
5.18k
            if (feerate_cmp != 0) return feerate_cmp < 0;
1538
            // Then by decreasing chunk size.
1539
5.18k
            if (chunk_feerate_a.size != chunk_feerate_b.size) {
1540
5.18k
                return chunk_feerate_a.size > chunk_feerate_b.size;
1541
5.18k
            }
1542
            // Tie-break by decreasing fallback_order.
1543
5.18k
            auto fallback_cmp = fallback_order(a.second, b.second);
1544
5.18k
            if (fallback_cmp != 0) return fallback_cmp > 0;
1545
            // This should not be hit, because fallback_order defines a strong ordering.
1546
5.18k
            Assume(false);
1547
5.18k
            return a.second < b.second;
1548
5.18k
        };
1549
        // Construct a heap with all chunks that have no out-of-chunk dependencies.
1550
61.2k
        for (SetIdx chunk_idx : m_chunk_idxs) {
1551
61.2k
            if (chunk_deps[chunk_idx] == 0) {
1552
6.05k
                ready_chunks.emplace_back(chunk_idx, max_fallback_fn(chunk_idx));
1553
6.05k
            }
1554
61.2k
        }
1555
5.18k
        std::make_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1556
        // Pop chunks off the heap.
1557
66.4k
        while (!ready_chunks.empty()) {
1558
61.2k
            auto [chunk_idx, _rnd] = ready_chunks.front();
1559
61.2k
            std::pop_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1560
61.2k
            ready_chunks.pop_back();
1561
61.2k
            Assume(chunk_deps[chunk_idx] == 0);
1562
61.2k
            const auto& chunk_txn = m_set_info[chunk_idx].transactions;
1563
            // Build heap of all includable transactions in chunk.
1564
61.2k
            Assume(ready_tx.empty());
1565
68.7k
            for (TxIdx tx_idx : chunk_txn) {
1566
68.7k
                if (tx_deps[tx_idx] == 0) ready_tx.push_back(tx_idx);
1567
68.7k
            }
1568
61.2k
            Assume(!ready_tx.empty());
1569
61.2k
            std::make_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1570
            // Pick transactions from the ready heap, append them to linearization, and decrement
1571
            // dependency counts.
1572
130k
            while (!ready_tx.empty()) {
1573
                // Pop an element from the tx_ready heap.
1574
68.7k
                auto tx_idx = ready_tx.front();
1575
68.7k
                std::pop_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1576
68.7k
                ready_tx.pop_back();
1577
                // Append to linearization.
1578
68.7k
                ret.push_back(tx_idx);
1579
                // Decrement dependency counts.
1580
68.7k
                auto& tx_data = m_tx_data[tx_idx];
1581
68.7k
                for (TxIdx chl_idx : tx_data.children) {
1582
63.6k
                    auto& chl_data = m_tx_data[chl_idx];
1583
                    // Decrement tx dependency count.
1584
63.6k
                    Assume(tx_deps[chl_idx] > 0);
1585
63.6k
                    if (--tx_deps[chl_idx] == 0 && chunk_txn[chl_idx]) {
1586
                        // Child tx has no dependencies left, and is in this chunk. Add it to the tx heap.
1587
7.35k
                        ready_tx.push_back(chl_idx);
1588
7.35k
                        std::push_heap(ready_tx.begin(), ready_tx.end(), tx_cmp_fn);
1589
7.35k
                    }
1590
                    // Decrement chunk dependency count if this is out-of-chunk dependency.
1591
63.6k
                    if (chl_data.chunk_idx != chunk_idx) {
1592
56.1k
                        Assume(chunk_deps[chl_data.chunk_idx] > 0);
1593
56.1k
                        if (--chunk_deps[chl_data.chunk_idx] == 0) {
1594
                            // Child chunk has no dependencies left. Add it to the chunk heap.
1595
55.2k
                            ready_chunks.emplace_back(chl_data.chunk_idx, max_fallback_fn(chl_data.chunk_idx));
1596
55.2k
                            std::push_heap(ready_chunks.begin(), ready_chunks.end(), chunk_cmp_fn);
1597
55.2k
                        }
1598
56.1k
                    }
1599
63.6k
                }
1600
68.7k
            }
1601
61.2k
        }
1602
5.18k
        Assume(ret.size() == m_set_info.size());
1603
5.18k
        m_cost.GetLinearizationEnd(/*num_txns=*/m_set_info.size(), /*num_deps=*/num_deps);
1604
5.18k
        return ret;
1605
5.18k
    }
1606
1607
    /** Get the diagram for the current state, which must be topological. Test-only.
1608
     *
1609
     * The linearization produced by GetLinearization() is always at least as good (in the
1610
     * CompareChunks() sense) as this diagram, but may be better.
1611
     *
1612
     * After an OptimizeStep(), the diagram will always be at least as good as before. Once
1613
     * OptimizeStep() returns false, the diagram will be equivalent to that produced by
1614
     * GetLinearization(), and optimal.
1615
     *
1616
     * After a MinimizeStep(), the diagram cannot change anymore (in the CompareChunks() sense),
1617
     * but its number of segments can increase still. Once MinimizeStep() returns false, the number
1618
     * of chunks of the produced linearization will match the number of segments in the diagram.
1619
     */
1620
    std::vector<FeeFrac> GetDiagram() const noexcept
1621
    {
1622
        std::vector<FeeFrac> ret;
1623
        for (auto chunk_idx : m_chunk_idxs) {
1624
            ret.push_back(m_set_info[chunk_idx].feerate);
1625
        }
1626
        std::ranges::sort(ret, std::greater<ByRatioNegSize<FeeFrac>>{});
1627
        return ret;
1628
    }
1629
1630
    /** Determine how much work was performed so far. */
1631
5.07M
    uint64_t GetCost() const noexcept { return m_cost.GetCost(); }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned long>, cluster_linearize::SFLDefaultCostModel>::GetCost() const
Line
Count
Source
1631
1.52M
    uint64_t GetCost() const noexcept { return m_cost.GetCost(); }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned int, 2u>, cluster_linearize::SFLDefaultCostModel>::GetCost() const
Line
Count
Source
1631
1.37M
    uint64_t GetCost() const noexcept { return m_cost.GetCost(); }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 8u>, cluster_linearize::SFLDefaultCostModel>::GetCost() const
Line
Count
Source
1631
1.37M
    uint64_t GetCost() const noexcept { return m_cost.GetCost(); }
cluster_linearize::SpanningForestState<bitset_detail::IntBitSet<unsigned int>, cluster_linearize::SFLDefaultCostModel>::GetCost() const
Line
Count
Source
1631
398k
    uint64_t GetCost() const noexcept { return m_cost.GetCost(); }
cluster_linearize::SpanningForestState<bitset_detail::MultiIntBitSet<unsigned char, 4u>, cluster_linearize::SFLDefaultCostModel>::GetCost() const
Line
Count
Source
1631
397k
    uint64_t GetCost() const noexcept { return m_cost.GetCost(); }
1632
1633
    /** Verify internal consistency of the data structure. */
1634
    void SanityCheck() const
1635
    {
1636
        //
1637
        // Verify dependency parent/child information, and build list of (active) dependencies.
1638
        //
1639
        std::vector<std::pair<TxIdx, TxIdx>> expected_dependencies;
1640
        std::vector<std::pair<TxIdx, TxIdx>> all_dependencies;
1641
        std::vector<std::pair<TxIdx, TxIdx>> active_dependencies;
1642
        for (auto parent_idx : m_depgraph.Positions()) {
1643
            for (auto child_idx : m_depgraph.GetReducedChildren(parent_idx)) {
1644
                expected_dependencies.emplace_back(parent_idx, child_idx);
1645
            }
1646
        }
1647
        for (auto tx_idx : m_transaction_idxs) {
1648
            for (auto child_idx : m_tx_data[tx_idx].children) {
1649
                all_dependencies.emplace_back(tx_idx, child_idx);
1650
                if (m_tx_data[tx_idx].active_children[child_idx]) {
1651
                    active_dependencies.emplace_back(tx_idx, child_idx);
1652
                }
1653
            }
1654
        }
1655
        std::ranges::sort(expected_dependencies);
1656
        std::ranges::sort(all_dependencies);
1657
        assert(expected_dependencies == all_dependencies);
1658
1659
        //
1660
        // Verify the chunks against the list of active dependencies
1661
        //
1662
        SetType chunk_cover;
1663
        for (auto chunk_idx : m_chunk_idxs) {
1664
            const auto& chunk_info = m_set_info[chunk_idx];
1665
            // Verify that transactions in the chunk point back to it. This guarantees
1666
            // that chunks are non-overlapping.
1667
            for (auto tx_idx : chunk_info.transactions) {
1668
                assert(m_tx_data[tx_idx].chunk_idx == chunk_idx);
1669
            }
1670
            assert(!chunk_cover.Overlaps(chunk_info.transactions));
1671
            chunk_cover |= chunk_info.transactions;
1672
            // Verify the chunk's transaction set: start from an arbitrary chunk transaction,
1673
            // and for every active dependency, if it contains the parent or child, add the
1674
            // other. It must have exactly N-1 active dependencies in it, guaranteeing it is
1675
            // acyclic.
1676
            assert(chunk_info.transactions.Any());
1677
            SetType expected_chunk = SetType::Singleton(chunk_info.transactions.First());
1678
            while (true) {
1679
                auto old = expected_chunk;
1680
                size_t active_dep_count{0};
1681
                for (const auto& [par, chl] : active_dependencies) {
1682
                    if (expected_chunk[par] || expected_chunk[chl]) {
1683
                        expected_chunk.Set(par);
1684
                        expected_chunk.Set(chl);
1685
                        ++active_dep_count;
1686
                    }
1687
                }
1688
                if (old == expected_chunk) {
1689
                    assert(expected_chunk.Count() == active_dep_count + 1);
1690
                    break;
1691
                }
1692
            }
1693
            assert(chunk_info.transactions == expected_chunk);
1694
            // Verify the chunk's feerate.
1695
            assert(chunk_info.feerate == m_depgraph.FeeRate(chunk_info.transactions));
1696
            // Verify the chunk's reachable transactions.
1697
            assert(m_reachable[chunk_idx] == GetReachable(expected_chunk));
1698
            // Verify that the chunk's reachable transactions don't include its own transactions.
1699
            assert(!m_reachable[chunk_idx].first.Overlaps(chunk_info.transactions));
1700
            assert(!m_reachable[chunk_idx].second.Overlaps(chunk_info.transactions));
1701
        }
1702
        // Verify that together, the chunks cover all transactions.
1703
        assert(chunk_cover == m_depgraph.Positions());
1704
1705
        //
1706
        // Verify transaction data.
1707
        //
1708
        assert(m_transaction_idxs == m_depgraph.Positions());
1709
        for (auto tx_idx : m_transaction_idxs) {
1710
            const auto& tx_data = m_tx_data[tx_idx];
1711
            // Verify it has a valid chunk index, and that chunk includes this transaction.
1712
            assert(m_chunk_idxs[tx_data.chunk_idx]);
1713
            assert(m_set_info[tx_data.chunk_idx].transactions[tx_idx]);
1714
            // Verify parents/children.
1715
            assert(tx_data.parents == m_depgraph.GetReducedParents(tx_idx));
1716
            assert(tx_data.children == m_depgraph.GetReducedChildren(tx_idx));
1717
            // Verify active_children is a subset of children.
1718
            assert(tx_data.active_children.IsSubsetOf(tx_data.children));
1719
            // Verify each active child's dep_top_idx points to a valid non-chunk set.
1720
            for (auto child_idx : tx_data.active_children) {
1721
                assert(tx_data.dep_top_idx[child_idx] < m_set_info.size());
1722
                assert(!m_chunk_idxs[tx_data.dep_top_idx[child_idx]]);
1723
            }
1724
        }
1725
1726
        //
1727
        // Verify active dependencies' top sets.
1728
        //
1729
        for (const auto& [par_idx, chl_idx] : active_dependencies) {
1730
            // Verify the top set's transactions: it must contain the parent, and for every
1731
            // active dependency, except the chl_idx->par_idx dependency itself, if it contains the
1732
            // parent or child, it must contain both. It must have exactly N-1 active dependencies
1733
            // in it, guaranteeing it is acyclic.
1734
            SetType expected_top = SetType::Singleton(par_idx);
1735
            while (true) {
1736
                auto old = expected_top;
1737
                size_t active_dep_count{0};
1738
                for (const auto& [par2_idx, chl2_idx] : active_dependencies) {
1739
                    if (par_idx == par2_idx && chl_idx == chl2_idx) continue;
1740
                    if (expected_top[par2_idx] || expected_top[chl2_idx]) {
1741
                        expected_top.Set(par2_idx);
1742
                        expected_top.Set(chl2_idx);
1743
                        ++active_dep_count;
1744
                    }
1745
                }
1746
                if (old == expected_top) {
1747
                    assert(expected_top.Count() == active_dep_count + 1);
1748
                    break;
1749
                }
1750
            }
1751
            assert(!expected_top[chl_idx]);
1752
            auto& dep_top_info = m_set_info[m_tx_data[par_idx].dep_top_idx[chl_idx]];
1753
            assert(dep_top_info.transactions == expected_top);
1754
            // Verify the top set's feerate.
1755
            assert(dep_top_info.feerate == m_depgraph.FeeRate(dep_top_info.transactions));
1756
        }
1757
1758
        //
1759
        // Verify m_suboptimal_chunks.
1760
        //
1761
        SetType suboptimal_idxs;
1762
        for (size_t i = 0; i < m_suboptimal_chunks.size(); ++i) {
1763
            auto chunk_idx = m_suboptimal_chunks[i];
1764
            assert(!suboptimal_idxs[chunk_idx]);
1765
            suboptimal_idxs.Set(chunk_idx);
1766
        }
1767
        assert(m_suboptimal_idxs == suboptimal_idxs);
1768
1769
        //
1770
        // Verify m_nonminimal_chunks.
1771
        //
1772
        SetType nonminimal_idxs;
1773
        for (size_t i = 0; i < m_nonminimal_chunks.size(); ++i) {
1774
            auto [chunk_idx, pivot, flags] = m_nonminimal_chunks[i];
1775
            assert(m_tx_data[pivot].chunk_idx == chunk_idx);
1776
            assert(!nonminimal_idxs[chunk_idx]);
1777
            nonminimal_idxs.Set(chunk_idx);
1778
        }
1779
        assert(nonminimal_idxs.IsSubsetOf(m_chunk_idxs));
1780
    }
1781
};
1782
1783
/** Find or improve a linearization for a cluster.
1784
 *
1785
 * @param[in] depgraph            Dependency graph of the cluster to be linearized.
1786
 * @param[in] max_cost            Upper bound on the amount of work that will be done.
1787
 * @param[in] rng_seed            A random number seed to control search order. This prevents peers
1788
 *                                from predicting exactly which clusters would be hard for us to
1789
 *                                linearize.
1790
 * @param[in] fallback_order      A comparator to order transactions, used to sort equal-feerate
1791
 *                                chunks and transactions. See SpanningForestState::GetLinearization
1792
 *                                for details.
1793
 * @param[in] old_linearization   An existing linearization for the cluster, or empty.
1794
 * @param[in] is_topological      (Only relevant if old_linearization is not empty) Whether
1795
 *                                old_linearization is topologically valid.
1796
 * @return                        A tuple of:
1797
 *                                - The resulting linearization. It is guaranteed to be at least as
1798
 *                                  good (in the feerate diagram sense) as old_linearization.
1799
 *                                - A boolean indicating whether the result is guaranteed to be
1800
 *                                  optimal with minimal chunks.
1801
 *                                - How many optimization steps were actually performed.
1802
 */
1803
template<typename SetType>
1804
std::tuple<std::vector<DepGraphIndex>, bool, uint64_t> Linearize(
1805
    const DepGraph<SetType>& depgraph,
1806
    uint64_t max_cost,
1807
    uint64_t rng_seed,
1808
    const StrongComparator<DepGraphIndex> auto& fallback_order,
1809
    std::span<const DepGraphIndex> old_linearization = {},
1810
    bool is_topological = true) noexcept
1811
191k
{
1812
    /** Initialize a spanning forest data structure for this cluster. */
1813
191k
    SpanningForestState forest(depgraph, rng_seed);
1814
191k
    if (!old_linearization.empty()) {
1815
144k
        forest.LoadLinearization(old_linearization);
1816
144k
        if (!is_topological) forest.MakeTopological();
1817
144k
    } else {
1818
47.2k
        forest.MakeTopological();
1819
47.2k
    }
1820
    // Make improvement steps to it until we hit the max_iterations limit, or an optimal result
1821
    // is found.
1822
191k
    if (forest.GetCost() < max_cost) {
1823
191k
        forest.StartOptimizing();
1824
2.55M
        do {
1825
2.55M
            if (!forest.OptimizeStep()) break;
1826
2.55M
        } while (forest.GetCost() < max_cost);
1827
191k
    }
1828
    // Make chunk minimization steps until we hit the max_iterations limit, or all chunks are
1829
    // minimal.
1830
191k
    bool optimal = false;
1831
191k
    if (forest.GetCost() < max_cost) {
1832
191k
        forest.StartMinimizing();
1833
2.32M
        do {
1834
2.32M
            if (!forest.MinimizeStep()) {
1835
191k
                optimal = true;
1836
191k
                break;
1837
191k
            }
1838
2.32M
        } while (forest.GetCost() < max_cost);
1839
191k
    }
1840
191k
    return {forest.GetLinearization(fallback_order), optimal, forest.GetCost()};
1841
191k
}
std::tuple<std::vector<unsigned int, std::allocator<unsigned int>>, bool, unsigned long> cluster_linearize::Linearize<bitset_detail::IntBitSet<unsigned long>, std::compare_three_way>(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>> const&, unsigned long, unsigned long, std::compare_three_way const&, std::span<unsigned int const, 18446744073709551615ul>, bool)
Line
Count
Source
1811
45.4k
{
1812
    /** Initialize a spanning forest data structure for this cluster. */
1813
45.4k
    SpanningForestState forest(depgraph, rng_seed);
1814
45.4k
    if (!old_linearization.empty()) {
1815
33.8k
        forest.LoadLinearization(old_linearization);
1816
33.8k
        if (!is_topological) forest.MakeTopological();
1817
33.8k
    } else {
1818
11.5k
        forest.MakeTopological();
1819
11.5k
    }
1820
    // Make improvement steps to it until we hit the max_iterations limit, or an optimal result
1821
    // is found.
1822
45.4k
    if (forest.GetCost() < max_cost) {
1823
45.4k
        forest.StartOptimizing();
1824
733k
        do {
1825
733k
            if (!forest.OptimizeStep()) break;
1826
733k
        } while (forest.GetCost() < max_cost);
1827
45.4k
    }
1828
    // Make chunk minimization steps until we hit the max_iterations limit, or all chunks are
1829
    // minimal.
1830
45.4k
    bool optimal = false;
1831
45.4k
    if (forest.GetCost() < max_cost) {
1832
45.4k
        forest.StartMinimizing();
1833
596k
        do {
1834
596k
            if (!forest.MinimizeStep()) {
1835
45.4k
                optimal = true;
1836
45.4k
                break;
1837
45.4k
            }
1838
596k
        } while (forest.GetCost() < max_cost);
1839
45.4k
    }
1840
45.4k
    return {forest.GetLinearization(fallback_order), optimal, forest.GetCost()};
1841
45.4k
}
std::tuple<std::vector<unsigned int, std::allocator<unsigned int>>, bool, unsigned long> cluster_linearize::Linearize<bitset_detail::MultiIntBitSet<unsigned int, 2u>, std::compare_three_way>(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned int, 2u>> const&, unsigned long, unsigned long, std::compare_three_way const&, std::span<unsigned int const, 18446744073709551615ul>, bool)
Line
Count
Source
1811
45.4k
{
1812
    /** Initialize a spanning forest data structure for this cluster. */
1813
45.4k
    SpanningForestState forest(depgraph, rng_seed);
1814
45.4k
    if (!old_linearization.empty()) {
1815
33.8k
        forest.LoadLinearization(old_linearization);
1816
33.8k
        if (!is_topological) forest.MakeTopological();
1817
33.8k
    } else {
1818
11.5k
        forest.MakeTopological();
1819
11.5k
    }
1820
    // Make improvement steps to it until we hit the max_iterations limit, or an optimal result
1821
    // is found.
1822
45.4k
    if (forest.GetCost() < max_cost) {
1823
45.4k
        forest.StartOptimizing();
1824
735k
        do {
1825
735k
            if (!forest.OptimizeStep()) break;
1826
735k
        } while (forest.GetCost() < max_cost);
1827
45.4k
    }
1828
    // Make chunk minimization steps until we hit the max_iterations limit, or all chunks are
1829
    // minimal.
1830
45.4k
    bool optimal = false;
1831
45.4k
    if (forest.GetCost() < max_cost) {
1832
45.4k
        forest.StartMinimizing();
1833
596k
        do {
1834
596k
            if (!forest.MinimizeStep()) {
1835
45.4k
                optimal = true;
1836
45.4k
                break;
1837
45.4k
            }
1838
596k
        } while (forest.GetCost() < max_cost);
1839
45.4k
    }
1840
45.4k
    return {forest.GetLinearization(fallback_order), optimal, forest.GetCost()};
1841
45.4k
}
std::tuple<std::vector<unsigned int, std::allocator<unsigned int>>, bool, unsigned long> cluster_linearize::Linearize<bitset_detail::MultiIntBitSet<unsigned char, 8u>, std::compare_three_way>(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 8u>> const&, unsigned long, unsigned long, std::compare_three_way const&, std::span<unsigned int const, 18446744073709551615ul>, bool)
Line
Count
Source
1811
45.4k
{
1812
    /** Initialize a spanning forest data structure for this cluster. */
1813
45.4k
    SpanningForestState forest(depgraph, rng_seed);
1814
45.4k
    if (!old_linearization.empty()) {
1815
34.0k
        forest.LoadLinearization(old_linearization);
1816
34.0k
        if (!is_topological) forest.MakeTopological();
1817
34.0k
    } else {
1818
11.3k
        forest.MakeTopological();
1819
11.3k
    }
1820
    // Make improvement steps to it until we hit the max_iterations limit, or an optimal result
1821
    // is found.
1822
45.4k
    if (forest.GetCost() < max_cost) {
1823
45.4k
        forest.StartOptimizing();
1824
733k
        do {
1825
733k
            if (!forest.OptimizeStep()) break;
1826
733k
        } while (forest.GetCost() < max_cost);
1827
45.4k
    }
1828
    // Make chunk minimization steps until we hit the max_iterations limit, or all chunks are
1829
    // minimal.
1830
45.4k
    bool optimal = false;
1831
45.4k
    if (forest.GetCost() < max_cost) {
1832
45.4k
        forest.StartMinimizing();
1833
596k
        do {
1834
596k
            if (!forest.MinimizeStep()) {
1835
45.4k
                optimal = true;
1836
45.4k
                break;
1837
45.4k
            }
1838
596k
        } while (forest.GetCost() < max_cost);
1839
45.4k
    }
1840
45.4k
    return {forest.GetLinearization(fallback_order), optimal, forest.GetCost()};
1841
45.4k
}
std::tuple<std::vector<unsigned int, std::allocator<unsigned int>>, bool, unsigned long> cluster_linearize::Linearize<bitset_detail::IntBitSet<unsigned int>, std::compare_three_way>(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned int>> const&, unsigned long, unsigned long, std::compare_three_way const&, std::span<unsigned int const, 18446744073709551615ul>, bool)
Line
Count
Source
1811
25.0k
{
1812
    /** Initialize a spanning forest data structure for this cluster. */
1813
25.0k
    SpanningForestState forest(depgraph, rng_seed);
1814
25.0k
    if (!old_linearization.empty()) {
1815
18.5k
        forest.LoadLinearization(old_linearization);
1816
18.5k
        if (!is_topological) forest.MakeTopological();
1817
18.5k
    } else {
1818
6.45k
        forest.MakeTopological();
1819
6.45k
    }
1820
    // Make improvement steps to it until we hit the max_iterations limit, or an optimal result
1821
    // is found.
1822
25.0k
    if (forest.GetCost() < max_cost) {
1823
25.0k
        forest.StartOptimizing();
1824
172k
        do {
1825
172k
            if (!forest.OptimizeStep()) break;
1826
172k
        } while (forest.GetCost() < max_cost);
1827
25.0k
    }
1828
    // Make chunk minimization steps until we hit the max_iterations limit, or all chunks are
1829
    // minimal.
1830
25.0k
    bool optimal = false;
1831
25.0k
    if (forest.GetCost() < max_cost) {
1832
25.0k
        forest.StartMinimizing();
1833
200k
        do {
1834
200k
            if (!forest.MinimizeStep()) {
1835
25.0k
                optimal = true;
1836
25.0k
                break;
1837
25.0k
            }
1838
200k
        } while (forest.GetCost() < max_cost);
1839
25.0k
    }
1840
25.0k
    return {forest.GetLinearization(fallback_order), optimal, forest.GetCost()};
1841
25.0k
}
std::tuple<std::vector<unsigned int, std::allocator<unsigned int>>, bool, unsigned long> cluster_linearize::Linearize<bitset_detail::MultiIntBitSet<unsigned char, 4u>, std::compare_three_way>(cluster_linearize::DepGraph<bitset_detail::MultiIntBitSet<unsigned char, 4u>> const&, unsigned long, unsigned long, std::compare_three_way const&, std::span<unsigned int const, 18446744073709551615ul>, bool)
Line
Count
Source
1811
25.0k
{
1812
    /** Initialize a spanning forest data structure for this cluster. */
1813
25.0k
    SpanningForestState forest(depgraph, rng_seed);
1814
25.0k
    if (!old_linearization.empty()) {
1815
18.6k
        forest.LoadLinearization(old_linearization);
1816
18.6k
        if (!is_topological) forest.MakeTopological();
1817
18.6k
    } else {
1818
6.38k
        forest.MakeTopological();
1819
6.38k
    }
1820
    // Make improvement steps to it until we hit the max_iterations limit, or an optimal result
1821
    // is found.
1822
25.0k
    if (forest.GetCost() < max_cost) {
1823
25.0k
        forest.StartOptimizing();
1824
172k
        do {
1825
172k
            if (!forest.OptimizeStep()) break;
1826
172k
        } while (forest.GetCost() < max_cost);
1827
25.0k
    }
1828
    // Make chunk minimization steps until we hit the max_iterations limit, or all chunks are
1829
    // minimal.
1830
25.0k
    bool optimal = false;
1831
25.0k
    if (forest.GetCost() < max_cost) {
1832
25.0k
        forest.StartMinimizing();
1833
200k
        do {
1834
200k
            if (!forest.MinimizeStep()) {
1835
25.0k
                optimal = true;
1836
25.0k
                break;
1837
25.0k
            }
1838
200k
        } while (forest.GetCost() < max_cost);
1839
25.0k
    }
1840
25.0k
    return {forest.GetLinearization(fallback_order), optimal, forest.GetCost()};
1841
25.0k
}
txgraph.cpp:std::tuple<std::vector<unsigned int, std::allocator<unsigned int>>, bool, unsigned long> cluster_linearize::Linearize<bitset_detail::IntBitSet<unsigned long>, (anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0>(cluster_linearize::DepGraph<bitset_detail::IntBitSet<unsigned long>> const&, unsigned long, unsigned long, (anonymous namespace)::GenericClusterImpl::Relinearize((anonymous namespace)::TxGraphImpl&, int, unsigned long)::$_0 const&, std::span<unsigned int const, 18446744073709551615ul>, bool)
Line
Count
Source
1811
5.18k
{
1812
    /** Initialize a spanning forest data structure for this cluster. */
1813
5.18k
    SpanningForestState forest(depgraph, rng_seed);
1814
5.18k
    if (!old_linearization.empty()) {
1815
5.18k
        forest.LoadLinearization(old_linearization);
1816
5.18k
        if (!is_topological) forest.MakeTopological();
1817
5.18k
    } else {
1818
0
        forest.MakeTopological();
1819
0
    }
1820
    // Make improvement steps to it until we hit the max_iterations limit, or an optimal result
1821
    // is found.
1822
5.18k
    if (forest.GetCost() < max_cost) {
1823
5.18k
        forest.StartOptimizing();
1824
10.9k
        do {
1825
10.9k
            if (!forest.OptimizeStep()) break;
1826
10.9k
        } while (forest.GetCost() < max_cost);
1827
5.18k
    }
1828
    // Make chunk minimization steps until we hit the max_iterations limit, or all chunks are
1829
    // minimal.
1830
5.18k
    bool optimal = false;
1831
5.18k
    if (forest.GetCost() < max_cost) {
1832
5.18k
        forest.StartMinimizing();
1833
131k
        do {
1834
131k
            if (!forest.MinimizeStep()) {
1835
5.18k
                optimal = true;
1836
5.18k
                break;
1837
5.18k
            }
1838
131k
        } while (forest.GetCost() < max_cost);
1839
5.18k
    }
1840
5.18k
    return {forest.GetLinearization(fallback_order), optimal, forest.GetCost()};
1841
5.18k
}
1842
1843
/** Improve a given linearization.
1844
 *
1845
 * @param[in]     depgraph       Dependency graph of the cluster being linearized.
1846
 * @param[in,out] linearization  On input, an existing linearization for depgraph. On output, a
1847
 *                               potentially better linearization for the same graph.
1848
 *
1849
 * Postlinearization guarantees:
1850
 * - The resulting chunks are connected.
1851
 * - If the input has a tree shape (either all transactions have at most one child, or all
1852
 *   transactions have at most one parent), the result is optimal.
1853
 * - Given a linearization L1 and a leaf transaction T in it. Let L2 be L1 with T moved to the end,
1854
 *   optionally with its fee increased. Let L3 be the postlinearization of L2. L3 will be at least
1855
 *   as good as L1. This means that replacing transactions with same-size higher-fee transactions
1856
 *   will not worsen linearizations through a "drop conflicts, append new transactions,
1857
 *   postlinearize" process.
1858
 */
1859
template<typename SetType>
1860
void PostLinearize(const DepGraph<SetType>& depgraph, std::span<DepGraphIndex> linearization)
1861
5.18k
{
1862
    // This algorithm performs a number of passes (currently 2); the even ones operate from back to
1863
    // front, the odd ones from front to back. Each results in an equal-or-better linearization
1864
    // than the one started from.
1865
    // - One pass in either direction guarantees that the resulting chunks are connected.
1866
    // - Each direction corresponds to one shape of tree being linearized optimally (forward passes
1867
    //   guarantee this for graphs where each transaction has at most one child; backward passes
1868
    //   guarantee this for graphs where each transaction has at most one parent).
1869
    // - Starting with a backward pass guarantees the moved-tree property.
1870
    //
1871
    // During an odd (forward) pass, the high-level operation is:
1872
    // - Start with an empty list of groups L=[].
1873
    // - For every transaction i in the old linearization, from front to back:
1874
    //   - Append a new group C=[i], containing just i, to the back of L.
1875
    //   - While L has at least one group before C, and the group immediately before C has feerate
1876
    //     lower than C:
1877
    //     - If C depends on P:
1878
    //       - Merge P into C, making C the concatenation of P+C, continuing with the combined C.
1879
    //     - Otherwise:
1880
    //       - Swap P with C, continuing with the now-moved C.
1881
    // - The output linearization is the concatenation of the groups in L.
1882
    //
1883
    // During even (backward) passes, i iterates from the back to the front of the existing
1884
    // linearization, and new groups are prepended instead of appended to the list L. To enable
1885
    // more code reuse, both passes append groups, but during even passes the meanings of
1886
    // parent/child, and of high/low feerate are reversed, and the final concatenation is reversed
1887
    // on output.
1888
    //
1889
    // In the implementation below, the groups are represented by singly-linked lists (pointing
1890
    // from the back to the front), which are themselves organized in a singly-linked circular
1891
    // list (each group pointing to its predecessor, with a special sentinel group at the front
1892
    // that points back to the last group).
1893
    //
1894
    // Information about transaction t is stored in entries[t + 1], while the sentinel is in
1895
    // entries[0].
1896
1897
    /** Index of the sentinel in the entries array below. */
1898
5.18k
    static constexpr DepGraphIndex SENTINEL{0};
1899
    /** Indicator that a group has no previous transaction. */
1900
5.18k
    static constexpr DepGraphIndex NO_PREV_TX{0};
1901
1902
1903
    /** Data structure per transaction entry. */
1904
5.18k
    struct TxEntry
1905
5.18k
    {
1906
        /** The index of the previous transaction in this group; NO_PREV_TX if this is the first
1907
         *  entry of a group. */
1908
5.18k
        DepGraphIndex prev_tx;
1909
1910
        // The fields below are only used for transactions that are the last one in a group
1911
        // (referred to as tail transactions below).
1912
1913
        /** Index of the first transaction in this group, possibly itself. */
1914
5.18k
        DepGraphIndex first_tx;
1915
        /** Index of the last transaction in the previous group. The first group (the sentinel)
1916
         *  points back to the last group here, making it a singly-linked circular list. */
1917
5.18k
        DepGraphIndex prev_group;
1918
        /** All transactions in the group. Empty for the sentinel. */
1919
5.18k
        SetType group;
1920
        /** All dependencies of the group (descendants in even passes; ancestors in odd ones). */
1921
5.18k
        SetType deps;
1922
        /** The combined fee/size of transactions in the group. Fee is negated in even passes. */
1923
5.18k
        FeeFrac feerate;
1924
5.18k
    };
1925
1926
    // As an example, consider the state corresponding to the linearization [1,0,3,2], with
1927
    // groups [1,0,3] and [2], in an odd pass. The linked lists would be:
1928
    //
1929
    //                                        +-----+
1930
    //                                 0<-P-- | 0 S | ---\     Legend:
1931
    //                                        +-----+    |
1932
    //                                           ^       |     - digit in box: entries index
1933
    //             /--------------F---------+    G       |       (note: one more than tx value)
1934
    //             v                         \   |       |     - S: sentinel group
1935
    //          +-----+        +-----+        +-----+    |          (empty feerate)
1936
    //   0<-P-- | 2   | <--P-- | 1   | <--P-- | 4 T |    |     - T: tail transaction, contains
1937
    //          +-----+        +-----+        +-----+    |          fields beyond prev_tv.
1938
    //                                           ^       |     - P: prev_tx reference
1939
    //                                           G       G     - F: first_tx reference
1940
    //                                           |       |     - G: prev_group reference
1941
    //                                        +-----+    |
1942
    //                                 0<-P-- | 3 T | <--/
1943
    //                                        +-----+
1944
    //                                         ^   |
1945
    //                                         \-F-/
1946
    //
1947
    // During an even pass, the diagram above would correspond to linearization [2,3,0,1], with
1948
    // groups [2] and [3,0,1].
1949
1950
5.18k
    std::vector<TxEntry> entries(depgraph.PositionRange() + 1);
1951
1952
    // Perform two passes over the linearization.
1953
15.5k
    for (int pass = 0; pass < 2; ++pass) {
1954
10.3k
        int rev = !(pass & 1);
1955
        // Construct a sentinel group, identifying the start of the list.
1956
10.3k
        entries[SENTINEL].prev_group = SENTINEL;
1957
10.3k
        Assume(entries[SENTINEL].feerate.IsEmpty());
1958
1959
        // Iterate over all elements in the existing linearization.
1960
147k
        for (DepGraphIndex i = 0; i < linearization.size(); ++i) {
1961
            // Even passes are from back to front; odd passes from front to back.
1962
137k
            DepGraphIndex idx = linearization[rev ? linearization.size() - 1 - i : i];
1963
            // Construct a new group containing just idx. In even passes, the meaning of
1964
            // parent/child and high/low feerate are swapped.
1965
137k
            DepGraphIndex cur_group = idx + 1;
1966
137k
            entries[cur_group].group = SetType::Singleton(idx);
1967
137k
            entries[cur_group].deps = rev ? depgraph.Descendants(idx): depgraph.Ancestors(idx);
1968
137k
            entries[cur_group].feerate = depgraph.FeeRate(idx);
1969
137k
            if (rev) entries[cur_group].feerate.fee = -entries[cur_group].feerate.fee;
1970
137k
            entries[cur_group].prev_tx = NO_PREV_TX; // No previous transaction in group.
1971
137k
            entries[cur_group].first_tx = cur_group; // Transaction itself is first of group.
1972
            // Insert the new group at the back of the groups linked list.
1973
137k
            entries[cur_group].prev_group = entries[SENTINEL].prev_group;
1974
137k
            entries[SENTINEL].prev_group = cur_group;
1975
1976
            // Start merge/swap cycle.
1977
137k
            DepGraphIndex next_group = SENTINEL; // We inserted at the end, so next group is sentinel.
1978
137k
            DepGraphIndex prev_group = entries[cur_group].prev_group;
1979
            // Continue as long as the current group has higher feerate than the previous one.
1980
152k
            while (ByRatio{entries[cur_group].feerate} > ByRatio{entries[prev_group].feerate}) {
1981
                // prev_group/cur_group/next_group refer to (the last transactions of) 3
1982
                // consecutive entries in groups list.
1983
14.9k
                Assume(cur_group == entries[next_group].prev_group);
1984
14.9k
                Assume(prev_group == entries[cur_group].prev_group);
1985
                // The sentinel has empty feerate, which is neither higher or lower than other
1986
                // feerates. Thus, the while loop we are in here guarantees that cur_group and
1987
                // prev_group are not the sentinel.
1988
14.9k
                Assume(cur_group != SENTINEL);
1989
14.9k
                Assume(prev_group != SENTINEL);
1990
14.9k
                if (entries[cur_group].deps.Overlaps(entries[prev_group].group)) {
1991
                    // There is a dependency between cur_group and prev_group; merge prev_group
1992
                    // into cur_group. The group/deps/feerate fields of prev_group remain unchanged
1993
                    // but become unused.
1994
14.9k
                    entries[cur_group].group |= entries[prev_group].group;
1995
14.9k
                    entries[cur_group].deps |= entries[prev_group].deps;
1996
14.9k
                    entries[cur_group].feerate += entries[prev_group].feerate;
1997
                    // Make the first of the current group point to the tail of the previous group.
1998
14.9k
                    entries[entries[cur_group].first_tx].prev_tx = prev_group;
1999
                    // The first of the previous group becomes the first of the newly-merged group.
2000
14.9k
                    entries[cur_group].first_tx = entries[prev_group].first_tx;
2001
                    // The previous group becomes whatever group was before the former one.
2002
14.9k
                    prev_group = entries[prev_group].prev_group;
2003
14.9k
                    entries[cur_group].prev_group = prev_group;
2004
14.9k
                } else {
2005
                    // There is no dependency between cur_group and prev_group; swap them.
2006
0
                    DepGraphIndex preprev_group = entries[prev_group].prev_group;
2007
                    // If PP, P, C, N were the old preprev, prev, cur, next groups, then the new
2008
                    // layout becomes [PP, C, P, N]. Update prev_groups to reflect that order.
2009
0
                    entries[next_group].prev_group = prev_group;
2010
0
                    entries[prev_group].prev_group = cur_group;
2011
0
                    entries[cur_group].prev_group = preprev_group;
2012
                    // The current group remains the same, but the groups before/after it have
2013
                    // changed.
2014
0
                    next_group = prev_group;
2015
0
                    prev_group = preprev_group;
2016
0
                }
2017
14.9k
            }
2018
137k
        }
2019
2020
        // Convert the entries back to linearization (overwriting the existing one).
2021
10.3k
        DepGraphIndex cur_group = entries[0].prev_group;
2022
10.3k
        DepGraphIndex done = 0;
2023
132k
        while (cur_group != SENTINEL) {
2024
122k
            DepGraphIndex cur_tx = cur_group;
2025
            // Traverse the transactions of cur_group (from back to front), and write them in the
2026
            // same order during odd passes, and reversed (front to back) in even passes.
2027
122k
            if (rev) {
2028
68.7k
                do {
2029
68.7k
                    *(linearization.begin() + (done++)) = cur_tx - 1;
2030
68.7k
                    cur_tx = entries[cur_tx].prev_tx;
2031
68.7k
                } while (cur_tx != NO_PREV_TX);
2032
61.2k
            } else {
2033
68.7k
                do {
2034
68.7k
                    *(linearization.end() - (++done)) = cur_tx - 1;
2035
68.7k
                    cur_tx = entries[cur_tx].prev_tx;
2036
68.7k
                } while (cur_tx != NO_PREV_TX);
2037
61.2k
            }
2038
122k
            cur_group = entries[cur_group].prev_group;
2039
122k
        }
2040
10.3k
        Assume(done == linearization.size());
2041
10.3k
    }
2042
5.18k
}
2043
2044
} // namespace cluster_linearize
2045
2046
#endif // BITCOIN_CLUSTER_LINEARIZE_H