Skip to content

gh-106320: Re-add some PyLong/PyDict C-API functions - #111162

Merged
encukou merged 2 commits into
python:mainfrom
scoder:readd_pylong_capi
Oct 25, 2023
Merged

encukou merged 2 commits into
python:mainfrom
scoder:readd_pylong_capi

Conversation

@scoder

@scoder scoder commented Oct 21, 2023 •

Copy link
Copy Markdown
Contributor

Re-add _PyLong_FromByteArray(), _PyLong_AsByteArray(),_PyLong_GCD() and _PyDict_Pop() to the public header files.
They are used by third-party packages and there is no efficient replacement.

See #111140
See #111139
See #111262

…) and _PyLong_GCD() to the public header files since they are used by third-party packages and there is no efficient replacement.

See python#111140
See python#111139
@scoder

scoder commented Oct 21, 2023

Copy link
Copy Markdown
Contributor Author
@vstinner

Copy link
Copy Markdown
Member

I would prefer to take it as an opportunity to add a nice public API for that. In the meanwhile (until next alpha release), you can just use the internal C API.

@scoder

scoder commented Oct 22, 2023 via email

Copy link
Copy Markdown
Contributor Author
@scoder

scoder commented Oct 22, 2023

Copy link
Copy Markdown
Contributor Author

Just for the record, I micro-benchmarked _PyLong_AsByteArray() against the C-API call to int.to_bytes() and the incremental unpacking of 62 bit chunks using PyNumber_And(), PyLong_AsLong() and PyNumber_Rshift(). The code complexity increases substantially from first to last in that order.

# int.to_bytes()
$ python3.10 -m timeit -s 'from intbench import i128_from_py' 'i128_from_py(2**126)'
200000 loops, best of 5: 1.06 usec per loop

# incremental unpacking
$ python3.10 -m timeit -s 'from intbench import i128_from_py' 'i128_from_py(2**126)'
500000 loops, best of 5: 693 nsec per loop

# _PyLong_AsByteArray()
$ python3.10 -m timeit -s 'from intbench import i128_from_py' 'i128_from_py(2**126)'
500000 loops, best of 5: 504 nsec per loop

This suggests to me that making _PyLong_AsByteArray() public is worth it. The replacements are just horrible to implement, and the "obvious replacement" is really slow.

@encukou

encukou commented Oct 23, 2023

Copy link
Copy Markdown
Member

I would prefer adding these back, so that we can take our time to add a proper replacement.
Or, Victor, do you think you can design and add a good replacement in time for the next alpha?

@scoder scoder changed the title gh-106320: Re-add some PyLong C-API functions Oct 25, 2023

@encukou encukou left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't see the reason why this API was removed. PEP-387 says that it can be removed, not that it should.
We don't have a good replacement for this API. After we add that, it'll be time to start telling people to change their code.

I take @gvanrossum's comment on another issue as support for reverting the removal:

I strongly prefer revert over fix, for removed APIs that cause problems.

@encukou
encukou merged commit a8a89fc into python:main Oct 25, 2023
@scoder
scoder deleted the readd_pylong_capi branch October 25, 2023 11:12
@vstinner

Copy link
Copy Markdown
Member

Victor, do you think you can design and add a good replacement in time for the next alpha?

Yes, I plan to add public APIs to replace these useful functions. I created multiple issues to track that: issues listed in the PR description.

aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…#111162)

* pythongh-106320: Re-add _PyLong_FromByteArray(), _PyLong_AsByteArray() and _PyLong_GCD() to the public header files since they are used by third-party packages and there is no efficient replacement.

See python#111140
See python#111139

* pythongh-111262: Re-add _PyDict_Pop() to have a C-API until a new public one is designed.
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…#111162)

* pythongh-106320: Re-add _PyLong_FromByteArray(), _PyLong_AsByteArray() and _PyLong_GCD() to the public header files since they are used by third-party packages and there is no efficient replacement.

See python#111140
See python#111139

* pythongh-111262: Re-add _PyDict_Pop() to have a C-API until a new public one is designed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants