Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix performance issue on AllIntersections #14

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

gilbsgilbs
Copy link
Contributor

@gilbsgilbs gilbsgilbs commented Dec 5, 2024

This commit addresses a performance issue in the AllIntersections function, where the conditional for traversing the right side of the tree was evaluating to true most of the time. This led to unnecessary visits of right nodes, resulting in linear time complexity.

More background on why this fix is correct:

  • Left is safe to prune when all lower nodes end before the start of the lookup interval (Left.MaxEnd < start). (Note: this case was already correct before this commit, but I reversed the inequality as I find it easier to reason about)
  • Right is safe to prune when all higher nodes start after the end of lookup interval (end < Current.Start). If the current node starts after the end of the lookup interval, we're guaranteed all its right nodes will also because they all start after the current node by definition.

Fuzz testing yielded consistent results with original implementation. Benchmark showed ~30% speed improvement on average case (random intervals), and over 99.9% improvement in worst case (lookup interval at the far left of the tree). As expected, new implementation explores fewer nodes that can be evicted just as safely in advance.

@gilbsgilbs gilbsgilbs force-pushed the fix-intersect-perf branch 3 times, most recently from d053367 to 88a564a Compare December 5, 2024 18:00
@codecov-commenter
Copy link

codecov-commenter commented Dec 5, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.88%. Comparing base (93fac80) to head (22514f2).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #14      +/-   ##
==========================================
- Coverage   95.52%   94.88%   -0.64%     
==========================================
  Files           6        6              
  Lines         670      861     +191     
==========================================
+ Hits          640      817     +177     
- Misses         18       32      +14     
  Partials       12       12              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This commit addresses a performance issue in the AllIntersections
function, where the conditional for traversing the right side of the
tree was evaluating to true most of the time. This led to unnecessary
visits of right nodes, resulting in linear time complexity.

More background on why this fix is correct:

- Left is safe to prune when all lower nodes end before the start of the
  lookup interval (`Left.MaxEnd < start`). (Note: this case was already
  correct before this commit, but I reversed the inequality as I find it
  easier to reason about)
- Right is safe to prune when all higher nodes start after the end of
  lookup interval (`end < Current.Start`). If the current node starts
  after the end of the lookup interval, we're guaranteed all its right
  nodes will also because they all start after the current node by
  definition.

Fuzz testing yielded consistent results with original implementation.
Benchmark showed ~30% speed improvement on average case (random
intervals), and over 99.9% improvement in worst case (lookup interval at
the far left of the tree). As expected, new implementation explores
fewer nodes that can be evicted just as safely in advance.
@rdleal
Copy link
Owner

rdleal commented Dec 16, 2024

Hey @gilbsgilbs sorry for the late review. It's been a tough quarter at work.

Your contribution makes total sense to me. Thank you!

@rdleal rdleal merged commit 178e917 into rdleal:main Dec 16, 2024
2 checks passed
@rdleal
Copy link
Owner

rdleal commented Dec 16, 2024

v1.4.1 published with the changes. Thank you again.

@gilbsgilbs
Copy link
Contributor Author

No need to apologize. Thanks for the review and release 🙂.

@federicojasson
Copy link
Contributor

federicojasson commented Jan 7, 2025

Just wanted to share some results after upgrading to v1.4.1. Great job!

Screenshot 2025-01-07 at 3 25 05 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants