diff options
author | Jens W. Klein <jk@kleinundpartner.at> | 2020-02-17 00:37:45 +0100 |
---|---|---|
committer | Jens W. Klein <jk@kleinundpartner.at> | 2020-02-17 00:51:11 +0100 |
commit | 9c90b934a8550f7fde566891667923cbde7f6035 (patch) | |
tree | 6ab77d08ee3097df884ff1e1d895295c4594838d | |
parent | 7f6f60e824ac34e2bf9ec890e6c361f57bfc823b (diff) | |
download | zope-interface-9c90b934a8550f7fde566891667923cbde7f6035.tar.gz |
performance: store cached hashvalue in slot (~1.6x faster)hash-performance
-rw-r--r-- | src/zope/interface/_zope_interface_coptimizations.c | 4 | ||||
-rw-r--r-- | src/zope/interface/declarations.py | 2 | ||||
-rw-r--r-- | src/zope/interface/interface.py | 8 |
3 files changed, 11 insertions, 3 deletions
diff --git a/src/zope/interface/_zope_interface_coptimizations.c b/src/zope/interface/_zope_interface_coptimizations.c index f4f9cce..5ed60eb 100644 --- a/src/zope/interface/_zope_interface_coptimizations.c +++ b/src/zope/interface/_zope_interface_coptimizations.c @@ -277,6 +277,7 @@ typedef struct { PyObject* _v_attrs; PyObject* __iro__; PyObject* __sro__; + PyObject* _hashvalue; } Spec; /* @@ -291,6 +292,7 @@ Spec_traverse(Spec* self, visitproc visit, void* arg) Py_VISIT(self->_v_attrs); Py_VISIT(self->__iro__); Py_VISIT(self->__sro__); + Py_VISIT(self->_hashvalue); return 0; } @@ -302,6 +304,7 @@ Spec_clear(Spec* self) Py_CLEAR(self->_v_attrs); Py_CLEAR(self->__iro__); Py_CLEAR(self->__sro__); + Py_CLEAR(self->_hashvalue); return 0; } @@ -413,6 +416,7 @@ static PyMemberDef Spec_members[] = { {"_v_attrs", T_OBJECT_EX, offsetof(Spec, _v_attrs), 0, ""}, {"__iro__", T_OBJECT_EX, offsetof(Spec, __iro__), 0, ""}, {"__sro__", T_OBJECT_EX, offsetof(Spec, __sro__), 0, ""}, + {"_hashvalue", T_OBJECT_EX, offsetof(Spec, _hashvalue), 0, ""}, {NULL}, }; diff --git a/src/zope/interface/declarations.py b/src/zope/interface/declarations.py index 3118593..d061905 100644 --- a/src/zope/interface/declarations.py +++ b/src/zope/interface/declarations.py @@ -180,6 +180,8 @@ class _ImmutableDeclaration(Declaration): # object, and that includes a method.) return _ImmutableDeclaration + _hashvalue = hash((__module__, '_empty')) + ############################################################################## # diff --git a/src/zope/interface/interface.py b/src/zope/interface/interface.py index ade6f42..0f667a6 100644 --- a/src/zope/interface/interface.py +++ b/src/zope/interface/interface.py @@ -131,6 +131,8 @@ class SpecificationBase(object): '__iro__', '__sro__', '__weakref__', + # Things used in InterfaceClass. + '_hashvalue', ) def providedBy(self, ob): @@ -598,10 +600,10 @@ class InterfaceClass(Element, InterfaceBase, Specification): def __hash__(self): try: - return self._v_cached_hash + return self._hashvalue except AttributeError: - self._v_cached_hash = hash((self.__name__, self.__module__)) - return self._v_cached_hash + self._hashvalue = hash((self.__name__, self.__module__)) + return self._hashvalue def __eq__(self, other): c = self.__cmp(other) |