Title: C function calls: use Py_ssize_t rather than C int for number of arguments
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: pitrou, python-dev, scoder, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2016-08-24 12:11 by vstinner, last changed 2016-08-24 22:45 by vstinner. This issue is now closed.

File name Uploaded Description Edit
Py_ssize_t.patch vstinner, 2016-08-24 12:11 review
Messages (3)
msg273549 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-08-24 12:11
I recently added new functions to call functions in C, especially _PyObject_FastCallDict(). I started with the C int type for the number of positional arguments: "int nargs".

But slowly, when I started to patch code to use _PyObject_FastCallDict(), I used more and more Py_ssize_t variables for the number of arguments, variable downcasted to int to call _PyObject_FastCallDict().

It is similar to the old issue #18295.

I propose to avoid any risk of integer overflow by using Py_ssize_t everywhere. It *might* produce more efficient machine code, since "i++" may require extra code to handle overflow when the int rather is used. But I'm not sure that Py_ssize_t is better, especially when -fwrapv option of the GCC compiler is used.

Attached patch implements this idea.

The patch also uses Py_ssize_t for some BUILD_xxx opcodes like BUILD_LIST_UNPACK. The change is not directly related. I wrote it in the hope that machine code could be more efficient, and avoid having to ask myself if types could overflow or not.
msg273603 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-08-24 22:07
New changeset 9479da828358 by Victor Stinner in branch 'default':
Use Py_ssize_t type for number of arguments
msg273608 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-08-24 22:45
I pushed Py_ssize_t.patch without the "const" ;-) Thanks for the review Antoine.
Date User Action Args
2016-08-24 22:45:00vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg273608
2016-08-24 22:07:32python-devsetnosy: + python-dev
messages: + msg273603
2016-08-24 12:11:55vstinnersetnosy: + pitrou
type: enhancement
components: + Interpreter Core
2016-08-24 12:11:41vstinnercreate