Hi Nils,

On 2013-01-24, Nils Bruin <nbr...@sfu.ca> wrote:
>> > And that's a
>> > problem when cythoning UniqueRepresentation, because currently it is a
>> > Python class with __eq__, __ne__ and __hash__ implemented, but not with
>> > __richcmp__.
>
> That is NOT a problem! If you don't want to implement __le__, __lt__
> etc, just return the singleton NotImplemented and then python will
> continue its quest on finding an appropriate way to compare.

Thank you, I didn't know that. I hope the speed won't suffer too much
from it!

Question: When comparing with >=, would it work to do the following in
__richcmp__(self,other)?
    if self is other:
        return True
    return NotImplemented

That's to say, would this code snipped introduce a shortcut for
comparison when both sides are identical, so that python will only
continue its quest when the shortcut is not available?

> Instead, one would have to mandate creation via a factory function
> which can do the lookup (I guess metaclasses are just a way to
> formalize factory functions and incorporate them into inheritance).

To my understanding, UniqueRepresentation is indeed just a convenient
and inheritable way to create unique (parent) structures *plus* an
appropriate hash and __eq__ and __ne__, that is supposed to become a 
lot faster when cythoning the methods, *plus* it also gives pickling
for free, if I recall correctly.

Actually, the time for comparing two different instances of a class
inheriting from UniqueRepresentation already improves by 50%, if one
simply renames sage/structure/unique_representation.py into
sage/structure/unique_representation.pyx and updates module_list.py
So, there is a rather cheap why of getting a benefit from Cython in this
case.

> Are all our parents currently python classes then?

No. Some parents are cdef classes, and if they want to be unique
parents, then they should probably rely on UniqueFactory, which is a bit
more tedious to implement than UniqueRepresentation, but is also using
weak references (with #12313 at least).

> I guess we could
> have thin wrappers that simply are a multiple inheritance of a cython
> class (the actual implementation) and UniqueRepresentation,

You mean in order to make the default __hash__ and comparison of
UniqueRepresentation really fast? Yes, that's my plan.

> but in
> that case our parents could just as well be created by a weakly cached
> factory function, returning an instance of the relevant (cython) class.

Yes, that's what is done with UniqueFactory. It is also used when your
cache key is so tricky that implementing a custom __classcall__ seems to
be too complicated.

Cheers,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To post to this group, send email to sage-devel@googlegroups.com.
To unsubscribe from this group, send email to 
sage-devel+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.


Reply via email to