Skip to content

Commit 90bf8df

Browse files
committed
Avoid reallocating state for each basic block
1 parent 7291c49 commit 90bf8df

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

Python/flowgraph.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,9 +2534,16 @@ optimize_load_fast(cfg_builder *g)
25342534
{
25352535
int status;
25362536
ref_stack refs = {0};
2537-
bool *has_killed_refs = NULL;
2537+
int max_instrs = 0;
25382538
for (basicblock *b = g->g_block_list; b != NULL; b = b->b_list) {
25392539
b->b_startdepth = -1;
2540+
max_instrs = Py_MAX(max_instrs, b->b_iused);
2541+
}
2542+
size_t has_killed_refs_size = max_instrs * sizeof(bool);
2543+
bool *has_killed_refs = PyMem_Calloc(max_instrs, has_killed_refs_size);
2544+
if (has_killed_refs == NULL) {
2545+
PyErr_NoMemory();
2546+
return ERROR;
25402547
}
25412548
basicblock *entryblock = g->g_entryblock;
25422549
basicblock **blocks = make_cfg_traversal_stack(entryblock);
@@ -2556,17 +2563,7 @@ optimize_load_fast(cfg_builder *g)
25562563

25572564
// Reset state that tracks which instructions produce references to
25582565
// locals that are on the stack while the local is overwritten.
2559-
int size = sizeof(*has_killed_refs) * block->b_iused;
2560-
bool *p = PyMem_Realloc(has_killed_refs, size);
2561-
if (p == NULL) {
2562-
PyErr_NoMemory();
2563-
status = ERROR;
2564-
goto done;
2565-
}
2566-
else {
2567-
has_killed_refs = p;
2568-
}
2569-
memset(has_killed_refs, 0, size);
2566+
memset(has_killed_refs, 0, has_killed_refs_size);
25702567

25712568
// Reset the stack of refs. We don't track references on the stack
25722569
// across basic blocks, but the bytecode will expect their

0 commit comments

Comments
 (0)