[Python-Dev] Special-casing "O"

M.-A. Lemburg mal@lemburg.com
Fri, 25 May 2001 10:56:12 +0200


"Martin v. Loewis" wrote:
> 
> > Special-casing the snot out of "O" looks like a winner <wink>:
> 
> I have a patch on SF that takes this approach:
> 
> http://sourceforge.net/tracker/index.php?func=detail&aid=427190&group_id=5470&atid=305470
> 
> The idea is that functions can be declared as METH_O, instead of
> METH_VARARGS. I also offer METH_l, but this is currently not used. The
> approach could be extended to other signatures, e.g. METH_O_opt_O
> (i.e. "O|O").  Some signatures cannot be changed into special-calls,
> e.g. "O!", or "ll|l".
> 
> [benchmark]
> So the speed-up is roughly 30% to 50%, depending on how much work the
> function has to do.
> 
> Please let me know what you think.

Great idea, Martin.

One suggestion though: I would change is the way the
function is "declared" in the method list. Your currently use:

 {"append", (PyCFunction)listappend,  METH_O, append_doc},

Now this would be more flexible if you would implement a scheme
which lets us put the parser string into the method list. The
call mechanism could then easily figure out how to call the
method and it would also be more easily extensible:

 {"append", (PyCFunction)listappend,  METH_DIRECT, append_doc, "O"},

This would then (just like in your patch) call the listappend
function with the parser arguments inlined into the C call:

 listappend(self, arg0)

A parser marker "OO" would then call a method like this:

 method(self, arg0, arg1)

and so on.

This approach costs a little more (the string compare), but
should provide a more direct way of converting existing
functions to the new convention (just copy&paste the PyArg_ParseTuple()
argument) and also allows implementing a generic scheme which
then again relies on PyArg_ParseTuple() to do the argument
parsing, e.g. "is#" could be implemented as:

PyObject *method(PyObject self, int arg0, char *arg1, int *arg1_len)

For optional arguments we'd need some convention which then
lets the called function add the default value as needed.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/