⚡️ Speed up function _get_bbox_to_page_ratio by 353%
#51
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.
📄 353% (3.53x) speedup for
_get_bbox_to_page_ratioinunstructured/partition/pdf_image/analysis/bbox_visualisation.py⏱️ Runtime :
930 microseconds→205 microseconds(best of250runs)📝 Explanation and details
The optimization applies Numba's Just-In-Time (JIT) compilation using the
@njit(cache=True)decorator to dramatically speed up this mathematical computation function.Key changes:
from numba import njitimport@njit(cache=True)decorator to the functionWhy this leads to a speedup:
Numba compiles Python bytecode to optimized machine code at runtime, eliminating Python's interpreter overhead for numerical computations. The function performs several floating-point operations (
math.sqrt, exponentiation, arithmetic) that benefit significantly from native machine code execution. Thecache=Trueparameter ensures the compiled version is cached for subsequent calls, avoiding recompilation overhead.Performance characteristics:
Impact on workloads:
Based on
function_references, this function is called from_get_optimal_value_for_bbox(), which suggests it's used in document analysis pipelines where bounding box calculations are performed repeatedly. The substantial speedup will be particularly beneficial when processing documents with many bounding boxes, as demonstrated by the large-scale test cases showing 300%+ improvements when processing thousands of bboxes.Optimization effectiveness:
Most effective for computational workloads with repeated calls to this function, especially when processing large documents or batch operations where the function is called hundreds or thousands of times.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_bbox_to_page_ratio-mjdkzmaoand push.