Skip to content

Conversation

Empact
Copy link
Contributor

@Empact Empact commented May 13, 2018

This is prompted by #13167 and presented as a friendly alternative to it.

IMO you can improve code readability and performance by about 20% by tracking the selected utxos by index, rather than by position. This reduces the storage access complexity from roughly O(utxo_size) to O(selection_size).

On my machine (median of 5 trials):

BnBExhaustion, 5, 650, 2.2564, 0.000672999, 0.000711565, 0.000693112 - master
BnBExhaustion, 5, 650, 1.76232, 0.000528563, 0.000568806, 0.000539147 - this PR

@Empact
Copy link
Contributor Author

Empact commented May 14, 2018

/cc @achow101 this is the last of my BnB series

@fanquake fanquake requested a review from achow101 May 14, 2018 06:21
@Empact
Copy link
Contributor Author

Empact commented Jul 23, 2018

Rebased for #13691

@Empact Empact force-pushed the select-bnb-by-index branch from 514e49e to 109ca2e Compare July 23, 2018 01:32
@Empact
Copy link
Contributor Author

Empact commented Jul 23, 2018

Dropped the second commit as insignificant, updated the description / benchmarks.

@Empact Empact force-pushed the select-bnb-by-index branch from 109ca2e to 4c24e8e Compare July 30, 2018 00:13
@Empact Empact force-pushed the select-bnb-by-index branch from 4c24e8e to c95aa43 Compare October 8, 2018 04:59
@Empact
Copy link
Contributor Author

Empact commented May 28, 2019

Closing this given #13167 is closed, and optimizing coin selection perf isn't much of a priority.

@Empact Empact closed this May 28, 2019
@Empact
Copy link
Contributor Author

Empact commented Jun 3, 2021

@xekyo take a look

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 7, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

Copy link
Contributor

@murchandamus murchandamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 7714444

Regarding the commit message, AFAIU the complexity reduction applies to the vector of the selection index and the assembly of the best_selection. I'm surprised that's good for such a speedup. Am I missing something why this is so much faster?

Comment on lines 139 to 145
if (curr_selection.empty() ||
// The previous index is included and therefore not relevant for exclusion shortcut
(curr_index - 1) == curr_selection.back() ||
utxo.GetSelectionAmount() != utxo_pool.at(curr_index - 1).GetSelectionAmount() ||
utxo.fee != utxo_pool.at(curr_index - 1).fee) {
// Inclusion branch first (Largest First Exploration)
curr_selection.push_back(true);
curr_selection.push_back(curr_index);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a bit to verify whether this is equivalent, but this does look correct to me.

Optional: The comment about "fee to long term fee" threw me off at first, perhaps it would actually be clearer to drop the second sentence of the comment before the if-block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - as an aid to the reviewer, I'm applying De Morgan's law here.

I left the comment but moved it to above the relevant check.

@murchandamus
Copy link
Contributor

Hi @Empact, are you still making progress on this, or would you be open to the idea of someone else adopting the PR? I was just considering amending my prior commentary after not looking at this for a while, but I would be happy to take a look at the failing check instead, if you aren't working on this anymore.

@Empact
Copy link
Contributor Author

Empact commented Aug 28, 2021

Hey Mark, yes I'd like to pick it back up again - have been otherwise engaged for a while but I expect to have time next week.

@Empact Empact force-pushed the select-bnb-by-index branch 3 times, most recently from 4a218f3 to e6b45f3 Compare February 28, 2022 12:39
@achow101
Copy link
Member

achow101 commented Mar 2, 2022

ACK e6b45f3

@Empact Empact requested a review from practicalswift March 2, 2022 16:55
@fanquake fanquake requested a review from glozow March 10, 2022 13:51
@@ -67,7 +67,7 @@ std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_poo
SelectionResult result(selection_target);
CAmount curr_value = 0;

std::vector<bool> curr_selection; // select the utxo at this index
std::vector<size_t> curr_selection; // selected utxo indexes
curr_selection.reserve(utxo_pool.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in cec31e0:

Given that a big motivation here is to not allocate memory for every UTXO, we should probably remove this reserve(utxo_pool.size()) line

@achow101 achow101 requested a review from murchandamus March 11, 2022 15:02
Empact added 3 commits March 14, 2022 12:19
This is a performance optimization - rather than track all visited values
in a bool vector, track the selected index in a vector. This results in a
complexity reduction of O(utxo_size) to O(selection_size).
Clarifies purpose and removes name collisions with other indicies.
@Empact Empact force-pushed the select-bnb-by-index branch from e6b45f3 to 9d20052 Compare March 14, 2022 12:21
Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code review ACK 9d20052

My bench results on master:

ns/op op/s err% total benchmark
2,468,488.00 405.11 0.5% 0.03 BnBExhaustion

on this branch:

ns/op op/s err% total benchmark
1,396,491.00 716.08 0.4% 0.02 BnBExhaustion

@achow101
Copy link
Member

achow101 commented Mar 18, 2022

reACK 9d20052

Copy link
Contributor

@murchandamus murchandamus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reACK 9d20052

@achow101 achow101 merged commit e66630c into bitcoin:master Mar 21, 2022
@Empact Empact deleted the select-bnb-by-index branch April 15, 2022 18:42
@bitcoin bitcoin locked and limited conversation to collaborators Apr 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants