classification
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
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: haypo, pitrou, python-dev, scoder, serhiy.storchaka
Priority: normal Keywords: patch

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

Files
File name Uploaded Description Edit
Py_ssize_t.patch haypo, 2016-08-24 12:11 review
Messages (3)
msg273549 - (view) Author: STINNER Victor (haypo) * (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) Date: 2016-08-24 22:07
New changeset 9479da828358 by Victor Stinner in branch 'default':
Use Py_ssize_t type for number of arguments
https://hg.python.org/cpython/rev/9479da828358
msg273608 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2016-08-24 22:45
I pushed Py_ssize_t.patch without the "const" ;-) Thanks for the review Antoine.
History
Date User Action Args
2016-08-24 22:45:00hayposetstatus: open -> closed
resolution: fixed
messages: + msg273608
2016-08-24 22:07:32python-devsetnosy: + python-dev
messages: + msg273603
2016-08-24 12:11:55hayposetnosy: + pitrou
type: enhancement
components: + Interpreter Core
2016-08-24 12:11:41haypocreate