Skip to content

Commit

Permalink
Use functools.lru_cache to cache supported tags for wheels.
Browse files Browse the repository at this point in the history
This is a suggestion by @abravalheri for my PR.
#3805 (comment)
  • Loading branch information
mauritsvanrees committed Mar 7, 2023
1 parent 2c23449 commit 8af6bd4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
14 changes: 11 additions & 3 deletions setuptools/tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,17 @@ def sys_tags():
for t in parse_tag('cp36-cp36m-manylinux1_x86_64'):
yield t
monkeypatch.setattr('setuptools.wheel.sys_tags', sys_tags)
monkeypatch.setattr('setuptools.wheel._supported_tags', None)
assert Wheel(
'onnxruntime-0.1.2-cp36-cp36m-manylinux1_x86_64.whl').is_compatible()
# Clear the supported tags cache, otherwise the sys_tags monkeypatch
# has no effect.
setuptools.wheel._supported_tags.cache_clear()
try:
assert Wheel(
'onnxruntime-0.1.2-cp36-cp36m-manylinux1_x86_64.whl'
).is_compatible()
finally:
# Clear the cache again, otherwise the sys_tags monkeypatch
# is still in effect for the rest of the tests.
setuptools.wheel._supported_tags.cache_clear()


def test_wheel_mode():
Expand Down
17 changes: 9 additions & 8 deletions setuptools/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import email
import itertools
import functools
import os
import posixpath
import re
Expand All @@ -27,7 +28,13 @@
NAMESPACE_PACKAGE_INIT = \
"__import__('pkg_resources').declare_namespace(__name__)\n"

_supported_tags = None

@functools.lru_cache(maxsize=None)
def _get_supported_tags():
# We calculate the supported tags only once, otherwise calling
# this method on thousands of wheels takes seconds instead of
# milliseconds.
return set((t.interpreter, t.abi, t.platform) for t in sys_tags())


def unpack(src_dir, dst_dir):
Expand Down Expand Up @@ -85,13 +92,7 @@ def tags(self):

def is_compatible(self):
'''Is the wheel compatible with the current platform?'''
global _supported_tags
if _supported_tags is None:
# We calculate the supported tags only once, otherwise calling
# this method on thousands of wheels takes seconds instead of
# milliseconds.
_supported_tags = set(
(t.interpreter, t.abi, t.platform) for t in sys_tags())
_supported_tags = _get_supported_tags()
return next((True for t in self.tags() if t in _supported_tags), False)

def egg_name(self):
Expand Down

0 comments on commit 8af6bd4

Please sign in to comment.