Hi Nicolas, Florent and Maarten, On 13 Sep., 09:36, "Nicolas M. Thiery" <nicolas.thi...@u-psud.fr> wrote: > On Tue, Sep 13, 2011 at 09:21:45AM +0200, Nicolas M. Thiery wrote: > > What does your FastHashMetaclass do precisely? > > Oops, I had not read the other thread yet ...
I just created a ticket for it: #11794. The new metaclass provides a fast hash, and thus can be useful in applications that frequently work with, say, dictionary keys. However, there is no free lunch. The fast hash currently comes with a regression in accessing the other attributes: {{{ sage: from sage.misc.fast_hash import FastHashMetaclass sage: F = CombinatorialFreeModule(QQ, ['a','b']) sage: h = hash(F) sage: timeit("hash(F)") 625 loops, best of 3: 1.1 µs per loop sage: D = dir(F) sage: timeit('for s in D: a = getattr(F,s)', number=10000) 10000 loops, best of 3: 86.8 µs per loop sage: F.__class__ = FastHashMetaclass(F.__class__) sage: timeit("hash(F)") 625 loops, best of 3: 440 ns per loop sage: timeit('for s in D: a = getattr(F,s)', number=10000) 10000 loops, best of 3: 97.3 µs per loop }}} To my understanding, the reason is that my metaclass adds a new layer in the mro, and thus more steps in the mro are needed to find the old methods. I'll try to find a better solution. Fortunately, both the speed-up and the regression are temporary: {{{ sage: F.__class__ = F.__class__.__base__ sage: timeit("hash(F)") 625 loops, best of 3: 1.09 µs per loop sage: timeit('for s in D: a = getattr(F,s)', number=10000) 10000 loops, best of 3: 86.3 µs per loop }}} Hence, I would not suggest to use the FastHashMetaclass by default. But perhaps it is useful in some situations. The newly created classes seem to behave well: {{{ sage: F.__class__ = FastHashMetaclass(F.__class__) sage: TestSuite(F).run() }}} And, concerning dynamic metaclasses: Here we go. {{{ sage: type(F) <class 'sage.combinat.free_module.CombinatorialFreeModule_with_category_with_hash'> sage: type(type(F)) <class 'sage.misc.fast_hash.FastHashAndDynamicClasscallMetaclass'> sage: type(type(type(F))) <class 'sage.structure.dynamic_class.DynamicMetaclass'> }}} So, the type of F has a metaclass that is a dynamic class and thus has a metaclass as well. 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