Skip to content

Commit 451be19

Browse files
committed
opt: Skip evaluation of equivalent input sets
When two successive UTXOs match in effective value and weight, we can skip the second if the prior is not selected: adding it would create an equivalent input set to a previously evaluated. E.g. if we have three UTXOs with effective values {5, 3, 3} of the same weight each, we want to evaluate {5, _, _}, {5, 3, _}, {5, 3, 3}, {_, 3, _}, {_, 3, 3}, but skip {5, _, 3}, and {_, _, 3}, because the first 3 is not selected, and we therefore do not need to evaluate the second 3 at the same position in the input set. If we reach the end of the branch, we must SHIFT the previously selected UTXO group instead.
1 parent 407b1e3 commit 451be19

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/wallet/coinselection.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,9 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
401401
};
402402

403403
SelectionResult result(selection_target, SelectionAlgorithm::CG);
404+
bool is_done = false;
404405
size_t curr_try = 0;
405-
while (true) {
406+
while (!is_done) {
406407
bool should_shift{false}, should_cut{false};
407408
// Select `next_utxo`
408409
OutputGroup& utxo = utxo_pool[next_utxo];
@@ -454,16 +455,32 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
454455
should_shift = true;
455456
}
456457

457-
if (should_shift) {
458+
while (should_shift) {
458459
// Set `next_utxo` to one after last selected, then deselect last selected UTXO
459460
if (curr_selection.empty()) {
460461
// Exhausted search space before running into attempt limit
462+
is_done = true;
461463
result.SetAlgoCompleted(true);
462464
break;
463465
}
464466
next_utxo = curr_selection.back() + 1;
465467
deselect_last();
466468
should_shift = false;
469+
470+
// After SHIFTing to an omission branch, the `next_utxo` might have the same value and same weight as the
471+
// UTXO we just omitted (i.e. it is a "clone"). If so, selecting `next_utxo` would produce an equivalent
472+
// selection as one we previously evaluated. In that case, increment `next_utxo` until we find a UTXO with a
473+
// differing amount or weight.
474+
while (utxo_pool[next_utxo - 1].GetSelectionAmount() == utxo_pool[next_utxo].GetSelectionAmount()
475+
&& utxo_pool[next_utxo - 1].m_weight == utxo_pool[next_utxo].m_weight) {
476+
if (next_utxo >= utxo_pool.size() - 1) {
477+
// Reached end of UTXO pool skipping clones: SHIFT instead
478+
should_shift = true;
479+
break;
480+
}
481+
// Skip clone: previous UTXO is equivalent and unselected
482+
++next_utxo;
483+
}
467484
}
468485
}
469486

src/wallet/test/coinselector_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
11801180
});
11811181
BOOST_CHECK(res);
11821182
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
1183-
size_t expected_attempts = 100'000;
1183+
size_t expected_attempts = 184;
11841184
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
11851185
}
11861186

@@ -1202,7 +1202,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
12021202
add_coin(1 * COIN, 2, expected_result);
12031203
BOOST_CHECK(EquivalentResult(expected_result, *res));
12041204
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
1205-
size_t expected_attempts = 4;
1205+
size_t expected_attempts = 3;
12061206
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12071207
}
12081208

@@ -1271,7 +1271,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests)
12711271
BOOST_CHECK(EquivalentResult(expected_result, *res));
12721272
// Demonstrate how following improvements reduce iteration count and catch any regressions in the future.
12731273
// If this takes more attempts, the implementation has regressed
1274-
size_t expected_attempts = 82'407;
1274+
size_t expected_attempts = 43;
12751275
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
12761276
}
12771277

0 commit comments

Comments
 (0)