object's ob_item

8 views
Skip to first unread message

dg pb

unread,
Sep 2, 2025, 5:57:07 AM (10 days ago) Sep 2
to cython...@googlegroups.com
Hi all,

I am doing some work on sorting algorithms and hope to write something competitive with `list.sort` in Cython’s Pure Python mode.

One major obstacle for it is list operations.

Using `PyList_SETITEM` is very helpful, but it still has some overhead resulting in ~20% lower performance.

Is there a way to access list’s `ob_item` in Pure Python mode?

Regards,
dgpb

Stefan Behnel

unread,
Sep 2, 2025, 11:21:29 AM (9 days ago) Sep 2
to cython...@googlegroups.com
dg pb schrieb am 02.09.25 um 11:10:
You can declare the object struct yourself, cast the list object and
extract the array pointer.

You may also just use "&PyList_GET_ITEM(L, 0)" to get the pointer to the
first item and then treat it as the first entry of a "PyObject*" array.

However, note that this may not trivially work well and safely on recent
Python releases, especially in freethreading runtimes and with the recent
changes in refcounting. If it's for your local testing, that's probably
something you can live with, but a production-ready PyPI package might not
want to go that low. It's not unheard of, but it would mean that you'd have
to track CPython's core development branch to keep it working over time.
And it probably won't work on other Python implementations like PyPy.

Stefan

dg pb

unread,
Sep 2, 2025, 9:37:42 PM (9 days ago) Sep 2
to cython...@googlegroups.com
Thank you,

I was hoping to avoid declaring struct, but it seems I have no other good option.

Any chance it would be accepted into Cython’s list.pxd?
> --
>
> --- You received this message because you are subscribed to the Google Groups "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/cython-users/da3534c8-bd6f-4460-9764-e5a4590c4b08%40behnel.de.

Stefan Behnel

unread,
Sep 2, 2025, 11:21:14 PM (9 days ago) Sep 2
to cython...@googlegroups.com
dg pb schrieb am 02.09.25 um 21:39:
>> On 2 Sep 2025, at 18:21, 'Stefan Behnel' via cython-users <cython...@googlegroups.com> wrote:
>>
>> dg pb schrieb am 02.09.25 um 11:10:
>>> I am doing some work on sorting algorithms and hope to write something competitive with `list.sort` in Cython’s Pure Python mode.
>>> One major obstacle for it is list operations.
>>> Using `PyList_SETITEM` is very helpful, but it still has some overhead resulting in ~20% lower performance.
>>> Is there a way to access list’s `ob_item` in Pure Python mode?
>> You can declare the object struct yourself, cast the list object and extract the array pointer.
>>
>> You may also just use "&PyList_GET_ITEM(L, 0)" to get the pointer to the first item and then treat it as the first entry of a "PyObject*" array.
>
> I was hoping to avoid declaring struct, but it seems I have no other
> good option.

Reading the array pointer through "PyList_GET_ITEM" seems a much better
option to me.


> Any chance it would be accepted into Cython’s list.pxd?

It's Python version specific, so I'd rather not maintain it in there.

Stefan

Reply all
Reply to author
Forward
0 new messages