-
Notifications
You must be signed in to change notification settings - Fork 37.7k
refactor: improve complexity of removing preselected coins #24814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK on replacing O(n) erase with O(1) operations push_back and swap, provided the difference is meaningful (a benchmark comparison might be helpful).
src/wallet/spend.cpp
Outdated
if (!preset_coins.count(it->outpoint)) | ||
vCoins_filtered.push_back(*it); | ||
} | ||
swap(vCoins_filtered, vCoins); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggested diff per clang-format and guidelines in doc/developer-notes.md
- if(coin_control.HasSelected()){
+ if (coin_control.HasSelected()) {
std::vector<COutput> vCoins_filtered;
- for (std::vector<COutput>::iterator it = vCoins.begin(); it != vCoins.end(); it++)
- {
- if (!preset_coins.count(it->outpoint))
+ for (std::vector<COutput>::iterator it = vCoins.begin(); it != vCoins.end(); ++it) {
+ if (!preset_coins.count(it->outpoint)) {
vCoins_filtered.push_back(*it);
+ }
edit: unsure if std::swap(vCoins_filtered, vCoins)
or vCoins_filtered.swap(vCoins)
would be preferred to swap(vCoins_filtered, vCoins)
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made these changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be solved easier with the erase-remove idiom so you don't need to swap with a second vector?
Or we could wait until C++20 to use constant-time |
Concept ACK |
986b355
to
236011b
Compare
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
I am not sure what I am supposed to do right now. Do I wait for more reviewers? |
Haven't gotten back yet as there are many PRs to review; my suggestions would be the last entry of this section |
If you wanted to use the remove-erase idiom, you should push that over this PR's branch. |
I am not sure if the remove-erase idiom is better, it saves memory but is harder to read as well. I prefer the current push_back and swap approach. |
Just FYI, I think it could be quite short like
(didn't test this though) |
As @jonatack suggested, another option is to wait until C++20. It does both: saves memory and is easier to read. |
I would also prefer the erase-remove idiom as I think it's clearer as to what is actually happening (although perhaps having both erase and remove is a little bit confusing, but they're both clear that something is being deleted). |
The logic makes sense and all the tests are running fine in my system on applying your suggested code changes. For people not accustomed with |
🐙 This pull request conflicts with the target branch and needs rebase. Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a "draft". |
Looks like this can be closed, #24584 changed this spot to use the remove-erase idiom. |
Worth to mention that #25685 completely removes the need to erase the pre-selected coins from the available coins vector by not including them there in the first place. |
Improve the complexity of removing pre-selected coins from the pool to linear from quadratic.