clusterlin: permit passing in existing linearization to Linearize

This implements the LIMO algorithm for linearizing by improving an existing
linearization. See
https://delvingbitcoin.org/t/limo-combining-the-best-parts-of-linearization-search-and-merging
for details.
This commit is contained in:
Pieter Wuille
2024-05-09 09:02:18 -04:00
parent 97d98718b0
commit 28549791b3
3 changed files with 53 additions and 9 deletions

View File

@@ -143,8 +143,9 @@ public:
/** A simple linearization algorithm.
*
* This matches Linearize() in interface and behavior, though with fewer optimizations, and using
* just SimpleCandidateFinder rather than AncestorCandidateFinder and SearchCandidateFinder.
* This matches Linearize() in interface and behavior, though with fewer optimizations, lacking
* the ability to pass in an existing linearization, and using just SimpleCandidateFinder rather
* than AncestorCandidateFinder and SearchCandidateFinder.
*/
template<typename SetType>
std::pair<std::vector<ClusterIndex>, bool> SimpleLinearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations)
@@ -614,12 +615,32 @@ FUZZ_TARGET(clusterlin_linearize)
reader >> VARINT(iter_count) >> Using<DepGraphFormatter>(depgraph) >> rng_seed;
} catch (const std::ios_base::failure&) {}
// Optionally construct an old linearization for it.
std::vector<ClusterIndex> old_linearization;
{
uint8_t have_old_linearization{0};
try {
reader >> have_old_linearization;
} catch(const std::ios_base::failure&) {}
if (have_old_linearization & 1) {
old_linearization = ReadLinearization(depgraph, reader);
SanityCheck(depgraph, old_linearization);
}
}
// Invoke Linearize().
iter_count &= 0x7ffff;
auto [linearization, optimal] = Linearize(depgraph, iter_count, rng_seed);
auto [linearization, optimal] = Linearize(depgraph, iter_count, rng_seed, old_linearization);
SanityCheck(depgraph, linearization);
auto chunking = ChunkLinearization(depgraph, linearization);
// Linearization must always be as good as the old one, if provided.
if (!old_linearization.empty()) {
auto old_chunking = ChunkLinearization(depgraph, old_linearization);
auto cmp = CompareChunks(chunking, old_chunking);
assert(cmp >= 0);
}
// If the iteration count is sufficiently high, an optimal linearization must be found.
// Each linearization step can use up to 2^k iterations, with steps k=1..n. That sum is
// 2 * (2^n - 1)