⚡️ Speed up function _group_and_resolve_entities by 299%#60
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function _group_and_resolve_entities by 299%#60codeflash-ai[bot] wants to merge 1 commit intomainfrom
_group_and_resolve_entities by 299%#60codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimization achieves a **299% speedup** by eliminating the expensive pandas `groupby.agg()` operation with Python lambdas, which was consuming 86.5% of the original runtime (879.9ms out of 1017ms). **Key optimizations:** 1. **Replaced pandas groupby.agg() with manual grouping**: Instead of using pandas' slow lambda functions in `.agg()`, the code now uses `np.unique()` with `return_inverse=True` to efficiently group rows by title, then applies operations directly on numpy arrays. 2. **Eliminated lambda overhead**: The original lambdas like `lambda x: list(x.astype(str))` and `lambda x: list(itertools.chain(*x.tolist()))` were called for each group. The optimized version precomputes string conversions and list flattening using vectorized operations and list comprehensions. 3. **Direct DataFrame construction**: Rather than creating an intermediate grouped object and resetting the index, the code builds the result dictionary directly and constructs the final DataFrame in one step. 4. **Removed itertools import**: No longer needed since list flattening is handled with nested list comprehensions. **Performance characteristics by test case:** - **Basic cases** (2-4 entities): 63-69% faster, showing consistent overhead reduction - **Large scale no overlap** (1000 entities): 517-598% faster, demonstrating excellent scaling for non-overlapping data - **Large scale with overlap** (1000 entities): 378-491% faster, still highly effective when merging is required The optimization is particularly effective for larger datasets where the groupby operation becomes the dominant bottleneck, while maintaining identical functionality and output format.
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.
📄 299% (2.99x) speedup for
_group_and_resolve_entitiesingraphrag/index/update/entities.py⏱️ Runtime :
384 milliseconds→96.1 milliseconds(best of13runs)📝 Explanation and details
The optimization achieves a 299% speedup by eliminating the expensive pandas
groupby.agg()operation with Python lambdas, which was consuming 86.5% of the original runtime (879.9ms out of 1017ms).Key optimizations:
Replaced pandas groupby.agg() with manual grouping: Instead of using pandas' slow lambda functions in
.agg(), the code now usesnp.unique()withreturn_inverse=Trueto efficiently group rows by title, then applies operations directly on numpy arrays.Eliminated lambda overhead: The original lambdas like
lambda x: list(x.astype(str))andlambda x: list(itertools.chain(*x.tolist()))were called for each group. The optimized version precomputes string conversions and list flattening using vectorized operations and list comprehensions.Direct DataFrame construction: Rather than creating an intermediate grouped object and resetting the index, the code builds the result dictionary directly and constructs the final DataFrame in one step.
Removed itertools import: No longer needed since list flattening is handled with nested list comprehensions.
Performance characteristics by test case:
The optimization is particularly effective for larger datasets where the groupby operation becomes the dominant bottleneck, while maintaining identical functionality and output format.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_group_and_resolve_entities-mglmouewand push.