Hi Leif, On 9 Sep., 22:41, leif <not.rea...@online.de> wrote: > I'm not sure whether one would notice (or even be able to measure) any > performance gain though, since for typical Sage objects most time is > certainly spent in actually computing the hash (assuming the > __hash__() methods are already cached, which I'm not sure about),
I do not suggest to rewrite cached __hash__ methods written in Cython. My suggestion is mainly (but not exclusively) addressed to Python classes inheriting from SageObject. If you write a Python class then a method with a cache implemented in Python will never be as fast as a Python class that inherits a method from a Cython class with a cache implemented in Cython. It turns out that one can not simply add a cdef attribute to SageObject: The result would be layout conflicts, for example for sequences, inheriting from both SageObject and list. Two random ideas: 1. One may create another layer: The current SageObject class could be renamed into SagePlainObject, so that Sequence can inherit from that and from list. The new version of SageObject will inherit from SagePlainObject, and adds the fast cached hash sketched above. In that way, all parents and elements will have access to the fast hash. Disadvantages: Perhaps in some cases it is a waste of memory to have an additional attribute for *each* object. And one would need to rename all the old __hash__ methods into _hash_. Tedious. 2. One could take UniqueRepresentation as a source of inspiration. Namely, one could create a Cython class called FastHash, roughly like this: cdef class FastHash(object): cdef object __hash def __hash__(self): if self.__hash is not None: return self.__hash self.__hash = hash(super(FastHash,self)) return self.__hash Now, if one writes a Python class that could benefit from a fast hash written in Cython, then one simply lets the Python class inherit from FastHash first. Advantage: One would spend the additional memory for the cache only were it makes sense, and using it is as easy as prepending FastHash to the list of base classes. So, it is like UniqueRepresentation: You prepend it to the list of base classes. Disadvantage: I tried the above, but multiple inheritance (such as "class Foo(FastHash,ZZ.__class__): pass") simply doesn't work, because there is a "class layout conflict". I don't understand how UniqueRepresentation is able to avoid that kind of problems. Perhaps I should learn more about __metaclass__, which is used by UniqueRepresentation? Best regards, Simon -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org