From 70952aca35d7021dcce34149f38672aadd577f70 Mon Sep 17 00:00:00 2001 From: "Jens W. Klein" Date: Sun, 16 Feb 2020 18:09:08 +0100 Subject: [PATCH] Fixes #178: performace improvement by using objects hash --- CHANGES.rst | 10 +++------- src/zope/interface/interface.py | 9 +++------ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d0ee2e8a..923e3f03 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -18,13 +18,9 @@ value forces the Python implementation to be used, ignoring the C extensions. See `PR 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 - `_. +- Improve performance by not using a custom ``InterfaceClass.__hash__`` + function but use ``object.__hash__`` instead. For details see + `Issue 178 `_. - Change the C classes ``SpecificationBase`` and its subclass ``ClassProvidesBase`` to store implementation attributes in their structures diff --git a/src/zope/interface/interface.py b/src/zope/interface/interface.py index ade6f42b..2e7d76f2 100644 --- a/src/zope/interface/interface.py +++ b/src/zope/interface/interface.py @@ -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)