Skip to content

Commit

Permalink
markers: avoid unnecessary stack depth (#848)
Browse files Browse the repository at this point in the history
This is a slight performance optimization and preliminary work for improvements to `union_simplify` and `intersect_simplify`. The `SingleMarker` branch finally ends in the multi/union branch via some function calls anyway, so we can shortcut it by checking for multi/union markers first.
  • Loading branch information
radoering authored Mar 11, 2025
1 parent 5209a79 commit 776fb4b
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,24 @@ def of(cls, *markers: BaseMarker) -> BaseMarker:

intersected = False
for i, mark in enumerate(new_markers):
# If we have a MarkerUnion then we can look for the simplifications
# implemented in intersect_simplify().
intersection: BaseMarker | None = None
is_one_union = False
if isinstance(mark, MarkerUnion):
is_one_union = True
intersection = mark.intersect_simplify(marker)
elif isinstance(marker, MarkerUnion):
is_one_union = True
intersection = marker.intersect_simplify(mark)
if intersection is not None:
new_markers[i] = intersection
intersected = True
break

# If we have a SingleMarker then with any luck after intersection
# it'll become another SingleMarker.
if isinstance(mark, SingleMarkerLike):
if not is_one_union and isinstance(mark, SingleMarkerLike):
new_marker = mark.intersect(marker)
if new_marker.is_empty():
return EmptyMarker()
Expand All @@ -671,15 +686,6 @@ def of(cls, *markers: BaseMarker) -> BaseMarker:
intersected = True
break

# If we have a MarkerUnion then we can look for the simplifications
# implemented in intersect_simplify().
elif isinstance(mark, MarkerUnion):
intersection = mark.intersect_simplify(marker)
if intersection is not None:
new_markers[i] = intersection
intersected = True
break

if intersected:
# flatten again because intersect_simplify may return a multi
new_markers = _flatten_markers(new_markers, MultiMarker)
Expand Down Expand Up @@ -833,9 +839,24 @@ def of(cls, *markers: BaseMarker) -> BaseMarker:

included = False
for i, mark in enumerate(new_markers):
# If we have a MultiMarker then we can look for the simplifications
# implemented in union_simplify().
union_: BaseMarker | None = None
is_one_multi = False
if isinstance(mark, MultiMarker):
is_one_multi = True
union_ = mark.union_simplify(marker)
elif isinstance(marker, MultiMarker):
is_one_multi = True
union_ = marker.union_simplify(mark)
if union_ is not None:
new_markers[i] = union_
included = True
break

# If we have a SingleMarker then with any luck after union it'll
# become another SingleMarker.
if isinstance(mark, SingleMarkerLike):
if not is_one_multi and isinstance(mark, SingleMarkerLike):
new_marker = mark.union(marker)
if new_marker.is_any():
return AnyMarker()
Expand All @@ -845,15 +866,6 @@ def of(cls, *markers: BaseMarker) -> BaseMarker:
included = True
break

# If we have a MultiMarker then we can look for the simplifications
# implemented in union_simplify().
elif isinstance(mark, MultiMarker):
union = mark.union_simplify(marker)
if union is not None:
new_markers[i] = union
included = True
break

if included:
# flatten again because union_simplify may return a union
new_markers = _flatten_markers(new_markers, MarkerUnion)
Expand Down

0 comments on commit 776fb4b

Please # to comment.