Skip to content

Commit cd14d21

Browse files
author
MarcoFalke
committed
Merge #15463: rpc: Speedup getaddressesbylabel
710a713 rpc: Speedup getaddressesbylabel (João Barbosa) Pull request description: Fixes #15447. Same approach of #14984, this change avoids duplicate key check when building the JSON response in memory. ACKs for commit 710a71: MarcoFalke: utACK 710a713 ryanofsky: utACK 710a713. Just new comments and assert since last review. Tree-SHA512: 77c95df9ff3793e348619aa070e6fd36df9da1b461d708ab146652cb3699f1a472ef6eb38dafdb8374375cbc97daef07635fcb0501961f167a023309513742e2
2 parents 2d5419f + 710a713 commit cd14d21

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3683,9 +3683,20 @@ static UniValue getaddressesbylabel(const JSONRPCRequest& request)
36833683

36843684
// Find all addresses that have the given label
36853685
UniValue ret(UniValue::VOBJ);
3686+
std::set<std::string> addresses;
36863687
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
36873688
if (item.second.name == label) {
3688-
ret.pushKV(EncodeDestination(item.first), AddressBookDataToJSON(item.second, false));
3689+
std::string address = EncodeDestination(item.first);
3690+
// CWallet::mapAddressBook is not expected to contain duplicate
3691+
// address strings, but build a separate set as a precaution just in
3692+
// case it does.
3693+
bool unique = addresses.emplace(address).second;
3694+
assert(unique);
3695+
// UniValue::pushKV checks if the key exists in O(N)
3696+
// and since duplicate addresses are unexpected (checked with
3697+
// std::set in O(log(N))), UniValue::__pushKV is used instead,
3698+
// which currently is O(1).
3699+
ret.__pushKV(address, AddressBookDataToJSON(item.second, false));
36893700
}
36903701
}
36913702

0 commit comments

Comments
 (0)