⚡️ Speed up method NewWalletWizard.on_have_cosigner_seed by 60% #39
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
📄 60% (0.60x) speedup for
NewWalletWizard.on_have_cosigner_seedinelectrum/wizard.py⏱️ Runtime :
32.3 microseconds→20.2 microseconds(best of39runs)📝 Explanation and details
The optimization achieves a 59% speedup by eliminating redundant function calls and reducing dictionary lookups in hot paths. Here are the key improvements:
Function Call Elimination: The original code made expensive function calls in
needs_derivation_path()(callingcurrent_cosigner()) andon_have_cosigner_seed()(calling bothcurrent_cosigner()andlast_cosigner()). The optimized version inlines this logic directly, eliminating the function call overhead which was consuming 90.6% of execution time inneeds_derivation_path().Dictionary Access Optimization: The inlined logic reduces repeated dictionary lookups by storing intermediate values in local variables (e.g.,
cosigner_key = str(cosigner),required = wizard_data['multisig_participants'] - 1). This is particularly effective for the multisig workflow where the same keys are accessed multiple times.Membership Testing Improvement: Changed the seed variant check from list
['bip39', 'slip39']to tuple('bip39', 'slip39')for slightly faster constant membership testing.Control Flow Optimization: In
last_cosigner(), reordered conditions to handle the common non-multisig case first, allowing faster early returns.Performance Impact: The test results show consistent speedups across all scenarios (45-72% faster), with the largest gains in complex multisig workflows. Functions like
on_have_cosigner_seed()that previously spent significant time in function calls (46.8% oncurrent_cosigner(), 35.3% onneeds_derivation_path()) now execute this logic inline, explaining the substantial performance improvement.These optimizations are particularly valuable for wallet setup workflows involving multisig configurations, where these functions are called repeatedly during the wizard navigation process.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-NewWalletWizard.on_have_cosigner_seed-mhlb4rb4and push.