Recoding closures in C

J

Jacek Generowicz

Following on from my "Optimizing multiple dispatch" question last
week:

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_bar.add_method(this)
Foo_bar.add_method(that)
Foo_bar.add_method(the_other)
....

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) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top