[New-bugs-announce] [issue24582] Minor branch optimization in set implementation

Serhiy Storchaka report at bugs.python.org
Tue Jul 7 10:38:26 CEST 2015


New submission from Serhiy Storchaka:

For now multiple set functions call helpers that returns one of three possible values (0, 1, and -1 for error) and then analyze return code in the loop.

    while (...) {
        rv = some_set_operation();
        if (rv < 0) {
            // clean up
            return NULL;
        }
        if (rv) {
            other_set_operation();
        }
        // if (rv == 0) do nothing
    }

If rv is 0 or 1, it is tested two times. If rv is -1 (unlikely), it is tested only once.

It is possible to rewrite testing in other way:

    while (...) {
        rv = some_set_operation();
        if (rv != 0) { // 1 or -1
            if (rv < 0) {
                // clean up
                return NULL;
            }
            other_set_operation();
        }
    }

Now if rv is 0 (common case), it is tested only once, and likely the test jump will be merged with unconditional loop jump.

I have no benchmarking results, but I suppose that such rewritting will generate better machine code.

----------
components: Interpreter Core
files: set_branch_optimizations.patch
keywords: patch
messages: 246395
nosy: rhettinger, serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: Minor branch optimization in set implementation
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file39878/set_branch_optimizations.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24582>
_______________________________________


More information about the New-bugs-announce mailing list