Skip to content

Commit

Permalink
Simplify AST expansion.
Browse files Browse the repository at this point in the history
In the final stage of expanding an iteration with a finite upper bound,
use the equivalent of (.(.(.)?)?)? instead of (|.(|.(|.))).  This saves
one AST node per iteration, reducing memory usage and very slightly
speeding up both AST expansion and AST-to-TNFA translation, although
the resulting TNFA appears unchanged in the cases I examined.  In
practical terms, retest and wretest both run a little over 5% faster
with this patch than without it.
  • Loading branch information
dag-erling committed Jun 30, 2024
1 parent 80dfb72 commit b1edf87
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions lib/tre-compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ tre_expand_ast(tre_mem_t mem, tre_stack_t *stack, tre_ast_node_t *ast,
{
for (j = iter->min; j < iter->max; j++)
{
tre_ast_node_t *tmp, *copy;
tre_ast_node_t *copy;
pos_add_save = pos_add;
status = tre_copy_ast(mem, stack, iter->arg, 0,
&pos_add, NULL, &copy, &max_pos);
Expand All @@ -949,10 +949,7 @@ tre_expand_ast(tre_mem_t mem, tre_stack_t *stack, tre_ast_node_t *ast,
seq2 = copy;
if (seq2 == NULL)
return REG_ESPACE;
tmp = tre_ast_new_literal(mem, EMPTY, -1, -1);
if (tmp == NULL)
return REG_ESPACE;
seq2 = tre_ast_new_union(mem, tmp, seq2);
seq2 = tre_ast_new_iter(mem, seq2, 0, 1, 0);
if (seq2 == NULL)
return REG_ESPACE;
}
Expand Down

0 comments on commit b1edf87

Please sign in to comment.