From 5f84f3cc043c5fb15072f5072fee752eaa01a2ec Mon Sep 17 00:00:00 2001 From: Murch Date: Mon, 8 Jan 2024 15:26:21 -0500 Subject: [PATCH] opt: Skip branches with worse weight Once we exceed the weight of the current best selection, we can always shift as adding more inputs can never yield a better solution. --- src/wallet/coinselection.cpp | 3 +++ src/wallet/test/coinselector_tests.cpp | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index abd710d56d..cf666e7459 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -413,6 +413,9 @@ util::Result CoinGrinder(std::vector& utxo_pool, c // max_weight exceeded: SHIFT max_tx_weight_exceeded = true; should_shift = true; + } else if (curr_weight > best_selection_weight) { + // Worse weight than best solution. More UTXOs only increase weight: SHIFT + should_shift = true; } else if (curr_amount >= total_target) { // Success, adding more weight cannot be better: SHIFT should_shift = true; diff --git a/src/wallet/test/coinselector_tests.cpp b/src/wallet/test/coinselector_tests.cpp index e23292d21f..fe9922e31c 100644 --- a/src/wallet/test/coinselector_tests.cpp +++ b/src/wallet/test/coinselector_tests.cpp @@ -1231,7 +1231,7 @@ BOOST_AUTO_TEST_CASE(coin_grinder_tests) add_coin(4 * COIN, 3, expected_result); BOOST_CHECK(EquivalentResult(expected_result, *res)); // Demonstrate how following improvements reduce iteration count and catch any regressions in the future. - size_t expected_attempts = 2041; + size_t expected_attempts = 525; BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated())); }