-
Notifications
You must be signed in to change notification settings - Fork 1.1k
enhancement: Speed up function _assign_hash_ids by 34% #4101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+17
−12
Conversation
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
The optimization replaces `itertools.groupby` with a simple dictionary-based counting approach in the `_assign_hash_ids` function. **Key change:** Instead of creating intermediate lists (`page_numbers` and `page_seq_numbers`) and using `itertools.groupby`, the optimized version uses a dictionary `page_seq_counts` to track sequence numbers for each page in a single pass. **Why it's faster:** - **Eliminates list comprehensions:** The original code creates a full `page_numbers` list upfront, then processes it with `groupby`. The optimized version processes elements directly without intermediate collections. - **Removes `itertools.groupby` overhead:** `groupby` requires sorting/grouping operations that add computational complexity. The dictionary lookup `page_seq_counts.get(page_number, 0)` is O(1) vs the O(n) grouping operations. - **Single-pass processing:** Instead of two passes (first to collect page numbers, then to generate sequences), the optimization does everything in one loop through the elements. **Performance characteristics:** The optimization is particularly effective for documents with many pages or elements, as shown in the test results where empty lists see 300%+ speedups. The 34% overall speedup demonstrates the efficiency gain from eliminating the `itertools.groupby` bottleneck, which consumed 19.5% + 6.3% of the original runtime according to the line profiler.
remove newline
|
You have run out of free Bugbot PR reviews for this billing cycle. This will reset on October 21. To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial. |
badGarnet
approved these changes
Sep 25, 2025
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.
In-repo duplicate of #4089.