⚡️ Speed up method AdvancedPdfLoader._format_table_element by 34%#53
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
Conversation
The optimized code achieves a **33% speedup** by eliminating unnecessary function calls and optimizing string handling in the hot path.
**Key optimizations:**
1. **Function call elimination**: The original code always called `self._clean_text()` even when `table_html` was available and would be returned immediately. The optimized version moves the text cleaning logic inline and only executes it when needed (when `table_html` is falsy), avoiding 214 unnecessary function calls based on the profiler data.
2. **Lazy text processing**: Instead of always calling `element.get("text", "")` and cleaning it upfront, the optimized code first checks if `table_html` exists. Only when falling back to text does it retrieve and process the text value, saving work in 62% of cases (214 out of 345 calls returned HTML).
3. **Conditional string conversion**: The optimized code uses `isinstance(text, str)` to avoid redundant `str()` calls when the text is already a string, which is the common case. This eliminates double string conversions.
**Performance impact by test case type:**
- **HTML-preferred cases** (54-104% faster): Maximum benefit since text processing is completely skipped
- **Missing text cases** (67-88% faster): Substantial gains from avoiding unnecessary `_clean_text` calls
- **Text-only cases** (0-17% faster): Modest gains from inline processing and conditional string conversion
- **Large-scale tests** (42-68% faster): Benefits scale well with data size
The optimization is particularly effective because most real PDF processing workflows prioritize HTML table representations when available, making the early return path the dominant case. The inline text processing also reduces Python function call overhead, which becomes significant in data processing pipelines.
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.
📄 34% (0.34x) speedup for
AdvancedPdfLoader._format_table_elementincognee/infrastructure/loaders/external/advanced_pdf_loader.py⏱️ Runtime :
205 microseconds→153 microseconds(best of95runs)📝 Explanation and details
The optimized code achieves a 33% speedup by eliminating unnecessary function calls and optimizing string handling in the hot path.
Key optimizations:
Function call elimination: The original code always called
self._clean_text()even whentable_htmlwas available and would be returned immediately. The optimized version moves the text cleaning logic inline and only executes it when needed (whentable_htmlis falsy), avoiding 214 unnecessary function calls based on the profiler data.Lazy text processing: Instead of always calling
element.get("text", "")and cleaning it upfront, the optimized code first checks iftable_htmlexists. Only when falling back to text does it retrieve and process the text value, saving work in 62% of cases (214 out of 345 calls returned HTML).Conditional string conversion: The optimized code uses
isinstance(text, str)to avoid redundantstr()calls when the text is already a string, which is the common case. This eliminates double string conversions.Performance impact by test case type:
_clean_textcallsThe optimization is particularly effective because most real PDF processing workflows prioritize HTML table representations when available, making the early return path the dominant case. The inline text processing also reduces Python function call overhead, which becomes significant in data processing pipelines.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-AdvancedPdfLoader._format_table_element-mhwrjz00and push.