All calls to mapWallet.count() have the intent to detect if a txid exists and most are followed by a second lookup to retrieve the CWalletTx.
This PR replaces all mapWallet.count() calls with mapWallet.find() to avoid the second lookup.
All calls to mapWallet.count() have the intent to detect if a txid exists and most are followed by a second lookup to retrieve the CWalletTx.
This PR replaces all mapWallet.count() calls with mapWallet.find() to avoid the second lookup.
Missing mapWallet[] cases.
General Concept ACK. I guess extending to cover mapWallet[] would make sense?
Yes already on it.
240 | @@ -241,12 +241,13 @@ bool CFeeBumper::commit(CWallet *pWallet) 241 | if (!vErrors.empty() || currentResult != BumpFeeResult::OK) { 242 | return false; 243 | } 244 | - if (txid.IsNull() || !pWallet->mapWallet.count(txid)) { 245 | + auto it = pWallet->mapWallet.find(txid); 246 | + if (txid.IsNull() || it == pWallet->mapWallet.end()) {
Maybe something for a followup commit, but I don't understand the txid IsNull check. If it's expected that there could be a null txid in mapWallet, there should be a comment explaining why that would happen, otherwise the check should be dropped or replaced with an assert or other error indicating that wallet state is corrupted.
@ryanofsky I guess it's to prevent an unnecessary count() in the old code. What about:
auto it = txid.IsNull() ? pWallet->mapWallet.end() : pWallet->mapWallet.find(txid);
if (it == pWallet->mapWallet.end()) {
Thread #11039 (review)
Good idea. I guess if the purpose of the IsNull call was to serve as a minor optimization, the intent is at still discernible, though it's also still unclear. And your new change would match previous behavior more closely, so that's good too.
I think ideally IsNull check would be dropped or actually explained, but that seems like something for a followup. Your new change would be good to add to this PR.
2012 | @@ -2011,7 +2013,7 @@ UniValue abandontransaction(const JSONRPCRequest& request) 2013 | uint256 hash; 2014 | hash.SetHex(request.params[0].get_str()); 2015 | 2016 | - if (!pwallet->mapWallet.count(hash)) { 2017 | + if (pwallet->mapWallet.find(hash) == pwallet->mapWallet.end()) {
Seems pointlessly verbose.
Yeah, and there is no second lookup here so..
1018 | @@ -1017,7 +1019,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, const CBlockI 1019 | } 1020 | } 1021 | 1022 | - bool fExisted = mapWallet.count(tx.GetHash()) != 0; 1023 | + bool fExisted = mapWallet.find(tx.GetHash()) != mapWallet.end();
Also unnecessarily verbose.
Yeah, and there is no second lookup here so..
utACK 8e1b843103235d5ded84af5d25ee1530da448d53
I guess extending to cover mapWallet[] would make sense?
Not sure specifically what is meant here, but #9680 replaces [] lookups with at() lookups to avoid inadvertently adding entries to the map when the txid doesn't exist.
@jonasschnelli leaving mapWallet[] cases out because of @ryanofsky suggestion above.
utACK 8f2f1e0458d263dc9b51caad0adc0246e3580114. Only changes since previous are what was discussed in comments.
1962 | + auto it = pwallet->mapWallet.find(hash); 1963 | + if (it == pwallet->mapWallet.end()) { 1964 | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid or non-wallet transaction id"); 1965 | } 1966 | - const CWalletTx& wtx = pwallet->mapWallet[hash]; 1967 | + const CWalletTx& wtx = it->second;
IMO also use this in the other cases you're using it->second.
utACK, with nit
utACK 8f2f1e0
This brought three new -Wshadow warnings:
CXX wallet/libbitcoin_wallet_a-wallet.o
wallet/wallet.cpp:1125:14: warning: declaration shadows a local variable [-Wshadow]
auto it = mapWallet.find(now);
^
wallet/wallet.cpp:1112:10: note: previous declaration is here
auto it = mapWallet.find(hashTx);
^
wallet/wallet.cpp:1152:22: warning: declaration shadows a local variable [-Wshadow]
auto it = mapWallet.find(txin.prevout.hash);
^
wallet/wallet.cpp:1125:14: note: previous declaration is here
auto it = mapWallet.find(now);
^
wallet/wallet.cpp:1215:22: warning: declaration shadows a local variable [-Wshadow]
auto it = mapWallet.find(txin.prevout.hash);
^
wallet/wallet.cpp:1193:14: note: previous declaration is here
auto it = mapWallet.find(now);
^
3 warnings generated.