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

C function calls: use Py_ssize_t rather than C int for number of arguments #72035

Closed
vstinner opened this issue Aug 24, 2016 · 3 comments
Closed
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

BPO 27848
Nosy @pitrou, @scoder, @vstinner, @serhiy-storchaka
Files
  • Py_ssize_t.patch
  • 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 2016-08-24.22:45:00.150>
    created_at = <Date 2016-08-24.12:11:41.572>
    labels = ['interpreter-core', 'type-feature']
    title = 'C function calls: use Py_ssize_t rather than C int for number of arguments'
    updated_at = <Date 2016-08-24.22:45:00.149>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2016-08-24.22:45:00.149>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-08-24.22:45:00.150>
    closer = 'vstinner'
    components = ['Interpreter Core']
    creation = <Date 2016-08-24.12:11:41.572>
    creator = 'vstinner'
    dependencies = []
    files = ['44209']
    hgrepos = []
    issue_num = 27848
    keywords = ['patch']
    message_count = 3.0
    messages = ['273549', '273603', '273608']
    nosy_count = 5.0
    nosy_names = ['pitrou', 'scoder', 'vstinner', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue27848'
    versions = ['Python 3.6']

    @vstinner
    Copy link
    Member Author

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

    @vstinner vstinner added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Aug 24, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 24, 2016

    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

    @vstinner
    Copy link
    Member Author

    I pushed Py_ssize_t.patch without the "const" ;-) Thanks for the review Antoine.

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant