Skip to content

Commit

Permalink
opt: Track remaining effective_value in lookahead
Browse files Browse the repository at this point in the history
Introduces a dedicated data structure to track the total
effective_value available in the remaining UTXOs at each index of the
UTXO pool. In contrast to the approach in BnB, this allows us to
immediately jump to a lower index instead of visiting every UTXO to add
back their eff_value to the lookahead.
  • Loading branch information
murchandamus committed Feb 9, 2024
1 parent 5f84f3c commit 407b1e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,17 @@ util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool
util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, CAmount change_target, int max_weight)
{
std::sort(utxo_pool.begin(), utxo_pool.end(), descending);
// The sum of UTXO amounts after this UTXO index, e.g. lookahead[5] = Σ(UTXO[6+].amount)
std::vector<CAmount> lookahead(utxo_pool.size());

// Check that there are sufficient funds
// Calculate lookahead values, and check that there are sufficient funds
CAmount total_available = 0;
for (const OutputGroup& utxo : utxo_pool) {
// Assert UTXOs with non-positive effective value have been filtered
Assume(utxo.GetSelectionAmount() > 0);
total_available += utxo.GetSelectionAmount();
for (size_t i = 0; i < utxo_pool.size(); ++i) {
size_t index = utxo_pool.size() - 1 - i; // Loop over every element in reverse order
lookahead[index] = total_available;
// UTXOs with non-positive effective value must have been filtered
Assume(utxo_pool[index].GetSelectionAmount() > 0);
total_available += utxo_pool[index].GetSelectionAmount();
}

const CAmount total_target = selection_target + change_target;
Expand Down Expand Up @@ -409,7 +413,10 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
++curr_try;

// EVALUATE current selection: check for solutions and see whether we can CUT or SHIFT before EXPLORING further
if (curr_weight > max_weight) {
if (curr_amount + lookahead[curr_selection.back()] < total_target) {
// Insufficient funds with lookahead: CUT
should_cut = true;
} else if (curr_weight > max_weight) {
// max_weight exceeded: SHIFT
max_tx_weight_exceeded = true;
should_shift = true;
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/test/coinselector_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 525;
size_t expected_attempts = 281;
BOOST_CHECK_MESSAGE(res->GetSelectionsEvaluated() == expected_attempts, strprintf("Expected %i attempts, but got %i", expected_attempts, res->GetSelectionsEvaluated()));
}

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

Expand Down

0 comments on commit 407b1e3

Please sign in to comment.