Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
replace storing the task counter on task construction with just getti…
…ng the next counter at lazy name generation
  • Loading branch information
itamaro committed Apr 25, 2023
commit 2efd376a739946c5fd9d2902f04634f21763c9a7
5 changes: 2 additions & 3 deletions 5 Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ def __init__(self, coro, *, loop=None, name=None, context=None):
self._log_destroy_pending = False
raise TypeError(f"a coroutine was expected, got {coro!r}")

self._task_name_counter = 0
if name is None:
self._task_name_counter = _task_name_counter()
# optimization: defer task name formatting to first get_name
self._name = None
else:
self._name = str(name)
Expand Down Expand Up @@ -146,7 +145,7 @@ def get_context(self):

def get_name(self):
if self._name is None:
self._name = f'Task-{self._task_name_counter}'
self._name = f'Task-{_task_name_counter()}'
return self._name

def set_name(self, value):
Expand Down
10 changes: 3 additions & 7 deletions 10 Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ typedef struct {
int task_must_cancel;
int task_log_destroy_pending;
int task_num_cancels_requested;
uint64_t task_name_counter;
} TaskObj;

typedef struct {
Expand Down Expand Up @@ -2066,16 +2065,13 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
self->task_must_cancel = 0;
self->task_log_destroy_pending = 1;
self->task_num_cancels_requested = 0;
self->task_name_counter = 0;
Py_INCREF(coro);
Py_XSETREF(self->task_coro, coro);

if (name == Py_None) {
// optimization: defer task name formatting
// set task_name to None to indicate deferred formatting, and
// store the task name counter for formatting in get_name impl
// set task_name to None to indicate deferred formatting
Py_INCREF(name);
self->task_name_counter = ++state->task_name_counter;
} else if (!PyUnicode_CheckExact(name)) {
name = PyObject_Str(name);
} else {
Expand Down Expand Up @@ -2455,9 +2451,9 @@ _asyncio_Task_get_name_impl(TaskObj *self)
{
if (self->task_name) {
if (Py_IsNone(self->task_name)) {
assert(self->task_name_counter > 0);
asyncio_state *state = get_asyncio_state_by_def((PyObject *)self);
PyObject *name = PyUnicode_FromFormat(
"Task-%" PRIu64, self->task_name_counter);
"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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.