⚡️ Speed up function merge_with by 22%
#73
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.
📄 22% (0.22x) speedup for
merge_withinpython/ccxt/static_dependencies/toolz/dicttoolz.py⏱️ Runtime :
1.87 milliseconds→1.54 milliseconds(best of243runs)📝 Explanation and details
The optimization replaces an inefficient
defaultdictpattern with a cleaner, more performant approach that achieves a 21% speedup.Key Changes:
Replaced inefficient
defaultdictpattern: Changedcollections.defaultdict(lambda: [].append)tocollections.defaultdict(list). The original creates a new empty list and binds its.appendmethod for each key, which is both memory-wasteful and computationally expensive.Direct method calls: Changed
values[k](v)tovalues[k].append(v), eliminating the function call overhead of invoking the bound.appendmethod through the lambda.Simplified variable naming: Renamed the loop variable from
vtovlistin the final loop for clarity, and removed the.__self__attribute access on the bound method.Why This is Faster:
The original code's
lambda: [].appendpattern creates overhead in two ways:.appendmethod is bound to each temporary list, creating function objects that must be called indirectlyThe optimized version uses Python's built-in
defaultdict(list)which:values[k].append(v)is a direct method call without function object overheadPerformance Impact:
From the line profiler, the critical bottleneck was in
values[k](v)(40.4% of runtime in original vs 32.8% in optimized). The optimization shows consistent 10-35% improvements across test cases, with larger gains on bigger datasets where the overhead compounds.Context Impact:
The function reference shows
merge_withis used in thecurriedmodule, suggesting it may be called frequently in functional programming contexts where this 21% improvement would be significant in aggregate performance.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-merge_with-mhy6uioqand push.