⚡️ Speed up method RIPEMD160.copy by 2,826%
#128
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.
📄 2,826% (28.26x) speedup for
RIPEMD160.copyinelectrum/ripemd.py⏱️ Runtime :
2.18 milliseconds→74.4 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 2826% speedup by replacing Python's
copy.deepcopy()with a custom shallow copy implementation and adding__slots__for memory efficiency.Key optimizations:
Eliminated
copy.deepcopy()overhead: The original code usedcopy.deepcopy(self)which has significant overhead - it imports the copy module, traverses the entire object graph, and creates deep copies of all nested objects. The line profiler shows this single call took 99.6% of execution time (15.4ms out of 15.5ms total).Custom shallow copy implementation: The optimized version manually creates new instances using
__new__()and copies only the necessary attributes. Since RIPEMD160 objects contain simple data structures (thectxobject with basic attributes like state arrays, count, and buffer), a shallow copy of the context's__dict__is sufficient and safe.Added
__slots__: This prevents Python from creating a__dict__for each instance, reducing memory overhead and slightly improving attribute access speed.Why this works:
RMDContext) contains primitive data types and byte strings that don't require deep copying__new__()bypasses__init__()overhead__dict__.update()is much faster than deep copy's recursive traversalPerformance benefits by test case:
This optimization is especially valuable in cryptographic contexts where hash objects are frequently copied for parallel processing or state preservation.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_6p7ovzz5/tmp5tjepnqs/test_concolic_coverage.py::test_RIPEMD160_copyTo edit these changes
git checkout codeflash/optimize-RIPEMD160.copy-mhwxat34and push.