⚡️ Speed up method SwapData.is_funded by 11%
#31
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
SwapData.is_fundedinelectrum/submarine_swaps.py⏱️ Runtime :
708 microseconds→641 microseconds(best of171runs)📝 Explanation and details
The optimization reorders the boolean expression in the
is_fundedmethod fromself._payment_pending or bool(self.funding_txid)tobool(self.funding_txid) or self._payment_pending. This simple change achieves a 10% speedup by taking advantage of Python's short-circuit evaluation.Key optimization: When
funding_txidis truthy (non-None, non-empty string), the optimized version can returnTrueimmediately without evaluating_payment_pending. Thebool()function call is faster than accessing the_payment_pendingattribute.Why this works: The test results show this optimization performs best when
funding_txidis set and_payment_pendingisFalse(18.6% faster in one test case). This suggests that in typical usage,funding_txidbeing truthy is more common than_payment_pendingbeingTrue, making the reordering beneficial for the common case.Trade-off: The optimization is slightly slower (6-28%) when
_payment_pendingisTruebutfunding_txidis falsy, since it must evaluate the more expensivebool(self.funding_txid)first. However, the overall 10% speedup indicates this is a less frequent scenario.This optimization is most effective for workloads where funded swaps (with valid funding transaction IDs) are processed frequently, as demonstrated by the large batch tests showing 25.8% improvement when all swaps have funding_txid set.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic__q13nwva/tmpurhc1cva/test_concolic_coverage.py::test_SwapData_is_fundedTo edit these changes
git checkout codeflash/optimize-SwapData.is_funded-mhjidh8fand push.