⚡️ Speed up function get_bbox_span_subset by 306%#38
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function get_bbox_span_subset by 306%#38codeflash-ai[bot] wants to merge 1 commit intomainfrom
get_bbox_span_subset by 306%#38codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimized code achieves a **305% speedup** (9.24ms → 2.28ms) by eliminating expensive object allocations and method calls that dominated the original implementation. ## Key Optimizations **1. Eliminated Rect Object Construction (67.8% → 0% of overlaps() time)** - The original code created `Rect` objects via `Rect(list(bbox1))` and `Rect(list(bbox2))`, which involved: - Two `list()` calls to copy sequences - Object instantiation overhead - Attribute assignments in `__init__` - The optimized version directly indexes into bbox coordinates (`bbox1[0]`, `bbox1[1]`, etc.), avoiding all allocations **2. Inlined Intersection Area Calculation** - Original: `rect1.intersect(other).get_area()` required method calls and state mutations - Optimized: Direct arithmetic with conditional logic computes intersection area inline - Eliminates method call overhead and intermediate object state changes **3. List Comprehension in get_bbox_span_subset()** - Replaced explicit loop + append pattern with list comprehension - Reduces Python-level loop overhead and function call overhead for `list.append()` - Comprehensions are optimized at the C level in CPython ## Performance Impact by Test Case The optimization shows **~2-3.5x speedup** across all test patterns: - Simple cases (single spans): **~110-125% faster** (8-9μs → 3-4μs) - Large-scale tests (100-1000 spans): **~300-350% faster** (150-3000μs → 40-770μs) - Zero-area edge cases benefit most: **up to 149% faster** due to early exit efficiency ## Context from Function References The function `extract_text_inside_bbox()` calls `get_bbox_span_subset()` in what appears to be a text extraction pipeline. Given this is table postprocessing code (Microsoft Table Transformer), this likely runs on **every table cell or region** during document analysis. The optimization is particularly valuable because: - Table extraction processes many bounding boxes per page - Each bbox may be checked against hundreds of text spans - The cumulative effect of 3-4x speedup per call becomes significant in production workloads ## Why It Works The line profiler shows the original `overlaps()` spent 67.8% of time in `rect1.intersect(Rect(list(bbox2))).get_area()`. By replacing object-oriented abstractions with direct arithmetic, the optimized version distributes work across simple operations (indexing, comparisons, arithmetic) that execute much faster than object construction and method dispatch in Python.
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.
📄 306% (3.06x) speedup for
get_bbox_span_subsetinunstructured_inference/models/table_postprocess.py⏱️ Runtime :
9.24 milliseconds→2.28 milliseconds(best of115runs)📝 Explanation and details
The optimized code achieves a 305% speedup (9.24ms → 2.28ms) by eliminating expensive object allocations and method calls that dominated the original implementation.
Key Optimizations
1. Eliminated Rect Object Construction (67.8% → 0% of overlaps() time)
Rectobjects viaRect(list(bbox1))andRect(list(bbox2)), which involved:list()calls to copy sequences__init__bbox1[0],bbox1[1], etc.), avoiding all allocations2. Inlined Intersection Area Calculation
rect1.intersect(other).get_area()required method calls and state mutations3. List Comprehension in get_bbox_span_subset()
list.append()Performance Impact by Test Case
The optimization shows ~2-3.5x speedup across all test patterns:
Context from Function References
The function
extract_text_inside_bbox()callsget_bbox_span_subset()in what appears to be a text extraction pipeline. Given this is table postprocessing code (Microsoft Table Transformer), this likely runs on every table cell or region during document analysis. The optimization is particularly valuable because:Why It Works
The line profiler shows the original
overlaps()spent 67.8% of time inrect1.intersect(Rect(list(bbox2))).get_area(). By replacing object-oriented abstractions with direct arithmetic, the optimized version distributes work across simple operations (indexing, comparisons, arithmetic) that execute much faster than object construction and method dispatch in Python.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-get_bbox_span_subset-mkospfudand push.