New submission from Vladimir Matveev <desco...@gmail.com>:

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

----------
components: Interpreter Core
messages: 378999
nosy: v2m
priority: normal
severity: normal
status: open
title: Add dedicated slot for sending values
versions: Python 3.10

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

Reply via email to