Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global instances of int 0 and 1 #74064

Closed
serhiy-storchaka opened this issue Mar 22, 2017 · 8 comments
Closed

Add global instances of int 0 and 1 #74064

serhiy-storchaka opened this issue Mar 22, 2017 · 8 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 29878
Nosy @rhettinger, @mdickinson, @vstinner, @serhiy-storchaka
PRs
  • bpo-29878: Add global instances of int for 0 and 1. #852
  • Update Argument Clinic generated code for bpo-29878. #1001
  • Files
  • long-constants.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-03-30.06:20:51.117>
    created_at = <Date 2017-03-22.06:15:11.395>
    labels = ['interpreter-core', 'type-feature', '3.7']
    title = 'Add global instances of int 0 and 1'
    updated_at = <Date 2017-04-05.09:00:44.630>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2017-04-05.09:00:44.630>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-03-30.06:20:51.117>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2017-03-22.06:15:11.395>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['46751']
    hgrepos = []
    issue_num = 29878
    keywords = ['patch']
    message_count = 8.0
    messages = ['289974', '289983', '289989', '289990', '290000', '290005', '290818', '291170']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'vstinner', 'serhiy.storchaka']
    pr_nums = ['852', '1001']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue29878'
    versions = ['Python 3.7']

    @serhiy-storchaka
    Copy link
    Member Author

    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).

    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Mar 22, 2017
    @mdickinson
    Copy link
    Member

    I like it.

    @vstinner
    Copy link
    Member

    +1. Please create a PR for it.

    @rhettinger
    Copy link
    Contributor

    +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.

    @vstinner
    Copy link
    Member

    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 bpo-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.

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset ba85d69 by Serhiy Storchaka in branch 'master':
    bpo-29878: Add global instances of int for 0 and 1. (#852)
    ba85d69

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset bae6881 by Serhiy Storchaka in branch 'master':
    Update Argument Clinic generated code for bpo-29878. (bpo-1001)
    bae6881

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants