diff options
author | David Mitchell <davem@iabyn.com> | 2016-12-03 14:01:19 +0000 |
---|---|---|
committer | David Mitchell <davem@iabyn.com> | 2016-12-05 11:54:03 +0000 |
commit | 0e0707c5741b52ed1f26e1f69d89b66a1b05f985 (patch) | |
tree | 4d6dd171e310f637b0b9cf33d63efbd1555167fb /perly.c | |
parent | a14be3eb83c92f38596c3a429d631615e84e660b (diff) | |
download | perl-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.c | 22 |
1 files changed, 12 insertions, 10 deletions
@@ -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)); } } |