The implementation of contextlib.contextmanager currently advances the wrapped generator using explicit next calls.
Replacing these with equivalent for loops allows the interpreter to take advantage of existing iterator-loop optimizations:
- Generator iteration in
for loops can be specialized more effectively than direct next calls.
- The interpreter can avoid constructing and raising
StopIteration exceptions in the common case where loop termination is expected.
- This is also more JIT-friendly, since iteration is expressed using a common language construct rather than explicit exception-driven control flow.
Local microbenchmarks of entering and exiting a context manager repeatedly show measurable reductions in the overhead introduced by _GeneratorContextManager.
This issue tracks updating the implementation to use for loops where appropriate.
Linked PRs
The implementation of
contextlib.contextmanagercurrently advances the wrapped generator using explicitnextcalls.Replacing these with equivalent
forloops allows the interpreter to take advantage of existing iterator-loop optimizations:forloops can be specialized more effectively than directnextcalls.StopIterationexceptions in the common case where loop termination is expected.Local microbenchmarks of entering and exiting a context manager repeatedly show measurable reductions in the overhead introduced by
_GeneratorContextManager.This issue tracks updating the implementation to use
forloops where appropriate.Linked PRs
forinstead ofnextincontextlib.contextmanager#141275