Skip to content

Commit 1d19c27

Browse files
committed
speedup Wheel.support_index_min
Given an input tag list of size `n` and wheel file tags of size `m` this method is currently `O(n*m)` because it iterates over the set of file tags then for each file tag it iterates over the input tags. We can do much better and get `O(m)` time complexity by iterating the input tags instead, and doing a cheap `O(1)` membership test among the set of file tags. As a side benefit, this also allows early-termination of the loop. The impact of this seemingly trivial change is surprisingly big: for a run of `pip-compile` on macOS, which calls this method many times with large inputs this changes gives a ~50% speedup on end-to-end `pip-compile` time, from ~8s down to ~4s!
1 parent f20ab57 commit 1d19c27

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

news/4C988F0E-6A4D-45CE-A60F-9E86DB494FBF.trivial.rst

Whitespace-only changes.

src/pip/_internal/models/wheel.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ def support_index_min(self, tags: List[Tag]) -> int:
5858
:raises ValueError: If none of the wheel's file tags match one of
5959
the supported tags.
6060
"""
61-
return min(tags.index(tag) for tag in self.file_tags if tag in tags)
61+
try:
62+
return next(i for i, t in enumerate(tags) if t in self.file_tags)
63+
except StopIteration:
64+
raise ValueError()
6265

6366
def find_most_preferred_tag(
6467
self, tags: List[Tag], tag_to_priority: Dict[Tag, int]

0 commit comments

Comments
 (0)