[Python-Dev] Rewrite @contextlib.contextmanager in C
Wolfgang Maier
wolfgang.maier at biologie.uni-freiburg.de
Mon Aug 8 17:14:59 EDT 2016
On 8/8/2016 22:38, Yury Selivanov wrote:
>
>
> On 2016-08-08 4:18 PM, Guido van Rossum wrote:
>> I think Nick would be interested in understanding why this is the
>> case. What does the decorator do that could be so expensive?
>
> From the looks of it it doesn't do anything special. Although with
> @contextlib.contextmanager we have to instantiate a generator (the
> decorated one) and advance it in __enter__. So it's an extra object
> instantiation + extra code in __enter__ and __exit__. Anyways, Nick
> knows much more about that code.
>
Right, I think a fairer comparison would be to:
class ctx2:
def __enter__(self):
self.it = iter(self)
return next(self.it)
def __exit__(self, *args):
try:
next(self.it)
except StopIteration:
pass
def __iter__(self):
yield
With this change alone the slowdown diminishes to ~ 1.7x for me. The
rest is probably the extra overhead for being able to pass exceptions
raised inside the with block back into the generator and such.
More information about the Python-Dev
mailing list