[New-bugs-announce] [issue30501] Produce optimized code for boolean conditions

Serhiy Storchaka report at bugs.python.org
Mon May 29 02:58:36 EDT 2017

New submission from Serhiy Storchaka:

The peephole optimizer optimizes some boolean conditions. For example in "if not a:" it replaces UNARY_NOT+POP_JUMP_IF_FALSE with POP_JUMP_IF_TRUE, and in "if a and b:" it makes checking the boolean value of a only once. But it is unable to optimize more complex conditions, like "if not a and b:".

Proposed patch makes the compiler producing optimized code for conditions. It supports expressions with arbitrary complexity containing the "not" operator, the "and" and "or" binary operators, the "if" ternary operator, and chained comparisons, used as conditions in the ternary operator, in the "if", "while" and "assert" statements, and in generator expressions and comprehensions.

It would be possible to remove the part of the peepholer optimizer, but it is needed also for optimizing the "and" and "or" operators in non-boolean context. Doing this optimization in the compiler is possible but too cumbersome, it requires 3 times more code that in the proposed patch. May be I'll find the more general solution in future.

components: Interpreter Core
messages: 294674
nosy: benjamin.peterson, brett.cannon, ncoghlan, rhettinger, serhiy.storchaka, yselivanov
priority: normal
severity: normal
stage: patch review
status: open
title: Produce optimized code for boolean conditions
type: performance
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list