Skip to contentSkip to conversation

src: add symbol range expression to further compact intervals

PATCH New Pablo Neira Ayuso 1 participant 1 message Feb 13, 2025 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 src: add symbol range expression to further compact intervals

Update parser to use a new symbol range expression with smaller memory footprint than range expression + two symbol expressions.

The evaluation step translates this into EXPR_RANGE_VALUE from the evaluation step for interval sets.

Note that maps or concatenations still use the less compact range expressions representation, those requires more work to use this new symbol range expression. The parser also uses the classic range expression if variables are used.

Testing with a 100k intervals, worst case scenario: no prefix or singleton elements, show a reduction from 49.58 Mbytes to 35.47 Mbytes for 100k intervals (-29.56% memory footprint for this case).

This follow up work to previous commits:

91dc281a82ea ("src: rework singleton interval transformation to reduce memory consumption") c9ee9032b0ee ("src: add EXPR_RANGE_VALUE expression and use it")

---
 include/expression.h | 13 ++++++++++--
 src/evaluate.c       | 47 ++++++++++++++++++++++++++++++++++++++++++++
 src/expression.c     | 45 ++++++++++++++++++++++++++++++++++++++++++
 src/optimize.c       |  3 +++
 src/parser_bison.y   | 11 ++++++++++-
 5 files changed, 116 insertions(+), 3 deletions(-)
include/expression.h +11 -2
index f2b45250872d..8472748621ef 100644
@@ -49,6 +49,7 @@
  * @EXPR_SET_ELEM_CATCHALL catchall element expression
  * @EXPR_FLAGCMP	flagcmp expression
  * @EXPR_RANGE_VALUE	constant range expression
+ * @EXPR_RANGE_SYMBOL	unparse symbol range expression
  */
 enum expr_types {
 	EXPR_INVALID,
@@ -82,6 +83,7 @@ enum expr_types {
 	EXPR_SET_ELEM_CATCHALL,
 	EXPR_FLAGCMP,
 	EXPR_RANGE_VALUE,
+	EXPR_RANGE_SYMBOL,
 
 	EXPR_MAX = EXPR_FLAGCMP
 };
@@ -261,9 +263,12 @@ struct expr {
 
 	union {
 		struct {
-			/* EXPR_SYMBOL */
+			/* EXPR_SYMBOL, EXPR_RANGE_SYMBOL */
 			const struct scope	*scope;
-			const char		*identifier;
+			union {
+				const char	*identifier;
+				const char	*identifier_range[2];
+			};
 			enum symbol_types	symtype;
 		};
 		struct {
@@ -486,6 +491,10 @@ extern struct expr *constant_range_expr_alloc(const struct location *loc,
 					      unsigned int len,
 					      mpz_t low, mpz_t high);
 
+struct expr *symbol_range_expr_alloc(const struct location *loc,
+				     enum symbol_types type, const struct scope *scope,
+				     const char *identifier_low, const char *identifier_high);
+
 extern struct expr *flag_expr_alloc(const struct location *loc,
 				    const struct datatype *dtype,
 				    enum byteorder byteorder,
src/evaluate.c +47 -0

59 lines not on this page · raw message

src/expression.c +45 -0

57 lines not on this page · raw message

src/optimize.c +3 -0
index 03c8bad234e2..230fe4a23de3 100644
@@ -139,6 +139,7 @@ static bool stmt_expr_supported(const struct expr *expr)
 {
 	switch (expr->right->etype) {
 	case EXPR_SYMBOL:
+	case EXPR_RANGE_SYMBOL:
 	case EXPR_RANGE:
 	case EXPR_PREFIX:
 	case EXPR_SET:
@@ -630,6 +631,7 @@ static void __merge_concat(const struct optimize_ctx *ctx, uint32_t i,
 			case EXPR_SYMBOL:
 			case EXPR_VALUE:
 			case EXPR_PREFIX:
+			case EXPR_RANGE_SYMBOL:
 			case EXPR_RANGE:
 				clone = expr_clone(stmt_a->expr->right);
 				compound_expr_add(concat, clone);
@@ -730,6 +732,7 @@ static void build_verdict_map(struct expr *expr, struct stmt *verdict,
 		stmt_free(counter);
 		break;
 	case EXPR_PREFIX:
+	case EXPR_RANGE_SYMBOL:
 	case EXPR_RANGE:
 	case EXPR_VALUE:
 	case EXPR_SYMBOL:
src/parser_bison.y +10 -1
index d15bf212489d..1aaccc00369c 100644
@@ -4462,7 +4462,16 @@ prefix_rhs_expr		:	basic_rhs_expr	SLASH	NUM
 
 range_rhs_expr		:	basic_rhs_expr	DASH	basic_rhs_expr
 			{
-				$$ = range_expr_alloc(&@$, $1, $3);
+				if ($1->etype == EXPR_SYMBOL &&
+				    $1->symtype == SYMBOL_VALUE &&
+				    $3->etype == EXPR_SYMBOL &&
+				    $3->symtype == SYMBOL_VALUE) {
+					$$ = symbol_range_expr_alloc(&@$, $1->symtype, $1->scope, $1->identifier, $3->identifier);
+					expr_free($1);
+					expr_free($3);
+				} else {
+					$$ = range_expr_alloc(&@$, $1, $3);
+				}
 			}
 			;
 
Signature · 1 line
2.30.2