Hi all,

is anyone interested in digging into speeding up __array_function__?  I
distracted myself briefly looking into moving (more) of it to C and got
up with this:

https://github.com/numpy/numpy/compare/main...seberg:faster-array-function

The effect seems to be an up to 40% speed improvement for very
lightweight functions.  (There may be more room for improvement but I
am not sure how much and it should not be as easy.)


I had never really tried this before, because on older Python versions
it was just slower/harder to jump between C and Python.  That was due
to the lack of "vectorcall", now it is all there, although for PyPy it
might still need fixups.

The main point is moving the object that we replace our functions with
(the entry point) to C [1].  This works, but it was a one-evening quick
try, so it needs a lot of polishing:

* Fixing `like=` for Python defined functions:  They are broken, I have
  one possible approach, but it is just an "idea stage".
  (should not be hard, but needs a some thought)
* Make sure the C entry-point looks and feel right, such as printing
  (I am not sure what is left to do here, maybe we can find prior art?)
* Enable pickling (should be very easy)
* Ensure PyPy compatibility if necessary
* Change all warning `stacklevel=` to one lower (in wrapped functions).
  (simple, but a lot of churn)
* General code cleanup

Thought I would put it out there.  It might be a cool and worthwhile
project for someone interested in decreasing overheads and familiar
with the Python C-API.

Cheers,

Sebastian


[1] This also avoids the need for packing `args` and `kwargs` into a
tuple/dict, which is faster, even if we have to pack it again for
`__array_function__` (we can avoid it for the NumPy version though).
I had wondered if we could evolve `__array_function__` to accept
`*args, **kwargs`.  I expect this is only useful if
`__array_function__` was defined in C though.

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to