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.
mapWallet[]
cases.
mapWallet[]
would make sense?
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()) {
@ryanofsky I guess it’s to prevent an unnecessary count()
in the old code. What about:
0auto it = txid.IsNull() ? pWallet->mapWallet.end() : pWallet->mapWallet.find(txid);
1if (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()) {
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();
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.
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;
it->second
.
This brought three new -Wshadow warnings:
0 CXX wallet/libbitcoin_wallet_a-wallet.o
1wallet/wallet.cpp:1125:14: warning: declaration shadows a local variable [-Wshadow]
2 auto it = mapWallet.find(now);
3 ^
4wallet/wallet.cpp:1112:10: note: previous declaration is here
5 auto it = mapWallet.find(hashTx);
6 ^
7wallet/wallet.cpp:1152:22: warning: declaration shadows a local variable [-Wshadow]
8 auto it = mapWallet.find(txin.prevout.hash);
9 ^
10wallet/wallet.cpp:1125:14: note: previous declaration is here
11 auto it = mapWallet.find(now);
12 ^
13wallet/wallet.cpp:1215:22: warning: declaration shadows a local variable [-Wshadow]
14 auto it = mapWallet.find(txin.prevout.hash);
15 ^
16wallet/wallet.cpp:1193:14: note: previous declaration is here
17 auto it = mapWallet.find(now);
18 ^
193 warnings generated.