⚡️ Speed up function areIntersecting by 51%#37
Open
codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
Open
⚡️ Speed up function areIntersecting by 51%#37codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
areIntersecting by 51%#37codeflash-ai[bot] wants to merge 1 commit intoreleasefrom
Conversation
The optimized code achieves a **50% runtime improvement** (from 62.5μs to 41.5μs) by introducing **early-exit short-circuits** that avoid unnecessary property reads when rectangles don't intersect. **Key Optimization: Lazy Property Access with Early Returns** The original implementation eagerly reads all 8 rectangle properties upfront, even when rectangles clearly don't intersect. The optimized version reads properties on-demand and exits immediately when any overlap condition fails: 1. **Left/Right Check First**: Reads only `r2.left` and `r1.right`, returns false if no horizontal overlap 2. **Progressive Validation**: Only reads `r2.right` and `r1.left` if the first check passes 3. **Vertical Checks Last**: Only evaluates top/bottom overlap if horizontal overlap exists **Why This Works** From the line profiler data: - **Original**: All 8 property reads execute on every call (522-523 hits each) - **Optimized**: Properties are read selectively: - First two properties: 524 hits (always checked) - Next two properties: 267 hits (~50% early exit) - Final properties: 264 hits (~50% reach full evaluation) This means the optimization **avoids ~50% of property accesses** in typical cases where rectangles don't overlap, which aligns perfectly with the 50% speedup. **Performance by Test Pattern** The annotated tests show the optimization excels when: - **Non-intersecting rectangles** (most impactful): 89-161% faster on edge-touching and separated cases - **Zero-area rectangles**: 91-104% faster (early exits on invalid bounds) - **Large-scale checks**: 96-112% faster (cumulative effect across many calls) - **Intersecting rectangles**: 36-133% faster (still benefits from reduced property access ordering) The optimization maintains identical correctness while reducing CPU time through smarter evaluation order—a pure win for this frequently-called geometric utility.
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.
📄 51% (0.51x) speedup for
areIntersectinginapp/client/src/utils/boxHelpers.ts⏱️ Runtime :
62.5 microseconds→41.5 microseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 50% runtime improvement (from 62.5μs to 41.5μs) by introducing early-exit short-circuits that avoid unnecessary property reads when rectangles don't intersect.
Key Optimization: Lazy Property Access with Early Returns
The original implementation eagerly reads all 8 rectangle properties upfront, even when rectangles clearly don't intersect. The optimized version reads properties on-demand and exits immediately when any overlap condition fails:
r2.leftandr1.right, returns false if no horizontal overlapr2.rightandr1.leftif the first check passesWhy This Works
From the line profiler data:
This means the optimization avoids ~50% of property accesses in typical cases where rectangles don't overlap, which aligns perfectly with the 50% speedup.
Performance by Test Pattern
The annotated tests show the optimization excels when:
The optimization maintains identical correctness while reducing CPU time through smarter evaluation order—a pure win for this frequently-called geometric utility.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
📊 Performance Profile
View detailed line-by-line performance analysis
To edit these changes
git checkout codeflash/optimize-areIntersecting-ml25onm7and push.