mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-04 04:02:23 +02:00
Merge #15463: rpc: Speedup getaddressesbylabel
710a7136f9 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 710a7136f93133bf256d37dc8c8faf5a6b9ba89d ryanofsky: utACK 710a7136f93133bf256d37dc8c8faf5a6b9ba89d. Just new comments and assert since last review. Tree-SHA512: 77c95df9ff3793e348619aa070e6fd36df9da1b461d708ab146652cb3699f1a472ef6eb38dafdb8374375cbc97daef07635fcb0501961f167a023309513742e2
This commit is contained in:
commit
cd14d210c4
@ -3683,9 +3683,20 @@ static UniValue getaddressesbylabel(const JSONRPCRequest& request)
|
|||||||
|
|
||||||
// Find all addresses that have the given label
|
// Find all addresses that have the given label
|
||||||
UniValue ret(UniValue::VOBJ);
|
UniValue ret(UniValue::VOBJ);
|
||||||
|
std::set<std::string> addresses;
|
||||||
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
|
for (const std::pair<const CTxDestination, CAddressBookData>& item : pwallet->mapAddressBook) {
|
||||||
if (item.second.name == label) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user