⚡️ Speed up function _get_optimal_value_for_bbox by 2,883%
#52
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.
📄 2,883% (28.83x) speedup for
_get_optimal_value_for_bboxinunstructured/partition/pdf_image/analysis/bbox_visualisation.py⏱️ Runtime :
11.9 milliseconds→398 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 2882% speedup by applying two key optimizations:
1. Numba JIT Compilation:
Added
@njit(cache=True, fastmath=True)decorators to_get_bbox_to_page_ratioand the new_linear_polyfit_2pointfunctions. Numba compiles these Python functions to machine code, eliminating interpreter overhead and providing near-C performance for numerical computations.2. Replaced NumPy's General-Purpose Linear Regression:
The original code used
np.polyfit()for simple 2-point linear interpolation, which is overkill and involves significant overhead. The optimization replaces this with a custom_linear_polyfit_2pointfunction that directly computes slope and intercept using basic arithmetic:slope = (y1-y0)/(x1-x0)andintercept = y0 - slope*x0. This eliminates the overhead of NumPy's general polynomial fitting algorithm.Performance Impact:
From the line profiler results, the original
np.polyfitcall consumed 86.5% of execution time (24.7ms out of 28.6ms total). The optimized version reduces this to just 15.2% of a much smaller total runtime. The first call to each JIT-compiled function includes compilation overhead, but subsequent calls benefit from cached machine code.Real-World Benefits:
Based on function references,
_get_optimal_value_for_bboxis called byget_bbox_text_sizeandget_bbox_thicknessfor PDF visualization. These functions likely process many bounding boxes during document analysis, making the 20x+ speedup significant for document processing pipelines.Test Case Performance:
The optimizations excel across all test scenarios, showing 15-30x speedups for individual calls and even higher gains (30x) for bulk processing tests with many bounding boxes, demonstrating the value of JIT compilation for repeated computational workloads.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_get_optimal_value_for_bbox-mjdl64kxand push.