summaryrefslogtreecommitdiff
path: root/perly.c
diff options
context:
space:
mode:
authorDavid Mitchell <davem@iabyn.com>2016-12-03 14:01:19 +0000
committerDavid Mitchell <davem@iabyn.com>2016-12-05 11:54:03 +0000
commit0e0707c5741b52ed1f26e1f69d89b66a1b05f985 (patch)
tree4d6dd171e310f637b0b9cf33d63efbd1555167fb /perly.c
parenta14be3eb83c92f38596c3a429d631615e84e660b (diff)
downloadperl-0e0707c5741b52ed1f26e1f69d89b66a1b05f985.tar.gz
optimising yyparse: replace stack_size with a ptr
Makes testing whether the parser stack needs extending cheaper
Diffstat (limited to 'perly.c')
-rw-r--r--perly.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/perly.c b/perly.c
index 2d5857d0c0..d20e4e3b7d 100644
--- a/perly.c
+++ b/perly.c
@@ -273,17 +273,17 @@ Perl_yyparse (pTHX_ int gramtype)
SAVEPPTR(parser->yylval.pval);
SAVEINT(parser->yychar);
SAVEINT(parser->yyerrstatus);
- SAVEINT(parser->stack_size);
SAVEINT(parser->yylen);
SAVEVPTR(parser->stack);
+ SAVEVPTR(parser->stack_max1);
SAVEVPTR(parser->ps);
/* initialise state for this parse */
parser->yychar = gramtype;
parser->yyerrstatus = 0;
- parser->stack_size = YYINITDEPTH;
parser->yylen = 0;
Newx(parser->stack, YYINITDEPTH, yy_stack_frame);
+ parser->stack_max1 = parser->stack + YYINITDEPTH - 1;
ps = parser->ps = parser->stack;
ps->state = 0;
SAVEDESTRUCTOR_X(S_clear_yystack, parser);
@@ -300,20 +300,22 @@ Perl_yyparse (pTHX_ int gramtype)
parser->yylen = 0;
{
- size_t size = ps - parser->stack + 1;
-
/* grow the stack? We always leave 1 spare slot,
- * in case of a '' -> 'foo' reduction */
+ * in case of a '' -> 'foo' reduction.
+ * Note that stack_max1 points to the (top-1)th allocated stack
+ * element to make this check fast */
- if (size >= (size_t)parser->stack_size - 1) {
+ if (ps >= parser->stack_max1) {
+ Size_t pos = ps - parser->stack;
+ Size_t newsize = 2 * (parser->stack_max1 + 2 - parser->stack);
/* this will croak on insufficient memory */
- parser->stack_size *= 2;
- Renew(parser->stack, parser->stack_size, yy_stack_frame);
- ps = parser->ps = parser->stack + size -1;
+ Renew(parser->stack, newsize, yy_stack_frame);
+ ps = parser->ps = parser->stack + pos;
+ parser->stack_max1 = parser->stack + newsize - 1;
YYDPRINTF((Perl_debug_log,
"parser stack size increased to %lu frames\n",
- (unsigned long int)parser->stack_size));
+ (unsigned long int)newsize));
}
}