Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-103793: Defer formatting task name#103767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from8 commits
e8353cc1822ff874d0084dbc70472bb6c1e2efd3766cb321b7cde2904b50646a155190fb2bd7f41e9a59712edf9592d44bFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -104,7 +104,8 @@ def __init__(self, coro, *, loop=None, name=None, context=None): | ||
| raise TypeError(f"a coroutine was expected, got {coro!r}") | ||
| if name is None: | ||
| # optimization: defer task name formatting to first get_name | ||
| self._name = None | ||
| ||
| else: | ||
| self._name = str(name) | ||
| @@ -143,6 +144,8 @@ def get_context(self): | ||
| return self._context | ||
| def get_name(self): | ||
| if self._name is None: | ||
| self._name = f'Task-{_task_name_counter()}' | ||
| return self._name | ||
| def set_name(self, value): | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Optimized asyncio Task creation by deferring expensive string formatting | ||
| (task name generation) from Task creation to the first time ``get_name`` is | ||
| called. This makes asyncio benchmarks up to 5% faster. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2069,8 +2069,9 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop, | ||
| Py_XSETREF(self->task_coro, coro); | ||
| if (name == Py_None) { | ||
| // optimization: defer task name formatting | ||
| // set task_name to None to indicate deferred formatting | ||
| Py_INCREF(name); | ||
| } else if (!PyUnicode_CheckExact(name)) { | ||
| name = PyObject_Str(name); | ||
| } else { | ||
| @@ -2449,6 +2450,12 @@ _asyncio_Task_get_name_impl(TaskObj *self) | ||
| /*[clinic end generated code: output=0ecf1570c3b37a8f input=a4a6595d12f4f0f8]*/ | ||
| { | ||
| if (self->task_name) { | ||
| if (Py_IsNone(self->task_name)) { | ||
| asyncio_state *state = get_asyncio_state_by_def((PyObject *)self); | ||
| PyObject *name = PyUnicode_FromFormat( | ||
| "Task-%" PRIu64, ++state->task_name_counter); | ||
itamaro marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Py_XSETREF(self->task_name, name); | ||
| } | ||
| return Py_NewRef(self->task_name); | ||
| } | ||