Skip to content

Commit

Permalink
Avoid recursion in optimize_bb() when only 1 child node
Browse files Browse the repository at this point in the history
Optimize the case where we only have one child. This avoids having
to do a recursive call to optimize_bb(). Keep following the nodes and
running optimize_bb_switch() on them until we hit one with more than 1
child.
Reduces the depth of recursion when compiling nqp from 99 to 29.
  • Loading branch information
samcv committed Sep 17, 2017
1 parent 5528083 commit 6d844e0
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/spesh/optimize.c
Expand Up @@ -2181,10 +2181,16 @@ static void optimize_bb_switch(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshB
static void optimize_bb(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb,
MVMSpeshPlanned *p) {
MVMint64 i = 0;
/* Because this optimize_bb() can be deeply recursive, separate as much code
/* Because optimize_bb() can be deeply recursive, separate as much code
* as possible into a separate function optimize_bb_switch(), so we don't
* trash the stack. (needed on musl) */
optimize_bb_switch(tc, g, bb, p);
/* Optimize the case where we only have one child. This avoids having
* to do a recursive call to optimize_bb(). Keep following the nodes and
* running optimize_bb_switch() on them until we hit one with more than 1
* child. */
while (bb->num_children == 1)
optimize_bb_switch(tc, g, (bb = bb->children[0]), p);
/* Visit children. */
for (; i < bb->num_children; i++)
optimize_bb(tc, g, bb->children[i], p);
Expand Down

0 comments on commit 6d844e0

Please sign in to comment.