fix: optimize SortedStreamSetObjectsList with binary search (#3137)#3145
Open
knightabir wants to merge 1 commit intoAutoMQ:mainfrom
Open
fix: optimize SortedStreamSetObjectsList with binary search (#3137)#3145knightabir wants to merge 1 commit intoAutoMQ:mainfrom
knightabir wants to merge 1 commit intoAutoMQ:mainfrom
Conversation
) Problem: - SortedStreamSetObjectsList used LinkedList with linear O(n) scans for insertion and removal operations - Poor performance as metadata size grows - Explicit TODO comments requesting binary search optimization Solution: - Replace LinkedList with ArrayList for efficient O(1) random access - Implement binary search using Collections.binarySearch() - Handle duplicate orderId cases correctly (compareTo uses orderId while equals uses objectId) Changes: - Switch from LinkedList to ArrayList - Implement findInsertionIndex() using Collections.binarySearch() - Implement findExactMatch() for remove/contains with binary search - Add proper null safety and type checking - Handle duplicates by scanning nearby elements after binary search Performance Impact: - Search operations: O(n) -> O(log n) - Insertion/removal: O(n) scan -> O(log n) search + O(n) array shift - Overall improvement for large datasets Testing: - All existing tests pass - Behavior-preserving change that implements existing TODOs Fixes AutoMQ#3137
Collaborator
|
@knightabir Hey! Thanks a ton for your PR! It's really awesome to see your contribution. By the way, would you be super amazing and add a test for it? |
Author
Yes sure I will be adding the test ASAP. |
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.
Description
This PR optimizes
SortedStreamSetObjectsListby replacingLinkedListwithArrayListand implementing binary search for insertion, removal, and contains operations.Related Issue
Fixes #3137
Problem
The current implementation uses
LinkedListwith linear O(n) scans for insertion and removal operations, leading to poor performance as metadata size grows. The code contained explicit TODO comments requesting binary search optimization.Solution
LinkedListwithArrayListfor O(1) random accessCollections.binarySearch()findInsertionIndex()andfindExactMatch()orderIdcases (sincecompareTousesorderIdwhileequalsusesobjectId)Changes Made
File Modified:
metadata/src/main/java/org/apache/kafka/metadata/stream/SortedStreamSetObjectsList.javaKey Changes:
LinkedList→ArrayListfindInsertionIndex(): UsesCollections.binarySearch()to find insertion pointfindExactMatch(): Handles duplicateorderIdvalues correctlyPerformance Impact
Special Handling
The implementation correctly handles the case where:
compareTo()usesorderIdfor sortingequals()usesobjectIdfor identityWhen multiple objects have the same
orderId, the binary search locates one of them, then scans forward and backward to find the exact match byobjectId.Testing
✅ All existing tests pass (
SortedStreamSetObjectsListTest.testSorted())✅ No changes to test files required - behavior is preserved
✅ Verified with:
./gradlew test --tests SortedStreamSetObjectsListTestChecklist
Additional Notes
This is a behavior-preserving optimization that directly implements the TODO comments present in the original code:
The implementation uses Java's standard
Collections.binarySearch()for reliability and maintainability.