Skip to content

Commit

Permalink
Problem: slow pkg_resources.Distribution.hashcmp
Browse files Browse the repository at this point in the history
Solution: cache it
  • Loading branch information
gotcha committed May 21, 2020
1 parent 16f9b86 commit 638d92a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Change History
3.0.0a2 (unreleased)
====================

- Nothing changed yet.
- Better patch for ``pkg_resources.Distribution.hashcmp`` performance.


3.0.0a1 (2020-05-17)
Expand Down
43 changes: 12 additions & 31 deletions src/zc/buildout/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,22 @@

def patch_Distribution():
try:
from pkg_resources import _remove_md5_fragment
from pkg_resources import Distribution

if hasattr(Distribution, 'location'):
return

# prepare any Distribution built before monkeypatch
from pkg_resources import working_set
for dist in working_set:
dist._location = dist.location
dist._location_without_md5 = _remove_md5_fragment(dist.location)

def hashcmp(self):
without_md5 = getattr(self, '_location_without_md5', '')
return (
self.parsed_version,
self.precedence,
self.key,
without_md5,
self.py_version or '',
self.platform or '',
)

def get_location(self):
try:
result = self._location
except AttributeError:
result = ''
return result

def set_location(self, l):
self._location = l
self._location_without_md5 = _remove_md5_fragment(l)
if hasattr(self, '_hashcmp'):
return self._hashcmp
else:
self._hashcmp = result = (
self.parsed_version,
self.precedence,
self.key,
self.location,
self.py_version or '',
self.platform or '',
)
return result

setattr(Distribution, 'location', property(get_location, set_location))
setattr(Distribution, 'hashcmp', property(hashcmp))
except ImportError:
return
Expand Down

0 comments on commit 638d92a

Please sign in to comment.