Skip to content

Commit

Permalink
Early out for non-overlapping intervals in __and__
Browse files Browse the repository at this point in the history
In cases where intervals are frequently compared against other
intervals they do not intersect with, a lot of unnecessary work is
done. This adds a shortcut for that case.
  • Loading branch information
jefftrull authored and AlexandreDecan committed Sep 12, 2021
1 parent c48515d commit ca7d791
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion portion/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ def __and__(self, other):
if not isinstance(other, Interval):
return NotImplemented

if self.atomic and other.atomic:
if (self.upper < other.lower) or (self.lower > other.upper):
# early out for non-overlapping intervals
intersections = []
return self.__class__(*intersections)
elif self.atomic and other.atomic:
if self.lower == other.lower:
lower = self.lower
left = self.left if self.left == Bound.OPEN else other.left
Expand Down

0 comments on commit ca7d791

Please sign in to comment.