⚡️ Speed up function sweep_htlctx_output by 10%
#127
+4
−2
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.
📄 10% (0.10x) speedup for
sweep_htlctx_outputinelectrum/lnsweep.py⏱️ Runtime :
996 microseconds→907 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 9% speedup by caching expensive method calls that were being repeated. Specifically:
Key changes:
ctx.outputs()- Stored in local variableoutputsto avoid redundant method calls when accessingoutputs[output_idx].valuectx.txid()- Stored in local variabletxidto avoid redundant computation inTxOutpoint(txid=bfh(txid), out_idx=output_idx)Why this optimization works:
ctx.outputs()andctx.txid()methods likely involve expensive operations like transaction deserialization and hash computation (as seen in the dependency code)ctx.outputs()[output_idx].valuecallsoutputs()every time, andbfh(ctx.txid())callstxid()every timeImpact based on function references:
This optimization is particularly valuable given the function references show
sweep_htlctx_outputis called in hot paths:Test case performance:
The optimization shows consistent 6-11% improvements across most test cases, with the largest gains (10-11%) in scenarios involving multiple calls or larger transactions - exactly the use cases indicated by the function references where this code runs in loops processing multiple HTLCs.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-sweep_htlctx_output-mhwrxshwand push.