-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Improved readability of ApproximateBestSubset #7183
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
|
vValue is not sorted in ascending order, the whole algorithm wouldn't make sense if it was... |
|
@MarcoFalke: Sorry, apparently the links always refer to the current version and shift around because of that, however in As you can see |
It actually is. If you say this sorts it in descending order, I don't see where that happens. |
Right in the line where Line 1719 in 5dc63ed
|
@xekyo If you want to link on GitHub, don't forget to press the |
|
Yikes, that's easy to overlook, perhaps a comment. |
|
And a unit test because this PR should not pass travis in the first place. |
|
@xekyo Can you take a look at this and add a comment above the " |
Absolutely. I also had no idea that you could pass reverse iterators to std::sort and it would just work. |
|
@MarcoFalke: TIL. I was unaware of that behaviour of Instead of using reverse iterators, one could replace the line with these two It would become much more readable, wouldn’t need a comment to clarify, and perhaps perform even faster. |
|
Sounds ok to me. Are you planning to write the unit test as well? |
|
@MarcoFalke: I could do that, yet I’m unsure what one would want to test. Since the sorting operation is in the middle of |
b9ecfc7 to
047cf77
Compare
|
@MarcoFalke: Unfortunately, I also overwrote my commit when I rebased my branch (perhaps prematurely) and pushed it to my fork. Would it make sense to redo them and push them onto another branch? I’ve added a test. |
|
Provided by @xekyo on IRC: b9ecfc7 diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index d23d54e..3d1f58c 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -1605,7 +1605,7 @@ static void ApproximateBestSubset(vector<pair<CAmount, pair<const CWalletTx*,uns
bool fReachedTarget = false;
for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
{
- for (unsigned int i = 0; i < vValue.size(); i++)
+ for (unsigned int i = 0; i < vValue.size() && !fReachedTarget; i++)
{
//The solver here uses a randomized algorithm,
//the randomness serves no real security purpose but is just
@@ -1625,8 +1625,6 @@ static void ApproximateBestSubset(vector<pair<CAmount, pair<const CWalletTx*,uns
nBest = nTotal;
vfBest = vfIncluded;
}
- nTotal -= vValue[i].first;
- vfIncluded[i] = false;
}
}
} |
src/wallet/test/wallet_tests.cpp
Outdated
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.
This will select quite a bunch of the 3ct coins, I don't think you want this. Just reduce the number to 1 like #7293
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.
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.
I’ve ACKed #7293 and will remove the test case here.
c2e38d6 to
047cf77
Compare
|
utACK 047cf77e324817594ce58e0ff53e5d817d1a9bc1. Nit: you could use |
Future proofing added lines
047cf77 to
96efcad
Compare
|
Edited to include the nit. :) |
|
trivial re-ACK 96efcad |
|
utACK 96efcad |
96efcad Improved readability of sorting for coin selection. (Murch)
|
utACK 96efcad |
|
Is there some reason not to just invert the CompareValueOnly function? |
|
And call it ReverseCompareValueOnly, yes that would have been the other option. But let's leave it as it is now, there's no need to shedpaint this. |
96efcad Improved readability of sorting for coin selection. (Murch)
vValueis sorted into ascending order before ApproximateBestSubset(...) is called.Currently, after a new best set is found, the final element that was added gets removed from the set, and the inner loop continues to search through the remainder of
vValuefor a better set. It is however impossible for a better set to be achieved with a later element invValue: AsvValueis sorted in ascending order, any set formed with a later element will be of same size or bigger.Therefore, the lines 1628 & 1629 are obsolete, and the inner loop should also stop on
fReachedTarget.