⚡️ Speed up method CollectionUtils.mergeSorted by 89%#310
Open
codeflash-ai[bot] wants to merge 1 commit intoexperimentalfrom
Open
⚡️ Speed up method CollectionUtils.mergeSorted by 89%#310codeflash-ai[bot] wants to merge 1 commit intoexperimentalfrom
CollectionUtils.mergeSorted by 89%#310codeflash-ai[bot] wants to merge 1 commit intoexperimentalfrom
Conversation
The optimized code caches the size values and current elements from each list to eliminate redundant `get()` calls in the hot merge loop. In the original version, each iteration called `a.get(i)` and `b.get(j)` multiple times—once for the size check, once for the comparison, and once for the add—accounting for 80.8% of runtime in the profiler. By fetching each element once and reusing the cached value across comparison and insertion, the optimized version cuts per-iteration cost and achieves an 89% speedup (2.29 ms → 1.21 ms). The code also adds a fast path using `addAll` for empty-list cases and an iterator-based fallback for non-RandomAccess lists to avoid O(n²) behavior on linked structures.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 89% (0.89x) speedup for
CollectionUtils.mergeSortedinjava/src/main/java/com/optimizeme/CollectionUtils.java⏱️ Runtime :
2.29 milliseconds→1.21 milliseconds(best of59runs)📝 Explanation and details
The optimized code caches the size values and current elements from each list to eliminate redundant
get()calls in the hot merge loop. In the original version, each iteration calleda.get(i)andb.get(j)multiple times—once for the size check, once for the comparison, and once for the add—accounting for 80.8% of runtime in the profiler. By fetching each element once and reusing the cached value across comparison and insertion, the optimized version cuts per-iteration cost and achieves an 89% speedup (2.29 ms → 1.21 ms). The code also adds a fast path usingaddAllfor empty-list cases and an iterator-based fallback for non-RandomAccess lists to avoid O(n²) behavior on linked structures.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-CollectionUtils.mergeSorted-mngpbjfrand push.