classification
Title: Add global instances of int 0 and 1
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, rhettinger, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2017-03-22 06:15 by serhiy.storchaka, last changed 2017-04-05 09:00 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
long-constants.diff serhiy.storchaka, 2017-03-22 06:15
Pull Requests
URL Status Linked Edit
PR 852 merged serhiy.storchaka, 2017-03-27 18:56
PR 1001 merged serhiy.storchaka, 2017-04-05 08:35
Messages (8)
msg289974 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-22 06:15
When C code needs to compare Python object with int 0 or add int 1 it either use local reference to PyLong_FromLong(0) and PyLong_FromLong(1) which should be decrefed just after use or module level global variable initialized and cleared during initializing and finalizing the module.

Proposed patch adds global variables _PyLong_Zero and _PyLong_One for references to integer objects 0 and 1. This simplifies the code since no need to initialize local variables, check for error the result of PyLong_FromLong() and decref it after use. The patch decreases the total code size by 244 lines.

That variables are only for internal use. User code should use PyLong_FromLong(0) and PyLong_FromLong(1).
msg289983 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-03-22 13:46
I like it.
msg289989 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-22 14:57
+1. Please create a PR for it.
msg289990 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-03-22 15:24
+1 for this idea.  

Also consider adding new function PyLong_Increment.  This basic operation is small pain using the current API.  It may also give a small speed benefit.
msg290000 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-03-22 16:49
Other common values used in C functions: empty byte string, empty Unicode string, empty tuple. The problem is to make sure that singletons are created in the right order :-/

This issue reminded me an old idea of writing a generalization of the _Py_IDENTIFIER() API. I created an issue #29881: Add a new private API for "static C variables" (_PyStaticVar) to clear them at exit. To be clear: it's related but different to this issue, the two issues are exclusive.
msg290005 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-22 18:02
Yet one idea that can make the code simpler is make PyLong_FromLong(0) and PyLong_FromLong(1) never failing. I.e. require NSMALLPOSINTS not less than 2.

> Also consider adding new function PyLong_Increment.  This basic operation is small pain using the current API.  It may also give a small speed benefit.

Smaller pain with using _PyLong_One and Py_SETREF().

    Py_SETREF(long_obj, PyNumber_Add(long_obj, _PyLong_One));

Agree that with _PyLong_Increment() it can look better and be faster. But I don't know whether incrementing by 1 is enough popular operation. I have counted 5 cases in the stdlib (not counting tests): for enumerate, range and Counter.

> The problem is to make sure that singletons are created in the right order :-/

Yes, I spent much time for making empty Unicode string singleton always be initialized. It can be accessed at very early stage.
msg290818 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-30 06:09
New changeset ba85d69a3e3610bdd05f0dd372cf4ebca178c7fb by Serhiy Storchaka in branch 'master':
bpo-29878: Add global instances of int for 0 and 1. (#852)
https://github.com/python/cpython/commit/ba85d69a3e3610bdd05f0dd372cf4ebca178c7fb
msg291170 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-04-05 09:00
New changeset bae6881b4215b2613ad08ef0dc7bed7743c2b8cc by Serhiy Storchaka in branch 'master':
Update Argument Clinic generated code for bpo-29878. (#1001)
https://github.com/python/cpython/commit/bae6881b4215b2613ad08ef0dc7bed7743c2b8cc
History
Date User Action Args
2017-04-05 09:00:44serhiy.storchakasetmessages: + msg291170
2017-04-05 08:35:45serhiy.storchakasetpull_requests: + pull_request1174
2017-03-30 06:20:51serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-03-30 06:09:43serhiy.storchakasetmessages: + msg290818
2017-03-27 18:56:52serhiy.storchakasetpull_requests: + pull_request752
2017-03-22 18:02:43serhiy.storchakasetmessages: + msg290005
2017-03-22 16:49:09vstinnersetmessages: + msg290000
2017-03-22 15:24:13rhettingersetnosy: + rhettinger
messages: + msg289990
2017-03-22 14:57:34vstinnersetmessages: + msg289989
2017-03-22 13:46:28mark.dickinsonsetmessages: + msg289983
2017-03-22 06:15:11serhiy.storchakacreate