Recoding closures in C

Jacek Generowicz jacek.generowicz at
Thu Jun 10 12:49:39 CEST 2004

Following on from my "Optimizing multiple dispatch" question last

Thanks to those who offered suggestions: recoding
tuple(map(type,args)) in C gives me a 20% speed improvement in "The
Politically Important Benchmark".

Now, in reaity, my code looks a bit more like this [though, still, I
am bending the truth siginificantly, in order to hide irrelevant
domain-specific details]:

class MethodCollection:


    def getMethod(dispatcher_self):

        def dispatching_method(self, *args):
            the_method = dispatcher_self.methods[tuple(map(type,args))]
            return the_method(*args)

        return dispatching_method

[Which is then used in a way which is broadly similar to this:

Foo_bar = MethodCollection()

Foo = type('Foo', (object,), {'bar': Foo_bar.getMethod()})


Now, "The Politically Imoprtant Benchmark" calls various incarantions
of dispatching_method in its inner loop (and the methods contained by
dispatching_method don't do much work), so I suspect that I could get
meaningful speed improvements by recoding dispatching_method in
C. However, note that it's a closure over dispatcher_self.

The most obvious way of doing this, seems to be to implement
dispatching_method as an extension type, with a tp_call slot which
implements the body of the original dispatching_method.

Any advice, comments, warnings ?

As an added complication, I would like the help for any given
dispatching_method incarnation to display the signatures of the
methods it holds. That's pretty easy in pure Python, but can it be
done if dispatching_method is written in C, (bearing in mind that
dispatching_methods are being created at run-time) ?

More information about the Python-list mailing list