⚡️ Speed up method KeystoreWizard.on_hardware_device by 11%
#38
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.
📄 11% (0.11x) speedup for
KeystoreWizard.on_hardware_deviceinelectrum/wizard.py⏱️ Runtime :
132 microseconds→118 microseconds(best of175runs)📝 Explanation and details
The optimized code achieves an 11% speedup by eliminating function call overhead through inlining the
current_cosignermethod directly intoon_hardware_device.What was optimized:
self.current_cosigner(wizard_data)which added significant overhead (537μs out of 1288μs total time). The optimized version inlines this logic directly, reducing overhead to just 70μs for the multisig check..get()with default values instead ofinchecks followed by direct key access, reducing potential KeyError exceptions and multiple dictionary lookups.Why this leads to speedup:
current_cosignercall consumed 41.7% of total execution time..get()method calls are slightly more expensive per operation but provide better safety and avoid the compound condition checking that was happening before.Test case performance:
The optimization shows consistent 3-16% improvements across all test cases, with the largest gains in basic single hardware device scenarios (16.7% faster) and good improvements in multisig scenarios (3-6% faster). This suggests the optimization benefits both simple and complex wallet configurations.
Impact assessment:
Since this appears to be part of a wallet setup wizard, even an 11% improvement in hardware device initialization could provide noticeable user experience improvements, especially when users are setting up multiple cosigners in multisig wallets or switching between different hardware devices.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-KeystoreWizard.on_hardware_device-mhlanls5and push.