classification
Title: Optimize asyncio.ensure_future by reordering if conditions
Type: performance Stage: resolved
Components: asyncio Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, jimmylai, yselivanov
Priority: normal Keywords: patch

Created on 2018-05-14 20:45 by jimmylai, last changed 2018-05-28 19:14 by yselivanov. This issue is now closed.

Files
File name Uploaded Description Edit
ensure_future_benchmark.py jimmylai, 2018-05-15 13:41 ./python.exe ensure_furture_benchmark.py -o ensure_future_optimized.json
Pull Requests
URL Status Linked Edit
PR 6832 closed jimmylai, 2018-05-14 21:18
PR 6836 merged jimmylai, 2018-05-14 22:07
PR 7162 merged miss-islington, 2018-05-28 16:43
Messages (4)
msg316572 - (view) Author: Jimmy Lai (jimmylai) * Date: 2018-05-14 20:45
`ensure_future` converts the input as future if it's not already a future.
The condition is the following:

if futures.isfuture(coro_or_future):
    ...
elif coroutines.iscoroutine(coro_or_future):
    ...
elif inspect.isawaitable(coro_or_future):
    ...

In real world, `ensure_future` is mostly called by `run_until_complete` and gather with async function call (coroutine) as input.
We should check `iscoroutine` first to make it most efficient.
msg316656 - (view) Author: Jimmy Lai (jimmylai) * Date: 2018-05-15 13:41
Benchmark result:
./python.exe -m perf compare_to ensure_future_original.json ensure_future_optimized.json
Mean +- std dev: [ensure_future_original] 57.4 ms +- 4.0 ms -> [ensure_future_optimized] 49.3 ms +- 4.5 ms: 1.17x faster (-14%)
msg317887 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-28 16:42
New changeset e549c4be5fb010f5faf12236af8faa720a1429be by Yury Selivanov (jimmylai) in branch 'master':
bpo-33505: Optimize asyncio.ensure_future by reordering if conditions (GH-6836)
https://github.com/python/cpython/commit/e549c4be5fb010f5faf12236af8faa720a1429be
msg317899 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-28 18:42
New changeset f8fdb368e3d8d048bffc248fbe1023651c276671 by Yury Selivanov (Miss Islington (bot)) in branch '3.7':
bpo-33505: Optimize asyncio.ensure_future by reordering if conditions (GH-6836) (#7162)
https://github.com/python/cpython/commit/f8fdb368e3d8d048bffc248fbe1023651c276671
History
Date User Action Args
2018-05-28 19:14:26yselivanovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-05-28 18:42:53yselivanovsetmessages: + msg317899
2018-05-28 16:43:33miss-islingtonsetpull_requests: + pull_request6797
2018-05-28 16:42:08yselivanovsetmessages: + msg317887
2018-05-15 13:41:57jimmylaisetfiles: + ensure_future_benchmark.py

messages: + msg316656
2018-05-14 22:07:48jimmylaisetpull_requests: + pull_request6518
2018-05-14 21:18:38jimmylaisetkeywords: + patch
stage: patch review
pull_requests: + pull_request6515
2018-05-14 20:45:32jimmylaicreate