Skip to content

gh-162749: Improve AC generated code for conversion of int to Py_ssize_t - #162750

Open
eendebakpt wants to merge 5 commits into
python:mainfrom
eendebakpt:ac-ssize-t-asssize
Open

eendebakpt wants to merge 5 commits into
python:mainfrom
eendebakpt:ac-ssize-t-asssize

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

For a Py_ssize_t parameter, Argument Clinic generates a call to _PyNumber_Index() followed by PyLong_AsSsize_t() and Py_DECREF(). When the argument is already an int, which is the common case, we can avoid the incref/decref pair. For n format unit of PyArg_Parse*() do the same. Identified when reviewing #162663

update: refactored to use PyNumber_AsSsize_t which gains a fast path. Some error messages change (see comments below).

Benchmark (first implementation)

Statement main PR
b'abc'.center(5) 80.4 ns 80.3 ns 1.00x
data.pop(50); data.insert(50, 0) 140.2 ns 135.6 ns 1.03x
bisect.bisect_right(data, 50, 0, 100) 150.4 ns 148.0 ns 1.02x
b'abc'.center(idx) (object with __index__) 162.5 ns 162.8 ns 1.00x
Benchmark script
import timeit

setup = """
import bisect
data = list(range(100))
class Index:
    def __index__(self):
        return 5
idx = Index()
"""
cases = [
    "b'abc'.center(5)",
    "data.pop(50); data.insert(50, 0)",
    "bisect.bisect_right(data, 50, 0, 100)",
    "b'abc'.center(idx)",
]
N = 500_000
for stmt in cases:
    best = min(timeit.repeat(stmt, setup, repeat=5, number=N)) / N
    print(f"{stmt}\t{best * 1e9:.1f}")

Developed with Claude Code.

eendebakpt and others added 2 commits November 27, 2026 16:24
…y_ssize_t

Add _PyNumber_IndexAsSsize_t() and use it in Argument Clinic and in the
'n' format unit of PyArg_Parse. Also add a fast path for int to
PyNumber_AsSsize_t().

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@cmaloney cmaloney left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice find small win per case but with AC that definitely adds up!

Rather than adding a new function can we just use PyNumber_AsSsize_t(x, PyExc_OverflowError) and rely on fast-paths inside of it?

I think that should keep most the code complexity and size wins.

@eendebakpt

Copy link
Copy Markdown
Contributor Author

Nice find small win per case but with AC that definitely adds up!

Rather than adding a new function can we just use PyNumber_AsSsize_t(x, PyExc_OverflowError) and rely on fast-paths inside of it?

That would work (also performance wise), but the error messages would change.

Message for b'abc'.center(2**100)
main / this PR Python int too large to convert to C ssize_t
PyNumber_AsSsize_t(x, PyExc_OverflowError) cannot fit 'int' into an index-sized integer

There is one test that explicitly tests for the message test_zlib.test_overflow, so I decided to keep the error messages the same.

Using PyNumber_AsSsize_t changes the PR into main...eendebakpt:ac-ssize-t-number-asssize. If people are ok with changing the message, I am as well I think.

@cmaloney

Copy link
Copy Markdown
Contributor

For me changing the exception text is okay. Argument text is not guaranteed to stay the same over time; only exception type and member attributes are. I personally prefer having less distinct forms for things. The Argument Clinic (AC) adoption is doing similarly in reducing how many different argument count errors are phrased.

In the AC Vectorcall work I spent a lot of time pre-PR getting so that the same errors would happen in the same precedence as the non-vectorcall. Generally I hope by having less sites we can also make error messages better more easily.

eendebakpt and others added 2 commits December 1, 2026 00:15
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
# Conflicts:
#	Modules/clinic/_elementtree.c.h
#	Modules/clinic/_operator.c.h
#	Modules/clinic/_testclinic.c.h
#	Modules/clinic/_testclinic_kwds.c.h
#	Modules/clinic/arraymodule.c.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants