Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Implement __Pyx_PyObject_LengthHint properly on the limited API #6121

Open
da-woods opened this issue Apr 1, 2024 · 1 comment
Open

Comments

@da-woods
Copy link
Contributor

da-woods commented Apr 1, 2024

Describe your issue

#6108 did the bare minimum implementation of PyObject_LengthHint needed to compile and run in the Limited API - essentially "length hint is always optional so just fail to find it and return the default value".

That's fine in that it works and is no worse than the code that's in Cython 3.0 right now (which doesn't use length hint). However there's no reason not to do it right.

The implementation in Python is at https://github.com/python/cpython/blob/dd44ab994b7262f0704d64996e0a1bc37b233407/Objects/abstract.c#L92 and can likely be adapted.

@scoder
Copy link
Contributor

scoder commented Apr 1, 2024

However there's no reason not to do it right.

Except speed. For short data structures (which are not uncommon), even just trying to read the length hint from a Python attribute and unpacking it might take a major part of the time for building the entire C/C++ data structure. The slower the sequence of C-API calls for pre-sizing the container, the larger the data structures for which it degrades the performance.

However, it might actually be worth inlining the fast path of PyObject_Length() for CPython, i.e. checking sq_length and calling it to get a size. Most containers will provide it, and generators etc. won't have it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants