New submission from Serhiy Storchaka <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue32949>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com