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

Remove dummy reuse optimization from sets #67448

Closed
rhettinger opened this issue Jan 17, 2015 · 8 comments
Closed

Remove dummy reuse optimization from sets #67448

rhettinger opened this issue Jan 17, 2015 · 8 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@rhettinger
Copy link
Contributor

BPO 23259
Nosy @tim-one, @rhettinger, @pitrou, @serhiy-storchaka
Files
  • late8.diff: Remove freeslot tracking
  • 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/rhettinger'
    closed_at = <Date 2015-01-18.20:58:59.238>
    created_at = <Date 2015-01-17.20:46:06.566>
    labels = ['interpreter-core', 'performance']
    title = 'Remove dummy reuse optimization from sets'
    updated_at = <Date 2015-01-18.20:58:59.237>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2015-01-18.20:58:59.237>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2015-01-18.20:58:59.238>
    closer = 'rhettinger'
    components = ['Interpreter Core']
    creation = <Date 2015-01-17.20:46:06.566>
    creator = 'rhettinger'
    dependencies = []
    files = ['37750']
    hgrepos = []
    issue_num = 23259
    keywords = ['patch']
    message_count = 8.0
    messages = ['234198', '234200', '234205', '234207', '234211', '234226', '234227', '234278']
    nosy_count = 4.0
    nosy_names = ['tim.peters', 'rhettinger', 'pitrou', 'serhiy.storchaka']
    pr_nums = []
    priority = 'low'
    resolution = 'rejected'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue23259'
    versions = ['Python 3.5']

    @rhettinger
    Copy link
    Contributor Author

    The lookkey routines in Object/setobject.c have logic to track the first open "freeslot" in a search.

    The benefit is that new keys can reuse previously deleted slots. The benefit only occurs in cases where keys are added, then some removed, and then more added with no intervening resizes (which clear all dummy entries) and only when the newly added keys happen to land on previously deleted entries.

    The cost of the optimization is the extra logic in the inner search loop, an extra freeslot stackframe field that needs to be saved and restored, and extra logic on the loop exit. It also causes dummies to "leak" out of the lookkey routines so that the code in contains(), discard(), and insert() all have to check for the dummy case.

    This patch removes the freeslot tracking. It saves one field on the stackframe, simplifies the lookkey inner-loop logic, simplifies the lookkey "found_null" logic, it confines knowledge of dummies to just the lookkey functions, and it simplifies the code in the insert(), contains(), and discard() functions.

    In short, it looks like the freeslot idea was a net negative -- it optimized an uncommon case at the cost of slowing and complicating the common cases.

    @rhettinger rhettinger self-assigned this Jan 17, 2015
    @rhettinger rhettinger added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Jan 17, 2015
    @serhiy-storchaka
    Copy link
    Member

    This can be a common case in following algorithm (mesh optimization):

    while elems:
        elem = elems.pop()
        changed = optimize(elem)
        if changed:
            elems.update(neighbors(elem))

    @rhettinger
    Copy link
    Contributor Author

    Even in the mesh algorithm, we let resizing periodically clean-up the dummies. The idea is to not pay the freeslot tracking cost on every lookup and instead only clean-up periodically (which would likely give better performance for the mesh algorithm as well, since making a single pass clean-up during resizing is cheaper than doing multi-step tracking for every insertion). The slots do get reused, just not immediately.

    Also, the idea is to not let the possibility of pop-change-update algorithms create a cost for the more common uses of sets (uniquification, fast membership testing, and set-to-set operations such as union, intersection, and difference).

    @pitrou
    Copy link
    Member

    pitrou commented Jan 18, 2015

    In short, it looks like the freeslot idea was a net negative -- it
    optimized an uncommon case at the cost of slowing and complicating the
    common cases.

    Do you have a benchmark showing the slowing down?

    @pitrou
    Copy link
    Member

    pitrou commented Jan 18, 2015

    Fewer instructions doesn't necessarily translate into better performance. The bottleneck in superscalar, pipelined processors is often the data dependency path between instructions. Adding instructions may as well not slow anything down, if those instructions don't lengthen the dependency path.

    It would be interesting to know what the impact of the patch is on the workloads that were supposed to be helped by the removed logic.

    @serhiy-storchaka
    Copy link
    Member

    In some cases the slowdown is over 100x.

    $ ./python -m timeit -s "s = set(range(10**4))" -- "s.discard(0); s.add(0)"

    Unpatched: 1000000 loops, best of 3: 1.68 usec per loop
    Patched: 10000 loops, best of 3: 183 usec per loop

    @serhiy-storchaka
    Copy link
    Member

    If you want to decrease the number of executed instructions, I suggest to duplicate lookup functions (actually add boolean flag and switch one of two branches). On read lookup function should ignore dummy entries, on modification it should behave as now.

    @rhettinger
    Copy link
    Contributor Author

    I'll just close this one. There seems to be little interest in it.

    Originally, I characterized freeslot tracking as an optimization that mildly benefited an uncommon case (adds/deletes/moreadds) at the expense of the common cases (uniquification, membership testing, and set-to-set operations).

    As Antoine pointed-out, on some systems the cost of a predictable branch routinely not take can be almost zero. There was still benefit in code simplification, reducing the size of the stack frame, freeing a register that needed to saved and restored when called PyRich_CompareBool(), etc. But no one seems to care about those.

    As Serhiy found, there is one case where the freeslot tracking benefit wasn't "mild". In the unusual but possible case of deleting and reinserting the exact same value in a large set, dummy reuse prevents a catastrophic linear pile-up.

    And so it goes. Free slot tracking lives.

    @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) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants