Author v2m
Recipients v2m
Date 2020-10-19.19:38:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1603136296.98.0.312180912341.issue42085@roundup.psfhosted.org>
In-reply-to
Content
https://bugs.python.org/issue41756 has introduced PyIter_Send as a common entrypoint for sending values however currently fast path that does not use StopIteration exception is only available for generators/coroutines. It would be quite nice if this machinery was extensible and other types (both internal and 3rd party) could opt-in into using exception-free way of returning values without needing to update the implementation of PyIter_Send. One way of solving this is adding a new slot with a signature that matches PyIter_Send. With it:
- it should be possible to implement this slot for coroutines/generators and remove  special casing for them  in PyIter_Send
- provide implementation for this slot for internal types (i.e. FutureIter in _asynciomodule.c) - results of this experiment can be found below
- enable external native extensions to provide efficient implementation of coroutines (i.e.  Cython could benefit from it)

Microbenchmark to demonstrate the difference of accessing the value of fulfiled Future without and with dedicated slot:
```
import asyncio
import time

N = 100000000

async def run():
    fut = asyncio.Future()
    fut.set_result(42)

    t0 = time.time()
    for _ in range(N):
        await fut
    t1 = time.time()
    print(f"Time: {t1 - t0} s")

asyncio.run(run())
```
Time: 8.365560054779053 s - without the slot
Time: 5.799655914306641 s - with the  slot
History
Date User Action Args
2020-10-19 19:38:17v2msetrecipients: + v2m
2020-10-19 19:38:16v2msetmessageid: <1603136296.98.0.312180912341.issue42085@roundup.psfhosted.org>
2020-10-19 19:38:16v2mlinkissue42085 messages
2020-10-19 19:38:16v2mcreate