⚡️ Speed up function bbox_to_polygon by 5%
#8
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.
📄 5% (0.05x) speedup for
bbox_to_polygonindoctr/utils/geometry.py⏱️ Runtime :
419 microseconds→398 microseconds(best of176runs)📝 Explanation and details
The optimization reduces redundant tuple indexing operations by caching
bbox[0]andbbox[1]in local variablesbbox0andbbox1.In the original code,
bbox[0]is accessed 3 times andbbox[1]is accessed 3 times within the single return statement. Each tuple indexing operation has overhead in Python. The optimized version performs each indexing only once, storing the results in local variables that are then reused.This micro-optimization is particularly effective because:
The test results show the optimization performs best on large-scale operations (5-7% speedup on batch processing tests like
test_bbox_to_polygon_many_boxesandtest_bbox_to_polygon_stress_randomized) where the indexing overhead compounds across many function calls. Individual function calls show mixed but generally positive results, with the overall 5% speedup representing meaningful performance gains for high-frequency geometric operations.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
common/test_utils_geometry.py::test_bbox_to_polygon🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-bbox_to_polygon-mg7ryhciand push.