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 <rep...@bugs.python.org>
<http://bugs.python.org/issue24582>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to