Mark Shannon <m...@hotpy.org> added the comment:

PR 29581 resulted in a 1% slowdown, which is not terrible, but code not using 
except* should not be slowed down at all.

IMO, the way to avoid the slowdown is to implement except* using the existing 
instruction set (perhaps with a few minor additions)

We already implement try-finally, named except blocks and with statements 
without any complex bytecodes (except perhaps WITH_EXCEPT_START).

These used to involve a lot of state and more complex bytecodes. So it is 
possible to make these simplifications,
but it does take work.


There are a number of techniques we can use:

If any state is needed, push it to the stack as we do with `ctx.__exit__` in 
the with statement, and when pushing f_lasti in exception handlers.
Duplicate code paths when the semantics differ in different cases, as we do for 
finally blocks.
If anything is too complex to handle on the stack, put it in a temporary 
variable.
Be liberal in your use of virtual try-excepts (SETUP_FINALLY, POP_FINALLY 
pairs), as zero-cost exception handling should keep the cost down.


It may be too late for this advice, but if I were writing the `except*` 
implementation from scratch, I would:

1. Sketch out the pseudo Python that a try-except* would map to. This is a good 
opportunity to discover any design bugs that might result in undesirable 
behavior for corner cases.

2. Implement the translation in the compiler, not worrying about any redundancy 
or inefficiency, just correctness.

3. Look to improve the above, either in the compiler front-end, or by replacing 
inefficient code patterns in the back-end. Handling the optimization in the 
backend has the advantage that other code might benefit as well.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45292>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to