Skip to content

Commit f811a24

Browse files
committed
wallet: cache descriptor ID to avoid repeated descriptor string creation
Right now a wallet descriptor is converted to it's string representation (via `Descriptor::ToString`) repeatedly at different instances: - on finding a `DescriptorScriptPubKeyMan` for a given descriptor (`CWallet::GetDescriptorScriptPubKeyMan`, e.g. used by the `importdescriptors` RPC); the string representation is created once for each spkm in the wallet and at each iteration again for the searched descriptor (`DescriptorScriptPubKeyMan::HasWalletDescriptor`) - whenever `DescriptorScriptPubKeyMan::GetID()` is called, e.g. in `TopUp` or any instances where a descriptor is written to the DB to determine the database key etc. As there is no good reason to calculate a fixed descriptor's string/ID more than once, add the ID as a field to `WalletDescriptor` and calculate it immediately at initialization (or deserialization). `HasWalletDescriptor` is changed to compare the spkm's and searched descriptor's ID instead of the string to take use of that. This speeds up the functional test `wallet_miniscript.py` by a factor of 5-6x on my machine (3m30.95s on master vs. 0m38.02s on PR). The recently introduced "max-size TapMiniscript" test-case introduced a descriptor that takes 2-3 seconds to create a string representation, so the repeated calls to that were significantly hurting the performance.
1 parent 953d302 commit f811a24

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,7 +2601,7 @@ std::unique_ptr<CKeyMetadata> DescriptorScriptPubKeyMan::GetMetadata(const CTxDe
26012601
uint256 DescriptorScriptPubKeyMan::GetID() const
26022602
{
26032603
LOCK(cs_desc_man);
2604-
return DescriptorID(*m_wallet_descriptor.descriptor);
2604+
return m_wallet_descriptor.id;
26052605
}
26062606

26072607
void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)
@@ -2655,7 +2655,7 @@ bool DescriptorScriptPubKeyMan::AddCryptedKey(const CKeyID& key_id, const CPubKe
26552655
bool DescriptorScriptPubKeyMan::HasWalletDescriptor(const WalletDescriptor& desc) const
26562656
{
26572657
LOCK(cs_desc_man);
2658-
return m_wallet_descriptor.descriptor != nullptr && desc.descriptor != nullptr && m_wallet_descriptor.descriptor->ToString() == desc.descriptor->ToString();
2658+
return !m_wallet_descriptor.id.IsNull() && !desc.id.IsNull() && m_wallet_descriptor.id == desc.id;
26592659
}
26602660

26612661
void DescriptorScriptPubKeyMan::WriteDescriptor()

src/wallet/walletutil.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class WalletDescriptor
8585
{
8686
public:
8787
std::shared_ptr<Descriptor> descriptor;
88+
uint256 id; // Descriptor ID (calculated once at descriptor initialization/deserialization)
8889
uint64_t creation_time = 0;
8990
int32_t range_start = 0; // First item in range; start of range, inclusive, i.e. [range_start, range_end). This never changes.
9091
int32_t range_end = 0; // Item after the last; end of range, exclusive, i.e. [range_start, range_end). This will increment with each TopUp()
@@ -99,6 +100,7 @@ class WalletDescriptor
99100
if (!descriptor) {
100101
throw std::ios_base::failure("Invalid descriptor: " + error);
101102
}
103+
id = DescriptorID(*descriptor);
102104
}
103105

104106
SERIALIZE_METHODS(WalletDescriptor, obj)
@@ -110,7 +112,7 @@ class WalletDescriptor
110112
}
111113

112114
WalletDescriptor() {}
113-
WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index) : descriptor(descriptor), creation_time(creation_time), range_start(range_start), range_end(range_end), next_index(next_index) {}
115+
WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index) : descriptor(descriptor), id(DescriptorID(*descriptor)), creation_time(creation_time), range_start(range_start), range_end(range_end), next_index(next_index) { }
114116
};
115117
} // namespace wallet
116118

0 commit comments

Comments
 (0)