⚡️ Speed up method PartialTransaction.has_change by 100%
#148
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.
📄 100% (1.00x) speedup for
PartialTransaction.has_changeinelectrum/transaction.py⏱️ Runtime :
217 microseconds→109 microseconds(best of42runs)📝 Explanation and details
The optimization replaces the inefficient
has_change()implementation with a short-circuit algorithm that delivers up to 99% speedup (217μs → 109μs).Key Optimization:
The original
has_change()calledget_change_outputs()which builds a complete list of all change outputs via list comprehension, then checkslen() > 0. The optimized version uses a directforloop that returnsTrueimmediately upon finding the first change output, eliminating unnecessary work.Why This Is Faster:
get_change_outputs()Performance Characteristics by Test Case:
Impact Analysis:
This optimization is particularly beneficial for:
has_change()might be called repeatedly during transaction processingThe
get_change_outputs()method remains unchanged to preserve existing behavior for callers that actually need the complete list of change outputs.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_6p7ovzz5/tmpar7e8lmc/test_concolic_coverage.py::test_PartialTransaction_has_changeTo edit these changes
git checkout codeflash/optimize-PartialTransaction.has_change-mhxurdyland push.