⚡️ Speed up function safe_division by 17%#47
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
Conversation
The optimization replaces `max(b, FLOAT_EPSILON)` with a conditional expression `a / FLOAT_EPSILON if b <= FLOAT_EPSILON else a / b`. This provides a **17% speedup** by eliminating the overhead of Python's `max()` built-in function call. **Key Performance Improvements:** 1. **Eliminates Function Call Overhead**: The `max()` function involves Python's function call machinery (argument unpacking, dispatch), which is costly for such a simple operation. The conditional expression evaluates directly without this overhead. 2. **Branch Prediction Benefits**: The `if-else` construct allows the CPU's branch predictor to optimize the common case. Looking at the test results, when `b` is a normal value (>> FLOAT_EPSILON), the else branch is taken and executes efficiently. Test cases with normal denominators show 10-15% speedups, while edge cases with tiny denominators show even better improvements (25-38%). 3. **Micro-optimization Impact**: Per-hit time improved from 919.2ns to 811.1ns (~12% per call), which compounds significantly when called repeatedly. **Why This Matters Based on Function References:** The function is used in hot paths for geometric computations in `unstructured_inference/inference/elements.py`: - `intersection_over_union()` - Called for comparing rectangle similarity - `intersection_over_minimum()` - Used for subset detection - `is_almost_subregion_of()` - Performs subregion checks These operations are likely executed in tight loops during document layout analysis, where comparing many bounding boxes is common. The 17% speedup means faster document processing pipelines. **Test Case Performance Patterns:** - **Normal cases** (denominator >> FLOAT_EPSILON): 10-16% faster - the common path benefits from avoiding `max()` - **Edge cases** (denominator ≤ FLOAT_EPSILON): 20-38% faster - these benefit from short-circuit evaluation taking the first branch immediately - **Batch operations**: 20-31% faster when processing multiple rectangles, compounding the per-call savings The optimization maintains identical behavior while delivering consistent performance gains across all workload types.
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.
📄 17% (0.17x) speedup for
safe_divisioninunstructured_inference/math.py⏱️ Runtime :
469 microseconds→401 microseconds(best of138runs)📝 Explanation and details
The optimization replaces
max(b, FLOAT_EPSILON)with a conditional expressiona / FLOAT_EPSILON if b <= FLOAT_EPSILON else a / b. This provides a 17% speedup by eliminating the overhead of Python'smax()built-in function call.Key Performance Improvements:
Eliminates Function Call Overhead: The
max()function involves Python's function call machinery (argument unpacking, dispatch), which is costly for such a simple operation. The conditional expression evaluates directly without this overhead.Branch Prediction Benefits: The
if-elseconstruct allows the CPU's branch predictor to optimize the common case. Looking at the test results, whenbis a normal value (>> FLOAT_EPSILON), the else branch is taken and executes efficiently. Test cases with normal denominators show 10-15% speedups, while edge cases with tiny denominators show even better improvements (25-38%).Micro-optimization Impact: Per-hit time improved from 919.2ns to 811.1ns (~12% per call), which compounds significantly when called repeatedly.
Why This Matters Based on Function References:
The function is used in hot paths for geometric computations in
unstructured_inference/inference/elements.py:intersection_over_union()- Called for comparing rectangle similarityintersection_over_minimum()- Used for subset detectionis_almost_subregion_of()- Performs subregion checksThese operations are likely executed in tight loops during document layout analysis, where comparing many bounding boxes is common. The 17% speedup means faster document processing pipelines.
Test Case Performance Patterns:
max()The optimization maintains identical behavior while delivering consistent performance gains across all workload types.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_math.py::test_safe_division🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-safe_division-mkouw6hoand push.