[Numpy-discussion] Getting C-function pointers from Python to C

Nathaniel Smith njs at pobox.com
Tue Apr 10 11:04:38 EDT 2012


On Tue, Apr 10, 2012 at 2:38 PM, Dag Sverre Seljebotn
<d.s.seljebotn at astro.uio.no> wrote:
> On 04/10/2012 03:29 PM, Nathaniel Smith wrote:
>> Right, that's what I wasn't getting until you mentioned strcmp :-).
>>
>> That said, the core numpy dtypes are singletons. For this purpose, the
>> signature could be stored as C array of PyArray_Descr*, but even if we
>> store it in a Python tuple/list, we'd still end up with a contiguous
>> array of PyArray_Descr*'s. (I'm assuming that we would guarantee that
>> it was always-and-only a real PyTupleObject* here.) So for the
>> function we're talking about, the check would compile down to doing
>> the equivalent of a 3*pointersize-byte strcmp, instead of a 5-byte
>> strcmp. That's admittedly worse, but I think the difference between
>> these two comparisons is unlikely to be measurable, considering that
>> they're followed immediately by a cache miss when we actually jump to
>> the function pointer.
>
> Yes, for singletons you're almost as good off. But if you have a struct
> argument, say
>
> void f(double x, struct {double a, float b} y);
>
> then PEP 3118 gives you the string "dT{dd}", whereas with NumPy dtypes
> you won't have a singleton?
>
> I can agree that that is a minor issue though (you could always *make*
> NumPy dtypes always be singleton).
>
> I think the real argument is that for Cython, it just wouldn't do to
> rely on NumPy dtypes (or NumPy being installed at all) for something as
> basic as calling to a C-level function; and strings are a simple substitute.
>
> And since it is a format defined in PEP 3118, NumPy should already
> support these kinds of strings internally (i.e. conversion to/from dtype).

Good points.

PEP 3118 is more thorough than I realized. Is it actually
canonical/implemented? The PEP says that all the added type syntax
will be added to struct, but that doesn't seem to have happened
(except for the "?" character, I guess).

-- Nathaniel



More information about the NumPy-Discussion mailing list