refactor: improve complexity of removing preselected coins#24814
refactor: improve complexity of removing preselected coins#24814rag-hav wants to merge 1 commit intobitcoin:masterfrom
Conversation
src/wallet/spend.cpp
Outdated
There was a problem hiding this comment.
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.
mzumsande
left a comment
There was a problem hiding this comment.
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.