aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoão Barbosa <joao.paulo.barbosa@gmail.com>2019-02-22 16:07:21 +0000
committerJoão Barbosa <joao.paulo.barbosa@gmail.com>2019-04-22 10:00:07 +0100
commit710a7136f93133bf256d37dc8c8faf5a6b9ba89d (patch)
tree9f333f213a8c3303cf4e102e1973fc135e17820e /src
parenta094b543324267e6d956bb0df2ede990b90118cb (diff)
downloadbitcoin-710a7136f93133bf256d37dc8c8faf5a6b9ba89d.tar.xz
rpc: Speedup getaddressesbylabel
Diffstat (limited to 'src')
-rw-r--r--src/wallet/rpcwallet.cpp13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
index 37fc88dfd5..0fd4527e59 100644
--- a/src/wallet/rpcwallet.cpp
+++ b/src/wallet/rpcwallet.cpp
@@ -3790,9 +3790,20 @@ static UniValue getaddressesbylabel(const JSONRPCRequest& request)
// Find all addresses that have the given label
UniValue ret(UniValue::VOBJ);
+ std::set<std::string> addresses;
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
if (item.second.name == label) {
- ret.pushKV(EncodeDestination(item.first), AddressBookDataToJSON(item.second, false));
+ std::string address = EncodeDestination(item.first);
+ // CWallet::mapAddressBook is not expected to contain duplicate
+ // address strings, but build a separate set as a precaution just in
+ // case it does.
+ bool unique = addresses.emplace(address).second;
+ assert(unique);
+ // UniValue::pushKV checks if the key exists in O(N)
+ // and since duplicate addresses are unexpected (checked with
+ // std::set in O(log(N))), UniValue::__pushKV is used instead,
+ // which currently is O(1).
+ ret.__pushKV(address, AddressBookDataToJSON(item.second, false));
}
}