Skip to content

Commit

Permalink
Merge pull request #156 from zopefoundation/hash_performance
Browse files Browse the repository at this point in the history
Improve InterfaceClass __hash__ performance
  • Loading branch information
jensens committed Jan 27, 2020
2 parents 02e11f2 + 32726fa commit eeaacb6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
an undocumented private C API function, and helps make some
instances require less memory. See `PR 154 <https://github.com/zopefoundation/zope.interface/pull/154>`_.

- Performance optimization of ``__hash__`` method on ``InterfaceClass``.
The method is called very often (i.e several 100.000 times on a Plone 5.2
startup). Because the hash value never changes it can be cached.
This improves test performance from 0.614s down to 0.575s (1.07x faster).
In a real world Plone case a reindex index came down from 402s to 320s (1.26x faster).
See `PR 156 <https://github.com/zopefoundation/zope.interface/pull/156>`_.


4.7.1 (2019-11-11)
==================
Expand Down
14 changes: 9 additions & 5 deletions src/zope/interface/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,15 @@ def __cmp(self, other):
return (n1 > n2) - (n1 < n2)

def __hash__(self):
d = self.__dict__
if '__module__' not in d or '__name__' not in d: # pragma: no cover
warnings.warn('Hashing uninitialized InterfaceClass instance')
return 1
return hash((self.__name__, self.__module__))
try:
return self._v_cached_hash
except AttributeError:
try:
self._v_cached_hash = hash((self.__name__, self.__module__))
except AttributeError: # pragma: no cover
warnings.warn('Hashing uninitialized InterfaceClass instance')
return 1
return self._v_cached_hash

def __eq__(self, other):
c = self.__cmp(other)
Expand Down

0 comments on commit eeaacb6

Please sign in to comment.