Skip to content

Commit

Permalink
deduplicate max_label calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Sep 20, 2023
1 parent 5234f4d commit 56d80a6
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,16 +575,23 @@ check_cfg(cfg_builder *g) {
return SUCCESS;
}

/* Calculate the actual jump target from the target_label */
static int
translate_jump_labels_to_targets(basicblock *entryblock)
get_max_label(basicblock *entryblock)
{
int max_label = -1;
int lbl = -1;
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
if (b->b_label.id > max_label) {
max_label = b->b_label.id;
if (b->b_label.id > lbl) {
lbl = b->b_label.id;
}
}
return lbl;
}

/* Calculate the actual jump target from the target_label */
static int
translate_jump_labels_to_targets(basicblock *entryblock)
{
int max_label = get_max_label(entryblock);
size_t mapsize = sizeof(basicblock *) * (max_label + 1);
basicblock **label2block = (basicblock **)PyMem_Malloc(mapsize);
if (!label2block) {
Expand Down Expand Up @@ -2230,18 +2237,6 @@ is_exit_without_lineno(basicblock *b) {
}


static int
max_label(cfg_builder *g)
{
int lbl = -1;
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
if (b->b_label.id > lbl) {
lbl = b->b_label.id;
}
}
return lbl;
}

/* PEP 626 mandates that the f_lineno of a frame is correct
* after a frame terminates. It would be prohibitively expensive
* to continuously update the f_lineno field at runtime,
Expand All @@ -2256,7 +2251,7 @@ duplicate_exits_without_lineno(cfg_builder *g)
{
assert(no_empty_basic_blocks(g));

int next_lbl = max_label(g) + 1;
int next_lbl = get_max_label(g->g_entryblock) + 1;

/* Copy all exit blocks without line number that are targets of a jump.
*/
Expand Down

0 comments on commit 56d80a6

Please sign in to comment.