Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-103793: Defer formatting task name #103767

Merged
merged 14 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ Optimizations
replacement strings containing group references by 2--3 times.
(Contributed by Serhiy Storchaka in :gh:`91524`.)

* Speed up :class:`asyncio.Task` creation by deferring expensive string formatting.
(Contributed by Itamar O in :gh:`103793`.)


CPython bytecode changes
========================
Expand Down
5 changes: 4 additions & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
self._name = f'Task-{_task_name_counter()}'
# optimization: defer task name formatting to first get_name
self._name = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to optimize the .py version (which is normally not run since there is a C accelerator version)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we definitely don't need to optimize the python version. I assumed we wanted to maintain parity between the versions, but maybe it doesn't matter to this extent (or my assumption was wrong and parity is not a goal).

I will revert the changes to the python version.

else:
self._name = str(name)

Expand Down Expand Up @@ -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):
Expand Down
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.
11 changes: 9 additions & 2 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2069,8 +2069,9 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
Py_XSETREF(self->task_coro, coro);

if (name == Py_None) {
name = PyUnicode_FromFormat("Task-%" PRIu64,
++state->task_name_counter);
// 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 {
Expand Down Expand Up @@ -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.
Show resolved Hide resolved
Py_XSETREF(self->task_name, name);
}
return Py_NewRef(self->task_name);
}

Expand Down