Skip to content

Commit bf46e7a

Browse files
achow101knst
authored andcommitted
partial Merge bitcoin#28799: wallet: cache descriptor ID to avoid repeated descriptor string creation
BACKPORT NOTICE: It doesn't include changes related to wallet_miniscript.py 5e6bc6d test: remove custom rpc timeout for `wallet_miniscript.py`, reorder in test_runner (Sebastian Falbesoner) f811a24 wallet: cache descriptor ID to avoid repeated descriptor string creation (Sebastian Falbesoner) Pull request description: Right now a wallet descriptor is converted to its 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, also at less obvious places like `FastWalletRescanFilter` 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. Fixes bitcoin#28800. ACKs for top commit: Sjors: ACK 5e6bc6d S3RK: Code Review ACK 5e6bc6d achow101: ACK 5e6bc6d BrandonOdiwuor: ACK 5e6bc6d Tree-SHA512: 98b43963a5dde6055bb26cecd3b878dadd837d6226af4c84142383310495da80b3c4bd552e73b9107f2f2ff1c11f5e18060c6fd3d9e44bbd5224114c4d245c1c
1 parent 05e5966 commit bf46e7a

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ std::unique_ptr<CKeyMetadata> DescriptorScriptPubKeyMan::GetMetadata(const CTxDe
23092309
uint256 DescriptorScriptPubKeyMan::GetID() const
23102310
{
23112311
LOCK(cs_desc_man);
2312-
return DescriptorID(*m_wallet_descriptor.descriptor);
2312+
return m_wallet_descriptor.id;
23132313
}
23142314

23152315
void DescriptorScriptPubKeyMan::SetInternal(bool internal)
@@ -2368,7 +2368,7 @@ bool DescriptorScriptPubKeyMan::AddCryptedKey(const CKeyID& key_id, const CPubKe
23682368
bool DescriptorScriptPubKeyMan::HasWalletDescriptor(const WalletDescriptor& desc) const
23692369
{
23702370
LOCK(cs_desc_man);
2371-
return m_wallet_descriptor.descriptor != nullptr && desc.descriptor != nullptr && m_wallet_descriptor.descriptor->ToString() == desc.descriptor->ToString();
2371+
return !m_wallet_descriptor.id.IsNull() && !desc.id.IsNull() && m_wallet_descriptor.id == desc.id;
23722372
}
23732373

23742374
void DescriptorScriptPubKeyMan::WriteDescriptor()

src/wallet/walletutil.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class WalletDescriptor
6767
{
6868
public:
6969
std::shared_ptr<Descriptor> descriptor;
70+
uint256 id; // Descriptor ID (calculated once at descriptor initialization/deserialization)
7071
uint64_t creation_time = 0;
7172
int32_t range_start = 0; // First item in range; start of range, inclusive, i.e. [range_start, range_end). This never changes.
7273
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()
@@ -80,7 +81,8 @@ class WalletDescriptor
8081
descriptor = Parse(str, keys, error, true);
8182
if (!descriptor) {
8283
throw std::ios_base::failure("Invalid descriptor: " + error);
83-
}
84+
}
85+
id = DescriptorID(*descriptor);
8486
}
8587

8688
SERIALIZE_METHODS(WalletDescriptor, obj)
@@ -92,7 +94,7 @@ class WalletDescriptor
9294
}
9395

9496
WalletDescriptor() {}
95-
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) {}
97+
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) { }
9698
};
9799

98100
#endif // BITCOIN_WALLET_WALLETUTIL_H

0 commit comments

Comments
 (0)