Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e5724e

Browse files
authored
Merge branch 'main' into platform_re_compile
2 parents 470ddc4 + 85c7bf5 commit 4e5724e

9 files changed

Lines changed: 121 additions & 82 deletions

File tree

Doc/whatsnew/3.12.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,9 @@ Optimizations
610610
replacement strings containing group references by 2--3 times.
611611
(Contributed by Serhiy Storchaka in :gh:`91524`.)
612612

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

614617
CPython bytecode changes
615618
========================

Include/internal/pycore_compile.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
1919
int optimize,
2020
struct _arena *arena);
2121

22+
static const _PyCompilerSrcLocation NO_LOCATION = {-1, -1, -1, -1};
2223

2324
typedef struct {
2425
int optimize;
@@ -33,15 +34,21 @@ extern int _PyAST_Optimize(
3334
struct _arena *arena,
3435
_PyASTOptimizeState *state);
3536

37+
typedef struct {
38+
int h_offset;
39+
int h_startdepth;
40+
int h_preserve_lasti;
41+
} _PyCompile_ExceptHandlerInfo;
3642

3743
typedef struct {
3844
int i_opcode;
3945
int i_oparg;
4046
_PyCompilerSrcLocation i_loc;
41-
} _PyCompilerInstruction;
47+
_PyCompile_ExceptHandlerInfo i_except_handler_info;
48+
} _PyCompile_Instruction;
4249

4350
typedef struct {
44-
_PyCompilerInstruction *s_instrs;
51+
_PyCompile_Instruction *s_instrs;
4552
int s_allocated;
4653
int s_used;
4754

@@ -82,6 +89,8 @@ int _PyCompile_EnsureArrayLargeEnough(
8289

8390
int _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj);
8491

92+
int _PyCompile_InstrSize(int opcode, int oparg);
93+
8594
/* Access compiler internals for unit testing */
8695

8796
PyAPI_FUNC(PyObject*) _PyCompile_CodeGen(

Include/internal/pycore_flowgraph.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ extern "C" {
1111
#include "pycore_opcode_utils.h"
1212
#include "pycore_compile.h"
1313

14-
static const _PyCompilerSrcLocation NO_LOCATION = {-1, -1, -1, -1};
1514

1615
typedef struct {
1716
int i_opcode;
@@ -97,7 +96,6 @@ int _PyCfg_OptimizeCodeUnit(_PyCfgBuilder *g, PyObject *consts, PyObject *const_
9796
int _PyCfg_Stackdepth(_PyCfgBasicblock *entryblock, int code_flags);
9897
void _PyCfg_ConvertExceptionHandlersToNops(_PyCfgBasicblock *entryblock);
9998
int _PyCfg_ResolveJumps(_PyCfgBuilder *g);
100-
int _PyCfg_InstrSize(_PyCfgInstruction *instruction);
10199

102100

103101
static inline int
@@ -113,7 +111,7 @@ basicblock_nofallthrough(const _PyCfgBasicblock *b) {
113111

114112
PyCodeObject *
115113
_PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *u, PyObject *const_cache,
116-
PyObject *consts, int maxdepth, _PyCfgBasicblock *entryblock,
114+
PyObject *consts, int maxdepth, _PyCompile_InstructionSequence *instrs,
117115
int nlocalsplus, int code_flags, PyObject *filename);
118116

119117
#ifdef __cplusplus

Lib/test/test_asyncio/test_tasks.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,18 @@ async def notmuch():
399399
self.loop.run_until_complete(t1)
400400
self.loop.run_until_complete(t2)
401401

402+
def test_task_set_name_pylong(self):
403+
# test that setting the task name to a PyLong explicitly doesn't
404+
# incorrectly trigger the deferred name formatting logic
405+
async def notmuch():
406+
return 123
407+
408+
t = self.new_task(self.loop, notmuch(), name=987654321)
409+
self.assertEqual(t.get_name(), '987654321')
410+
t.set_name(123456789)
411+
self.assertEqual(t.get_name(), '123456789')
412+
self.loop.run_until_complete(t)
413+
402414
def test_task_repr_name_not_str(self):
403415
async def notmuch():
404416
return 123
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Optimized asyncio Task creation by deferring expensive string formatting
2+
(task name generation) from Task creation to the first time ``get_name`` is
3+
called. This makes asyncio benchmarks up to 5% faster.

Modules/_asynciomodule.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,8 +2069,10 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
20692069
Py_XSETREF(self->task_coro, coro);
20702070

20712071
if (name == Py_None) {
2072-
name = PyUnicode_FromFormat("Task-%" PRIu64,
2073-
++state->task_name_counter);
2072+
// optimization: defer task name formatting
2073+
// store the task counter as PyLong in the name
2074+
// for deferred formatting in get_name
2075+
name = PyLong_FromUnsignedLongLong(++state->task_name_counter);
20742076
} else if (!PyUnicode_CheckExact(name)) {
20752077
name = PyObject_Str(name);
20762078
} else {
@@ -2449,6 +2451,13 @@ _asyncio_Task_get_name_impl(TaskObj *self)
24492451
/*[clinic end generated code: output=0ecf1570c3b37a8f input=a4a6595d12f4f0f8]*/
24502452
{
24512453
if (self->task_name) {
2454+
if (PyLong_CheckExact(self->task_name)) {
2455+
PyObject *name = PyUnicode_FromFormat("Task-%S", self->task_name);
2456+
if (name == NULL) {
2457+
return NULL;
2458+
}
2459+
Py_SETREF(self->task_name, name);
2460+
}
24522461
return Py_NewRef(self->task_name);
24532462
}
24542463

Python/assemble.c

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include <stdbool.h>
22

33
#include "Python.h"
4-
#include "pycore_flowgraph.h"
4+
#include "pycore_code.h" // write_location_entry_start()
55
#include "pycore_compile.h"
6+
#include "pycore_opcode.h" // _PyOpcode_Caches[] and opcode category macros
67
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
7-
#include "pycore_code.h" // write_location_entry_start()
88

99

1010
#define DEFAULT_CODE_SIZE 128
@@ -22,8 +22,8 @@
2222
}
2323

2424
typedef _PyCompilerSrcLocation location;
25-
typedef _PyCfgInstruction cfg_instr;
26-
typedef _PyCfgBasicblock basicblock;
25+
typedef _PyCompile_Instruction instruction;
26+
typedef _PyCompile_InstructionSequence instr_sequence;
2727

2828
static inline bool
2929
same_location(location a, location b)
@@ -117,21 +117,22 @@ assemble_emit_exception_table_item(struct assembler *a, int value, int msb)
117117
#define MAX_SIZE_OF_ENTRY 20
118118

119119
static int
120-
assemble_emit_exception_table_entry(struct assembler *a, int start, int end, basicblock *handler)
120+
assemble_emit_exception_table_entry(struct assembler *a, int start, int end,
121+
_PyCompile_ExceptHandlerInfo *handler)
121122
{
122123
Py_ssize_t len = PyBytes_GET_SIZE(a->a_except_table);
123124
if (a->a_except_table_off + MAX_SIZE_OF_ENTRY >= len) {
124125
RETURN_IF_ERROR(_PyBytes_Resize(&a->a_except_table, len * 2));
125126
}
126127
int size = end-start;
127128
assert(end > start);
128-
int target = handler->b_offset;
129-
int depth = handler->b_startdepth - 1;
130-
if (handler->b_preserve_lasti) {
129+
int target = handler->h_offset;
130+
int depth = handler->h_startdepth - 1;
131+
if (handler->h_preserve_lasti) {
131132
depth -= 1;
132133
}
133134
assert(depth >= 0);
134-
int depth_lasti = (depth<<1) | handler->b_preserve_lasti;
135+
int depth_lasti = (depth<<1) | handler->h_preserve_lasti;
135136
assemble_emit_exception_table_item(a, start, (1<<7));
136137
assemble_emit_exception_table_item(a, size, 0);
137138
assemble_emit_exception_table_item(a, target, 0);
@@ -140,29 +141,26 @@ assemble_emit_exception_table_entry(struct assembler *a, int start, int end, bas
140141
}
141142

142143
static int
143-
assemble_exception_table(struct assembler *a, basicblock *entryblock)
144+
assemble_exception_table(struct assembler *a, instr_sequence *instrs)
144145
{
145-
basicblock *b;
146146
int ioffset = 0;
147-
basicblock *handler = NULL;
147+
_PyCompile_ExceptHandlerInfo handler;
148+
handler.h_offset = -1;
148149
int start = -1;
149-
for (b = entryblock; b != NULL; b = b->b_next) {
150-
ioffset = b->b_offset;
151-
for (int i = 0; i < b->b_iused; i++) {
152-
cfg_instr *instr = &b->b_instr[i];
153-
if (instr->i_except != handler) {
154-
if (handler != NULL) {
155-
RETURN_IF_ERROR(
156-
assemble_emit_exception_table_entry(a, start, ioffset, handler));
157-
}
158-
start = ioffset;
159-
handler = instr->i_except;
150+
for (int i = 0; i < instrs->s_used; i++) {
151+
instruction *instr = &instrs->s_instrs[i];
152+
if (instr->i_except_handler_info.h_offset != handler.h_offset) {
153+
if (handler.h_offset >= 0) {
154+
RETURN_IF_ERROR(
155+
assemble_emit_exception_table_entry(a, start, ioffset, &handler));
160156
}
161-
ioffset += _PyCfg_InstrSize(instr);
157+
start = ioffset;
158+
handler = instr->i_except_handler_info;
162159
}
160+
ioffset += _PyCompile_InstrSize(instr->i_opcode, instr->i_oparg);
163161
}
164-
if (handler != NULL) {
165-
RETURN_IF_ERROR(assemble_emit_exception_table_entry(a, start, ioffset, handler));
162+
if (handler.h_offset >= 0) {
163+
RETURN_IF_ERROR(assemble_emit_exception_table_entry(a, start, ioffset, &handler));
166164
}
167165
return SUCCESS;
168166
}
@@ -316,31 +314,31 @@ assemble_emit_location(struct assembler* a, location loc, int isize)
316314
}
317315

318316
static int
319-
assemble_location_info(struct assembler *a, basicblock *entryblock, int firstlineno)
317+
assemble_location_info(struct assembler *a, instr_sequence *instrs,
318+
int firstlineno)
320319
{
321320
a->a_lineno = firstlineno;
322321
location loc = NO_LOCATION;
323322
int size = 0;
324-
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
325-
for (int j = 0; j < b->b_iused; j++) {
326-
if (!same_location(loc, b->b_instr[j].i_loc)) {
323+
for (int i = 0; i < instrs->s_used; i++) {
324+
instruction *instr = &instrs->s_instrs[i];
325+
if (!same_location(loc, instr->i_loc)) {
327326
RETURN_IF_ERROR(assemble_emit_location(a, loc, size));
328-
loc = b->b_instr[j].i_loc;
327+
loc = instr->i_loc;
329328
size = 0;
330-
}
331-
size += _PyCfg_InstrSize(&b->b_instr[j]);
332329
}
330+
size += _PyCompile_InstrSize(instr->i_opcode, instr->i_oparg);
333331
}
334332
RETURN_IF_ERROR(assemble_emit_location(a, loc, size));
335333
return SUCCESS;
336334
}
337335

338336
static void
339-
write_instr(_Py_CODEUNIT *codestr, cfg_instr *instruction, int ilen)
337+
write_instr(_Py_CODEUNIT *codestr, instruction *instr, int ilen)
340338
{
341-
int opcode = instruction->i_opcode;
339+
int opcode = instr->i_opcode;
342340
assert(!IS_PSEUDO_OPCODE(opcode));
343-
int oparg = instruction->i_oparg;
341+
int oparg = instr->i_oparg;
344342
assert(HAS_ARG(opcode) || oparg == 0);
345343
int caches = _PyOpcode_Caches[opcode];
346344
switch (ilen - caches) {
@@ -380,12 +378,12 @@ write_instr(_Py_CODEUNIT *codestr, cfg_instr *instruction, int ilen)
380378
*/
381379

382380
static int
383-
assemble_emit_instr(struct assembler *a, cfg_instr *i)
381+
assemble_emit_instr(struct assembler *a, instruction *instr)
384382
{
385383
Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
386384
_Py_CODEUNIT *code;
387385

388-
int size = _PyCfg_InstrSize(i);
386+
int size = _PyCompile_InstrSize(instr->i_opcode, instr->i_oparg);
389387
if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
390388
if (len > PY_SSIZE_T_MAX / 2) {
391389
return ERROR;
@@ -394,25 +392,24 @@ assemble_emit_instr(struct assembler *a, cfg_instr *i)
394392
}
395393
code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
396394
a->a_offset += size;
397-
write_instr(code, i, size);
395+
write_instr(code, instr, size);
398396
return SUCCESS;
399397
}
400398

401399
static int
402-
assemble_emit(struct assembler *a, basicblock *entryblock, int first_lineno,
403-
PyObject *const_cache)
400+
assemble_emit(struct assembler *a, instr_sequence *instrs,
401+
int first_lineno, PyObject *const_cache)
404402
{
405403
RETURN_IF_ERROR(assemble_init(a, first_lineno));
406404

407-
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
408-
for (int j = 0; j < b->b_iused; j++) {
409-
RETURN_IF_ERROR(assemble_emit_instr(a, &b->b_instr[j]));
410-
}
405+
for (int i = 0; i < instrs->s_used; i++) {
406+
instruction *instr = &instrs->s_instrs[i];
407+
RETURN_IF_ERROR(assemble_emit_instr(a, instr));
411408
}
412409

413-
RETURN_IF_ERROR(assemble_location_info(a, entryblock, a->a_lineno));
410+
RETURN_IF_ERROR(assemble_location_info(a, instrs, a->a_lineno));
414411

415-
RETURN_IF_ERROR(assemble_exception_table(a, entryblock));
412+
RETURN_IF_ERROR(assemble_exception_table(a, instrs));
416413

417414
RETURN_IF_ERROR(_PyBytes_Resize(&a->a_except_table, a->a_except_table_off));
418415
RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_except_table));
@@ -586,13 +583,13 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
586583

587584
PyCodeObject *
588585
_PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *umd, PyObject *const_cache,
589-
PyObject *consts, int maxdepth, basicblock *entryblock,
586+
PyObject *consts, int maxdepth, instr_sequence *instrs,
590587
int nlocalsplus, int code_flags, PyObject *filename)
591588
{
592589
PyCodeObject *co = NULL;
593590

594591
struct assembler a;
595-
int res = assemble_emit(&a, entryblock, umd->u_firstlineno, const_cache);
592+
int res = assemble_emit(&a, instrs, umd->u_firstlineno, const_cache);
596593
if (res == SUCCESS) {
597594
co = makecode(umd, &a, const_cache, consts, maxdepth, nlocalsplus,
598595
code_flags, filename);

0 commit comments

Comments
 (0)