Skip to content

Commit ec8759b

Browse files
authored
bpo-43950: optimize column table assembling with pre-sizing object (GH-26997)
The new resizing system works like this; ``` $ cat t.py a + a + a + b + c + a + a + a + b + c + a + a + a + b + c + a + a + a + b + c [repeated 99 more times] $ ./python t.py RESIZE: prev len = 32, new len = 66 FINAL SIZE: 56 ----------------------------------------------------- RESIZE: prev len = 32, new len = 66 RESIZE: prev len = 66, new len = 134 RESIZE: prev len = 134, new len = 270 RESIZE: prev len = 270, new len = 542 RESIZE: prev len = 542, new len = 1086 RESIZE: prev len = 1086, new len = 2174 RESIZE: prev len = 2174, new len = 4350 RESIZE: prev len = 4350, new len = 8702 FINAL SIZE: 8004 ``` So now we do considerably lower number of `_PyBytes_Resize` calls. Automerge-Triggered-By: GH:isidentical
1 parent 2560c61 commit ec8759b

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Python/compile.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#define DEFAULT_BLOCKS 8
4141
#define DEFAULT_CODE_SIZE 128
4242
#define DEFAULT_LNOTAB_SIZE 16
43-
#define DEFAULT_CNOTAB_SIZE 0
43+
#define DEFAULT_CNOTAB_SIZE 32
4444

4545
#define COMP_GENEXP 0
4646
#define COMP_LISTCOMP 1
@@ -6587,6 +6587,7 @@ struct assembler {
65876587
PyObject* a_cnotab; /* bytes containing cnotab */
65886588
int a_lnotab_off; /* offset into lnotab */
65896589
int a_enotab_off; /* offset into enotab */
6590+
int a_cnotab_off; /* offset into cnotab */
65906591
PyObject *a_except_table; /* bytes containing exception table */
65916592
int a_except_table_off; /* offset into exception table */
65926593
int a_prevlineno; /* lineno of last emitted line in line table */
@@ -6696,6 +6697,7 @@ assemble_init(struct assembler *a, int nblocks, int firstlineno)
66966697
a->a_lnotab = NULL;
66976698
a->a_enotab = NULL;
66986699
a->a_cnotab = NULL;
6700+
a->a_cnotab_off = 0;
66996701
a->a_except_table = NULL;
67006702
a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
67016703
if (a->a_bytecode == NULL) {
@@ -7106,14 +7108,16 @@ static int
71067108
assemble_cnotab(struct assembler* a, struct instr* i, int instr_size)
71077109
{
71087110
Py_ssize_t len = PyBytes_GET_SIZE(a->a_cnotab);
7109-
// TODO: Allocate more memory than just what we immediately need
7110-
// like a_lnotab does.
7111-
if (_PyBytes_Resize(&a->a_cnotab, len + (instr_size * 2)) < 0) {
7112-
return 0;
7111+
int difference = instr_size * 2;
7112+
if (a->a_cnotab_off + difference >= len) {
7113+
if (_PyBytes_Resize(&a->a_cnotab, difference + (len * 2)) < 0) {
7114+
return 0;
7115+
}
71137116
}
71147117

71157118
unsigned char* cnotab = (unsigned char*)PyBytes_AS_STRING(a->a_cnotab);
7116-
cnotab += len;
7119+
cnotab += a->a_cnotab_off;
7120+
a->a_cnotab_off += difference;
71177121

71187122
for (int j = 0; j < instr_size; j++) {
71197123
if (i->i_col_offset > 255 || i->i_end_col_offset > 255) {
@@ -7855,6 +7859,9 @@ assemble(struct compiler *c, int addNone)
78557859
if (!merge_const_one(c, &a.a_enotab)) {
78567860
goto error;
78577861
}
7862+
if (_PyBytes_Resize(&a.a_cnotab, a.a_cnotab_off) < 0) {
7863+
goto error;
7864+
}
78587865
if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
78597866
goto error;
78607867
}

0 commit comments

Comments
 (0)