Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve InterfaceClass __hash__ performance #156

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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