⚡️ Speed up function embed_community_reports by 28%#66
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function embed_community_reports by 28%#66codeflash-ai[bot] wants to merge 1 commit intomainfrom
embed_community_reports by 28%#66codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimization replaces pandas' `.apply()` with lambda function with a direct list comprehension approach, yielding a **27% speedup**. **Key Changes:** - **Eliminated pandas `.apply()` overhead**: The original code used `reports_df.loc[:, source_col].apply(lambda x: embedder.embed(x))` which has significant pandas overhead for element-wise operations - **Direct list comprehension**: Replaced with `src = reports_df[source_col].to_list()` followed by `embeddings = [embedder.embed(x) for x in src]` - **Reduced pandas Series operations**: Converted to native Python list processing before assigning back to the DataFrame **Why This is Faster:** 1. **Pandas `.apply()` overhead**: Each `.apply()` call has internal pandas machinery that processes each element through the pandas Series infrastructure 2. **Lambda function overhead**: Creating and calling lambda functions for each row adds computational cost 3. **List comprehension efficiency**: Native Python list comprehensions are highly optimized in CPython and avoid pandas' internal overhead **Performance Characteristics:** - **Best for moderate to large datasets**: Shows 18-40% improvements across test cases with varying DataFrame sizes - **Consistent gains**: Even small DataFrames (single row) see 37-40% speedup - **Scales well**: Large DataFrames (1000 rows) maintain 18-20% improvements - **Edge cases preserved**: Handles None values, mixed types, and empty DataFrames correctly while maintaining the performance benefit The line profiler shows the bottleneck shifted from a single expensive `.apply()` operation (93.4% of time) to three more balanced operations: list conversion (9.8%), embedding computation (33.5%), and DataFrame assignment (49.3%).
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.
📄 28% (0.28x) speedup for
embed_community_reportsingraphrag/query/indexer_adapters.py⏱️ Runtime :
5.77 milliseconds→4.52 milliseconds(best of377runs)📝 Explanation and details
The optimization replaces pandas'
.apply()with lambda function with a direct list comprehension approach, yielding a 27% speedup.Key Changes:
.apply()overhead: The original code usedreports_df.loc[:, source_col].apply(lambda x: embedder.embed(x))which has significant pandas overhead for element-wise operationssrc = reports_df[source_col].to_list()followed byembeddings = [embedder.embed(x) for x in src]Why This is Faster:
.apply()overhead: Each.apply()call has internal pandas machinery that processes each element through the pandas Series infrastructurePerformance Characteristics:
The line profiler shows the bottleneck shifted from a single expensive
.apply()operation (93.4% of time) to three more balanced operations: list conversion (9.8%), embedding computation (33.5%), and DataFrame assignment (49.3%).✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-embed_community_reports-mglotmuband push.