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

Replace size_t with Py_ssize_t as the type of local variable in list_resize #71847

Closed
zhangyangyu opened this issue Jul 31, 2016 · 22 comments
Closed
Assignees
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@zhangyangyu
Copy link
Member

BPO 27660
Nosy @tim-one, @rhettinger, @mdickinson, @vstinner, @bitdancer, @vadmium, @serhiy-storchaka, @zhangyangyu, @Mariatta
PRs
  • bpo-27660: remove unnecessary overflow checks in list_resize #189
  • Files
  • list_resize.patch
  • list_resize_v2.patch
  • list_resize_v3.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 = 'https://github.com/zhangyangyu'
    closed_at = <Date 2017-02-22.04:32:53.486>
    created_at = <Date 2016-07-31.17:40:09.746>
    labels = ['interpreter-core', 'type-feature', '3.7']
    title = 'Replace size_t with Py_ssize_t as the type of local variable in list_resize'
    updated_at = <Date 2017-02-22.04:32:53.485>
    user = 'https://github.com/zhangyangyu'

    bugs.python.org fields:

    activity = <Date 2017-02-22.04:32:53.485>
    actor = 'xiang.zhang'
    assignee = 'xiang.zhang'
    closed = True
    closed_date = <Date 2017-02-22.04:32:53.486>
    closer = 'xiang.zhang'
    components = ['Interpreter Core']
    creation = <Date 2016-07-31.17:40:09.746>
    creator = 'xiang.zhang'
    dependencies = []
    files = ['43957', '43976', '44192']
    hgrepos = []
    issue_num = 27660
    keywords = ['patch']
    message_count = 22.0
    messages = ['271746', '271748', '271749', '271765', '271784', '271785', '271799', '271806', '271808', '271827', '271829', '273378', '273398', '273401', '273418', '288187', '288256', '288260', '288264', '288266', '288268', '288328']
    nosy_count = 9.0
    nosy_names = ['tim.peters', 'rhettinger', 'mark.dickinson', 'vstinner', 'r.david.murray', 'martin.panter', 'serhiy.storchaka', 'xiang.zhang', 'Mariatta']
    pr_nums = ['189']
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue27660'
    versions = ['Python 3.7']

    @zhangyangyu
    Copy link
    Member Author

    In list_resize, new_allocated is of type size_t but I think don't have to be now since it finally have to assign back to self->allocated which is of type Py_ssize_t. With Py_ssize_t, we can check some overflows in the first overflow check and don't need the second overflow check.

    @zhangyangyu zhangyangyu added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Jul 31, 2016
    @bitdancer
    Copy link
    Member

    ob->allocated is temporarily set to -1 by list.sort to detect mutations, so the restriction of new_allocated to size_t is probably intentional. If so, though, there ought to be a comment making that clear.

    @bitdancer
    Copy link
    Member

    Oh, I got that backward didn't I. My rusty C skills :(

    @zhangyangyu
    Copy link
    Member Author

    :( Sorry David, my poor English doesn't enable me understand your message totally. Is list.sort related to this problem? Do I miss something?

    @bitdancer
    Copy link
    Member

    No, I was the one who missed something. Just ignore my comment and lets wait for someone with more current C chops to answer :)

    @bitdancer
    Copy link
    Member

    Erg. You mention your English proficiency and then I go using a colequialism :( "better C chops" means someone with more skill than I am currently exhibiting.

    @vadmium
    Copy link
    Member

    vadmium commented Aug 2, 2016

    The patch looks okay to me.

    @rhettinger rhettinger self-assigned this Aug 2, 2016
    @serhiy-storchaka
    Copy link
    Member

    Unsigned type can be used for more efficient checking on integer overflow.

        new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
        new_allocated += (size_t)newsize;
        if (new_allocated < (size_t)newsize) {
            PyErr_NoMemory();
            return -1;
        }

    Checking "new_allocated < (size_t)newsize" can be more efficient than "new_allocated > PY_SSIZE_T_MAX - newsize".

    @zhangyangyu
    Copy link
    Member Author

    But if you use size_t, you are checking overflow against PY_SIZE_MAX, not PY_SSIZE_T_MAX.

    @serhiy-storchaka
    Copy link
    Member

    True. Then we should use the condition "new_allocated > PY_SSIZE_T_MAX". This can be even more efficient.

    @zhangyangyu
    Copy link
    Member Author

    So how about list_resize_v2.patch?

    @zhangyangyu
    Copy link
    Member Author

    Ping.

    @rhettinger
    Copy link
    Contributor

    This is low priority. I will look at it during the sprint. As far as I know there is no bug here or performance problem; instead, the patch alters stable code and transfers responsibilities.

    @vstinner
    Copy link
    Member

    I reviewed list_resize_v2.patch.

    @zhangyangyu
    Copy link
    Member Author

    v3 applies haypo's suggestion.

    @zhangyangyu
    Copy link
    Member Author

    I opened a PR on GitHub for this issue. Hope Raymond you could review it some time.

    @zhangyangyu zhangyangyu added the 3.7 (EOL) end of life label Feb 20, 2017
    @rhettinger
    Copy link
    Contributor

    It was a little mind-numbing to check this code, but it looks correct.

    Please fix two nits for better readability. For review purposes, it was nice to have changes stand-out, but for the final code I would like to combine the two comments and the two new_allocated assignments into a single comment and single assignment:

      /* The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
         Note: new_allocated won't overflow because the largest possible value
               is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
      */
     new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
    

    Secondly, please rename the "size" variable to something like "num_allocated_bytes". It is confusing to have "newsize" mean the number of elements actually used while having "size" mean the number of bytes actually allocated (including the overallocation).

    @zhangyangyu
    Copy link
    Member Author

    Thanks for your time Raymond. :-) I applied your suggestions in the PR.

    @rhettinger
    Copy link
    Contributor

    Mariatta, would you like to run this against the test suite and then apply it. I don't think a MISC/NEWS entry is needed (it is just a code simplification) and Xiang is already in MISC/ACKS.

    @rhettinger rhettinger assigned Mariatta and unassigned rhettinger Feb 21, 2017
    @zhangyangyu
    Copy link
    Member Author

    Sorry, I have the commit bit and know what to do with a commit. So I assign it to myself and wait someone approve the PR on GitHub.

    @zhangyangyu zhangyangyu assigned zhangyangyu and unassigned Mariatta Feb 21, 2017
    @rhettinger
    Copy link
    Contributor

    Okay, go ahead. I was hoping to give Mariatta some practice applying C patches (she's new).

    @zhangyangyu
    Copy link
    Member Author

    New changeset 4cee049 by GitHub in branch 'master':
    bpo-27660: remove unnecessary overflow checks in list_resize (GH-189)
    4cee049

    @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

    7 participants