From f8a3e07e47b63e99160597b19afc9c8893d9524e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Fri, 26 Apr 2024 10:12:39 +0200 Subject: [PATCH] Simplify AST expansion. 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. --- lib/tre-compile.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/tre-compile.c b/lib/tre-compile.c index ebccfb8..aaa9e37 100644 --- a/lib/tre-compile.c +++ b/lib/tre-compile.c @@ -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, ©, &max_pos); @@ -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; }