Skip to content

Commit 710a713

Browse files
committed
rpc: Speedup getaddressesbylabel
1 parent a094b54 commit 710a713

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
@@ -3790,9 +3790,20 @@ static UniValue getaddressesbylabel(const JSONRPCRequest& request)
37903790

37913791
// Find all addresses that have the given label
37923792
UniValue ret(UniValue::VOBJ);
3793+
std::set<std::string> addresses;
37933794
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
37943795
if (item.second.name == label) {
3795-
ret.pushKV(EncodeDestination(item.first), AddressBookDataToJSON(item.second, false));
3796+
std::string address = EncodeDestination(item.first);
3797+
// CWallet::mapAddressBook is not expected to contain duplicate
3798+
// address strings, but build a separate set as a precaution just in
3799+
// case it does.
3800+
bool unique = addresses.emplace(address).second;
3801+
assert(unique);
3802+
// UniValue::pushKV checks if the key exists in O(N)
3803+
// and since duplicate addresses are unexpected (checked with
3804+
// std::set in O(log(N))), UniValue::__pushKV is used instead,
3805+
// which currently is O(1).
3806+
ret.__pushKV(address, AddressBookDataToJSON(item.second, false));
37963807
}
37973808
}
37983809

0 commit comments

Comments
 (0)