Repository navigation
gh-162749: Improve AC generated code for conversion of int to Py_ssize_t - #162750
eendebakpt wants to merge 5 commits into
Conversation
…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>
There was a problem hiding this comment.
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.
That would work (also performance wise), but the error messages would change.
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 |
|
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. |
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
For a
Py_ssize_tparameter, Argument Clinic generates a call to_PyNumber_Index()followed byPyLong_AsSsize_t()andPy_DECREF(). When the argument is already anint, which is the common case, we can avoid the incref/decref pair. Fornformat unit ofPyArg_Parse*()do the same. Identified when reviewing #162663update: refactored to use
PyNumber_AsSsize_twhich gains a fast path. Some error messages change (see comments below).Benchmark (first implementation)
b'abc'.center(5)data.pop(50); data.insert(50, 0)bisect.bisect_right(data, 50, 0, 100)b'abc'.center(idx)(object with__index__)Benchmark script
Developed with Claude Code.