From 6d844e008503b7b5e9bde81c59a7e1324127ddf5 Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Sat, 16 Sep 2017 21:35:50 -0700 Subject: [PATCH] Avoid recursion in optimize_bb() when only 1 child node 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. --- src/spesh/optimize.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/spesh/optimize.c b/src/spesh/optimize.c index c790204728..94b85a30ed 100644 --- a/src/spesh/optimize.c +++ b/src/spesh/optimize.c @@ -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);