⚡️ Speed up method BCDataStream.write_int64 by 17%
#53
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.
📄 17% (0.17x) speedup for
BCDataStream.write_int64inelectrum/transaction.py⏱️ Runtime :
1.92 milliseconds→1.65 milliseconds(best of227runs)📝 Explanation and details
The optimization achieves a 16% speedup by eliminating a function call overhead in the hot path. The key change is inlining the
_write_numlogic directly intowrite_int64instead of delegating through a method call.What changed:
write_int64calledself._write_num('<q', val), adding function call overheadWhy this is faster:
write_int64no longer requires setting up a new stack frame for_write_numwrite_int64was taking 20.6ms total time, with the function call consuming significant overheadbytearray.extend()for optimal memory managementPerformance impact by test case:
This optimization is particularly valuable because
write_int64appears to be a frequently called method in Bitcoin transaction serialization, where every microsecond saved in the hot path translates to meaningful performance gains during transaction processing.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-BCDataStream.write_int64-mholurw8and push.