Skip to content

Commit

Permalink
Fixes #178: performace improvement by using objects hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Feb 16, 2020
1 parent 7f6f60e commit 70952ac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
10 changes: 3 additions & 7 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@
value forces the Python implementation to be used, ignoring the C
extensions. See `PR 151 <https://github.com/zopefoundation/zope.interface/pull/151>`_.

- Cache the result of ``__hash__`` method in ``InterfaceClass`` as a
speed optimization. The method is called very often (i.e several
hundred thousand times during 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>`_.
- Improve performance by not using a custom ``InterfaceClass.__hash__``
function but use ``object.__hash__`` instead. For details see
`Issue 178 <https://github.com/zopefoundation/zope.interface/pull/156>`_.

- Change the C classes ``SpecificationBase`` and its subclass
``ClassProvidesBase`` to store implementation attributes in their structures
Expand Down
9 changes: 3 additions & 6 deletions src/zope/interface/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,9 @@ def __cmp(self, other):
# This spelling works under Python3, which doesn't have cmp().
return (n1 > n2) - (n1 < n2)

def __hash__(self):
try:
return self._v_cached_hash
except AttributeError:
self._v_cached_hash = hash((self.__name__, self.__module__))
return self._v_cached_hash
# use the very fast object hash function, no need for custom impl
# see https://github.com/zopefoundation/zope.interface/issues/178
__hash__ = object.__hash__

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

0 comments on commit 70952ac

Please sign in to comment.