[New-bugs-announce] [issue11462] Peephole creates duplicate and unused constants

Eugene Toder report at bugs.python.org
Fri Mar 11 01:55:59 CET 2011

New submission from Eugene Toder <eltoder at gmail.com>:

Peephole optimizer performs constant folding, however
1) When it replaces operation with LOAD_CONST it always adds a new constant to co_consts, even if constant with the same value is already there. It also can add the same constant multiple times.
2) It does not remove constants that are no longer used after the operation was folded.
The result is that code object after folding has more constants that it needs and so uses more memory.

Attached are patches to address this. Patch for 1) comes in 2 versions. PlanA is simple (it only needs changes in peephole.c), but does linear searches through co_consts and duplicates some logic from compiler.c. PlanB needs changes in both peephole.c and compiler.c, but is free from such problems. I favour PlanB.

Patch for 2) can be applied on top of either A or B.

components: Interpreter Core
messages: 130537
nosy: eltoder
priority: normal
severity: normal
status: open
title: Peephole creates duplicate and unused constants
type: performance
versions: Python 3.3

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list