[New-bugs-announce] [issue32949] Simplify "with"-related opcodes
Serhiy Storchaka
report at bugs.python.org
Sun Feb 25 08:43:14 EST 2018
New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:
There are some issues with "with"-related opcodes.
All other opcodes has constant stack effect for particular control flow. For example FOR_ITER always has the stack effect 1 if not jump (pushes the next item) and -1 if jumps (pops the iterator). The only exceptions are WITH_CLEANUP_START which pushes 1 or 2 values depending on TOS, and WITH_CLEANUP_FINISH which pops 2 or 3 values depending on values pushed by preceding WITH_CLEANUP_START. This breaks consistency and may make debugging harder.
WITH_CLEANUP_START duplicates a one of values on the stack without good reasons. Even the comment in the initial commit exposed uncertainty in this.
The proposed PR simplifies WITH_CLEANUP_START and WITH_CLEANUP_FINISH. They will be now executed only when the exception is raised. In normal case they will be replaced with calling a function `__exit__(None, None, None)`.
LOAD_CONST 0 ((None, None, None))
CALL_FUNCTION_EX 0
POP_TOP
WITH_CLEANUP_FINISH will be merged with the following END_FINALLY.
This PR is inspired by PR 5112 by Mark Shannon, but Mark goes further.
In addition to simplifying the implementation and the mental model, the PR adds a tiny bit of performance gain.
$ ./python -m perf timeit -s 'class CM:' -s ' def __enter__(s): pass' -s ' def __exit__(*args): pass' -s 'cm = CM()' -- 'with cm: pass'
Unpatched: Mean +- std dev: 227 ns +- 6 ns
Patched: Mean +- std dev: 205 ns +- 10 ns
----------
components: Interpreter Core
messages: 312813
nosy: Mark.Shannon, benjamin.peterson, pitrou, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Simplify "with"-related opcodes
type: performance
versions: Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32949>
_______________________________________
More information about the New-bugs-announce
mailing list