[Cython] Optimizing fastcall

da-woods dw-git at d-woods.co.uk
Thu Feb 6 07:51:13 EST 2020


Related to https://github.com/cython/cython/issues/3343

One issue with the current code generated for fastcall function is that 
for a function like:

def f(a, *args, **kwds):
     # something...

the wrapper function immediately generates a tuple for `args` and a dict 
for `kwds`, which presumably misses most of the benefits of the more 
efficient calling method. It therefore seems like a good idea to have an 
optional optimization so that the more efficient representation can be 
used (close to) directly.

I've created an initial attempt at it 
https://github.com/cython/cython/pull/3346. This defines a complier 
directive so that:

@cython.fastcall_args("*" or "**" or "both")
def function...

is converted to a relatively simple structure type. The tuple equivalent is

typedef struct {
     PyObject *const *args;
     Py_ssize_t nargs;
} __Pyx_FastcallTuple_obj;

and the dict equivalent a little more complicated is

typedef struct {
     PyObject *const *args; // start of the keyword args values
     PyObject *object;      // either a dict, a tuple or NULL
} __Pyx_FastcallDict_obj;
// exists in one of three states:
// * args is NULL, "object" is NULL, meaning no keyword arguments
// * args is NULL, in which case "object" is actually a dict, and this 
just defers to the dict methods
// * args is non-null, kwnames is a tuple

These then support simple operations (indexing, iterating, passing 
directly to a fastcall function call etc). For most non-simple 
operations they will just be coerced to a Python object with a warning 
(but trying to put them in a closure will explicitly fail).

-------------------------------------

I suspect my proposed code is still a bit of a mess, so I'm not looking 
for too much scrutiny of that right now. What I'm interested in is:
* is the syntax to use this sensible? (it's probably easily changed)
* is the level of warnings appropriate?
* any operations that I've missed that'd be sensible to support?
* anything else?

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20200206/635c7bc9/attachment.html>


More information about the cython-devel mailing list