>From c43f1ee93ccfe84ddf4504e7d232f645416dc354 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Sun, 20 Sep 2026 11:10:41 +0000 Subject: [PATCH 17/25] regex: struct changes for memory saving the type changes should be safe: backref is in -1..9, assertions is in 0..511, node type is 0..3, nullable is -1..1. Note: the node type was an enum which could warn about missed switch cases. tre_pos_and_tags_t and tre_tnfa_transition_t may be used O(n^2) times and dominate memory use. the size saving is 20-30% on 64bit targets: before -> now (-> with char tre_ctype_t -> packed assert/class) tre_ast_node_t: 48 -> 40 (-> 40 -> 40) tre_pos_and_tags_t: 56 -> 40 (-> 32 -> 24) tre_tnfa_transition_t: 56 -> 48 (-> 40 -> 32) tre_ctype_t can be char instead of wctype_t (but not portable outside musl). assertions+backref+class+neg_classes can be packed into 32bits. however this patch avoids source/logic changes. --- src/regex/regcomp.c | 12 ++++++------ src/regex/tre.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/regex/regcomp.c b/src/regex/regcomp.c index c3400c3d..c541e2fd 100644 --- a/src/regex/regcomp.c +++ b/src/regex/regcomp.c @@ -48,11 +48,11 @@ typedef struct { int position; int code_min; int code_max; - int *tags; - int assertions; + short assertions; + signed char backref; tre_ctype_t class; tre_ctype_t *neg_classes; - int backref; + int *tags; } tre_pos_and_tags_t; @@ -84,10 +84,10 @@ typedef enum { /* A generic AST node. All AST nodes consist of this node on the top level with `obj' pointing to the actual content. */ typedef struct { - tre_ast_type_t type; /* Type of the node. */ - void *obj; /* Pointer to actual node. */ - int nullable; + char type; /* Type of the node. */ + signed char nullable; int submatch_id; + void *obj; /* Pointer to actual node. */ int num_submatches; int num_tags; tre_pos_and_tags_t *firstpos; diff --git a/src/regex/tre.h b/src/regex/tre.h index 55b17257..0ad77f1d 100644 --- a/src/regex/tre.h +++ b/src/regex/tre.h @@ -98,19 +98,19 @@ struct tnfa_transition { tre_tnfa_transition_t *state; /* ID number of the destination state. */ int state_id; - /* -1 terminated array of tags (or NULL). */ - int *tags; /* Assertion bitmap. */ - int assertions; + short assertions; /* Assertion parameters. */ union { /* Character class assertion. */ tre_ctype_t class; /* Back reference assertion. */ - int backref; + signed char backref; } u; /* Negative character class assertions. */ tre_ctype_t *neg_classes; + /* -1 terminated array of tags (or NULL). */ + int *tags; }; -- 2.52.0