⚡️ Speed up function to_hexstr by 28%
#63
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.
📄 28% (0.28x) speedup for
to_hexstrinelectrum/plugins/digitalbitbox/digitalbitbox.py⏱️ Runtime :
51.1 microseconds→40.0 microseconds(best of250runs)📝 Explanation and details
The optimization leverages Python's built-in
bytes.hex()method for the common case where the input is abytesobject, providing a 27% speedup.Key Changes:
isinstance(s, bytes)to identify when the input is abytesobjectbytesinputs, use the native.hex()method instead ofbinascii.hexlify(s).decode('ascii')binascii.hexlify()approach for other types likebytearrayWhy It's Faster:
The
bytes.hex()method is implemented in C and optimized specifically for converting bytes to hexadecimal strings, avoiding the overhead of:binascii.hexlify()function.decode('ascii')step to convert bytes to stringPerformance Impact:
The performance profile shows that 80% of calls (40 out of 50) take the fast path through
s.hex(), making this optimization highly effective for the typical use case of convertingbytesobjects to hex strings in cryptocurrency/hardware wallet operations.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-to_hexstr-mhp0wo1xand push.