Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gvanrossum merged 14 commits intopython:mainfromitamaro:defer-task-name-formatting
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
14 commits
Select commitHold shift + click to select a range
e8353cc
Defer formatting task name
itamaroApr 24, 2023
1822ff8
Defer task name formatting also in Python Task impl
itamaroApr 24, 2023
74d0084
Add NEWS and whatsnew entries
itamaroApr 24, 2023
dbc7047
Merge branch 'main' into defer-task-name-formatting
ambvApr 25, 2023
2bb6c1e
Merge branch 'main' into defer-task-name-formatting
itamaroApr 25, 2023
2efd376
replace storing the task counter on task construction with just getti…
itamaroApr 25, 2023
6cb321b
Merge branch 'defer-task-name-formatting' of github.com:itamaro/cpyth…
itamaroApr 25, 2023
7cde290
remove redundant return
itamaroApr 25, 2023
4b50646
Merge branch 'main' into defer-task-name-formatting
itamaroApr 25, 2023
a155190
revert changes to python version
itamaroApr 25, 2023
fb2bd7f
pass error up if PyUnicode_FromFormat returns NULL
itamaroApr 25, 2023
41e9a59
Switch back to original "assign task number at construction" but stor…
itamaroApr 26, 2023
712edf9
Add test that using an explicit PyLong as task name does not accident…
itamaroApr 26, 2023
592d44b
Merge branch 'main' into defer-task-name-formatting
itamaroApr 26, 2023
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 deletionsDoc/whatsnew/3.12.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -565,6 +565,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
12 changes: 12 additions & 0 deletionsLib/test/test_asyncio/test_tasks.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -399,6 +399,18 @@ async def notmuch():
self.loop.run_until_complete(t1)
self.loop.run_until_complete(t2)

def test_task_set_name_pylong(self):
# test that setting the task name to a PyLong explicitly doesn't
# incorrectly trigger the deferred name formatting logic
async def notmuch():
return 123

t = self.new_task(self.loop, notmuch(), name=987654321)
self.assertEqual(t.get_name(), '987654321')
t.set_name(123456789)
self.assertEqual(t.get_name(), '123456789')
self.loop.run_until_complete(t)

def test_task_repr_name_not_str(self):
async def notmuch():
return 123
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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.
13 changes: 11 additions & 2 deletionsModules/_asynciomodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2069,8 +2069,10 @@ _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
// store the task counter as PyLong in the name
// for deferred formatting in get_name
name = PyLong_FromUnsignedLongLong(++state->task_name_counter);
} else if (!PyUnicode_CheckExact(name)) {
name = PyObject_Str(name);
} else {
Expand DownExpand Up@@ -2449,6 +2451,13 @@ _asyncio_Task_get_name_impl(TaskObj *self)
/*[clinic end generated code: output=0ecf1570c3b37a8f input=a4a6595d12f4f0f8]*/
{
if (self->task_name) {
if (PyLong_CheckExact(self->task_name)) {
PyObject *name = PyUnicode_FromFormat("Task-%S", self->task_name);
if (name == NULL) {
return NULL;
}
Py_SETREF(self->task_name, name);
}
return Py_NewRef(self->task_name);
}

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp