Skip to content

Commit e96b00d

Browse files
committed
txgraph: make number of acceptable iterations configurable (feature)
1 parent cfe9958 commit e96b00d

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

src/bench/txgraph.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ void BenchTxGraphTrim(benchmark::Bench& bench)
4646
static constexpr int NUM_DEPS_PER_BOTTOM_TX = 100;
4747
/** Set a very large cluster size limit so that only the count limit is triggered. */
4848
static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
49+
/** Set a very high number for acceptable iterations, so that we certainly benchmark optimal
50+
* linearization. */
51+
static constexpr uint64_t NUM_ACCEPTABLE_ITERS = 100'000'000;
4952

5053
/** Refs to all top transactions. */
5154
std::vector<TxGraph::Ref> top_refs;
@@ -57,7 +60,7 @@ void BenchTxGraphTrim(benchmark::Bench& bench)
5760
std::vector<size_t> top_components;
5861

5962
InsecureRandomContext rng(11);
60-
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE);
63+
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS);
6164

6265
// Construct the top chains.
6366
for (int chain = 0; chain < NUM_TOP_CHAINS; ++chain) {

src/test/fuzz/txgraph.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,11 @@ FUZZ_TARGET(txgraph)
309309
auto max_cluster_count = provider.ConsumeIntegralInRange<DepGraphIndex>(1, MAX_CLUSTER_COUNT_LIMIT);
310310
/** The maximum total size of transactions in a (non-oversized) cluster. */
311311
auto max_cluster_size = provider.ConsumeIntegralInRange<uint64_t>(1, 0x3fffff * MAX_CLUSTER_COUNT_LIMIT);
312+
/** The number of iterations to consider a cluster acceptably linearized. */
313+
auto acceptable_iters = provider.ConsumeIntegralInRange<uint64_t>(0, 10000);
312314

313315
// Construct a real graph, and a vector of simulated graphs (main, and possibly staging).
314-
auto real = MakeTxGraph(max_cluster_count, max_cluster_size);
316+
auto real = MakeTxGraph(max_cluster_count, max_cluster_size, acceptable_iters);
315317
std::vector<SimTxGraph> sims;
316318
sims.reserve(2);
317319
sims.emplace_back(max_cluster_count, max_cluster_size);

src/test/txgraph_tests.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
BOOST_AUTO_TEST_SUITE(txgraph_tests)
1515

16+
/** The number used as acceptable_iters argument in these tests. High enough that everything
17+
* should be optimal, always. */
18+
static constexpr uint64_t NUM_ACCEPTABLE_ITERS = 100'000'000;
19+
1620
BOOST_AUTO_TEST_CASE(txgraph_trim_zigzag)
1721
{
1822
// T T T T T T T T T T T T T T (50 T's)
@@ -35,7 +39,7 @@ BOOST_AUTO_TEST_CASE(txgraph_trim_zigzag)
3539
static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
3640

3741
// Create a new graph for the test.
38-
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE);
42+
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS);
3943

4044
// Add all transactions and store their Refs.
4145
std::vector<TxGraph::Ref> refs;
@@ -98,7 +102,7 @@ BOOST_AUTO_TEST_CASE(txgraph_trim_flower)
98102
/** Set a very large cluster size limit so that only the count limit is triggered. */
99103
static constexpr int32_t MAX_CLUSTER_SIZE = 100'000 * 100;
100104

101-
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE);
105+
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS);
102106

103107
// Add all transactions and store their Refs.
104108
std::vector<TxGraph::Ref> refs;
@@ -184,7 +188,7 @@ BOOST_AUTO_TEST_CASE(txgraph_trim_huge)
184188
std::vector<size_t> top_components;
185189

186190
FastRandomContext rng;
187-
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE);
191+
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS);
188192

189193
// Construct the top chains.
190194
for (int chain = 0; chain < NUM_TOP_CHAINS; ++chain) {
@@ -256,7 +260,7 @@ BOOST_AUTO_TEST_CASE(txgraph_trim_big_singletons)
256260
static constexpr int NUM_TOTAL_TX = 100;
257261

258262
// Create a new graph for the test.
259-
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE);
263+
auto graph = MakeTxGraph(MAX_CLUSTER_COUNT, MAX_CLUSTER_SIZE, NUM_ACCEPTABLE_ITERS);
260264

261265
// Add all transactions and store their Refs.
262266
std::vector<TxGraph::Ref> refs;

src/txgraph.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ class TxGraphImpl final : public TxGraph
255255
const DepGraphIndex m_max_cluster_count;
256256
/** This TxGraphImpl's maximum cluster size limit. */
257257
const uint64_t m_max_cluster_size;
258+
/** The number of linearization improvement steps needed per cluster to be considered
259+
* acceptable. */
260+
const uint64_t m_acceptable_iters;
258261

259262
/** Information about one group of Clusters to be merged. */
260263
struct GroupEntry
@@ -456,9 +459,10 @@ class TxGraphImpl final : public TxGraph
456459

457460
public:
458461
/** Construct a new TxGraphImpl with the specified limits. */
459-
explicit TxGraphImpl(DepGraphIndex max_cluster_count, uint64_t max_cluster_size) noexcept :
462+
explicit TxGraphImpl(DepGraphIndex max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept :
460463
m_max_cluster_count(max_cluster_count),
461464
m_max_cluster_size(max_cluster_size),
465+
m_acceptable_iters(acceptable_iters),
462466
m_main_chunkindex(ChunkOrder(this))
463467
{
464468
Assume(max_cluster_count >= 1);
@@ -1677,7 +1681,7 @@ void TxGraphImpl::MakeAcceptable(Cluster& cluster) noexcept
16771681
{
16781682
// Relinearize the Cluster if needed.
16791683
if (!cluster.NeedsSplitting() && !cluster.IsAcceptable() && !cluster.IsOversized()) {
1680-
cluster.Relinearize(*this, 10000);
1684+
cluster.Relinearize(*this, m_acceptable_iters);
16811685
}
16821686
}
16831687

@@ -2892,7 +2896,7 @@ TxGraph::Ref::Ref(Ref&& other) noexcept
28922896
std::swap(m_index, other.m_index);
28932897
}
28942898

2895-
std::unique_ptr<TxGraph> MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size) noexcept
2899+
std::unique_ptr<TxGraph> MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept
28962900
{
2897-
return std::make_unique<TxGraphImpl>(max_cluster_count, max_cluster_size);
2901+
return std::make_unique<TxGraphImpl>(max_cluster_count, max_cluster_size, acceptable_iters);
28982902
}

src/txgraph.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ class TxGraph
247247

248248
/** Construct a new TxGraph with the specified limit on the number of transactions within a cluster,
249249
* and on the sum of transaction sizes within a cluster. max_cluster_count cannot exceed
250-
* MAX_CLUSTER_COUNT_LIMIT. */
251-
std::unique_ptr<TxGraph> MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size) noexcept;
250+
* MAX_CLUSTER_COUNT_LIMIT. acceptable_iters controls how many linearization optimization
251+
* steps will be performed per cluster before they are considered to be of acceptable quality. */
252+
std::unique_ptr<TxGraph> MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept;
252253

253254
#endif // BITCOIN_TXGRAPH_H

0 commit comments

Comments
 (0)