[New-bugs-announce] [issue32525] Empty tuples are not optimized as constant expressions

benrg report at bugs.python.org
Tue Jan 9 16:49:13 EST 2018

New submission from benrg <benrudiak at gmail.com>:

>From 3.3 on, the expression () is compiled to BUILD_TUPLE 0 instead of LOAD_CONST. That's probably fine and I suppose it's slightly more efficient to avoid adding an entry to the constant table.

The problem is that BUILD_TUPLE 0 is not treated as a constant for folding purposes, so any otherwise constant expression that contain () ends up compiling into O(n) bytecode instructions instead of 1. I think this is a bug (rather than an enhancement) because it seems unlikely to be the intended behavior.

In 3.2 an earlier, and in 2.7, the constant-folding behavior is different, and many constant tuples aren't recognized at compile time for reasons unclear to me, but there are definitely cases it will fold that 3.3+ won't. For example, "x in {(), None}" tests a frozenset in 3.2, but builds a set at run time in 3.3+.

components: Interpreter Core
messages: 309739
nosy: benrg
priority: normal
severity: normal
status: open
title: Empty tuples are not optimized as constant expressions
type: performance
versions: Python 3.4, Python 3.5, Python 3.6, Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list