Skip to contentSkip to conversation

expression: remove elem_flags from EXPR_SET_ELEM to shrink struct expr size

PATCH New Pablo Neira Ayuso 1 participant 2 messages Dec 9, 2024 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 expression: remove elem_flags from EXPR_SET_ELEM to shrink struct expr size

Move NFTNL_SET_ELEM_F_INTERVAL_OPEN flag to the existing flags field in struct expr.

This saves 4 bytes in struct expr, shrinking it to 128 bytes according to pahole. This reworks:

6089630f54ce ("segtree: Introduce flag for half-open range elements")

---
low hanging fruit to shrink struct expr.

 include/expression.h |  2 +-
 src/intervals.c      |  4 ++--
 src/monitor.c        |  2 +-
 src/netlink.c        | 15 ++++++++++-----
 4 files changed, 14 insertions(+), 9 deletions(-)
include/expression.h +1 -1
index da2f693e72d3..877887ff1978 100644
@@ -215,6 +215,7 @@ enum expr_flags {
 	EXPR_F_INTERVAL		= 0x20,
 	EXPR_F_KERNEL		= 0x40,
 	EXPR_F_REMOVE		= 0x80,
+	EXPR_F_INTERVAL_OPEN	= 0x100,
 };
 
 #include <payload.h>
@@ -301,7 +302,6 @@ struct expr {
 			uint64_t		expiration;
 			const char		*comment;
 			struct list_head	stmt_list;
-			uint32_t		elem_flags;
 		};
 		struct {
 			/* EXPR_UNARY */
src/intervals.c +2 -2
index a93ceedd5a7a..7fd4a576f604 100644
@@ -740,7 +740,7 @@ int set_to_intervals(const struct set *set, struct expr *init, bool add)
 			}
 			newelem->flags |= EXPR_F_INTERVAL_END;
 		} else {
-			flags = NFTNL_SET_ELEM_F_INTERVAL_OPEN;
+			flags = EXPR_F_INTERVAL_OPEN;
 		}
 
 		expr = constant_expr_alloc(&elem->key->location, set->key->dtype,
@@ -752,7 +752,7 @@ int set_to_intervals(const struct set *set, struct expr *init, bool add)
 
 		expr_free(elem->key);
 		elem->key = expr;
-		i->elem_flags |= flags;
+		i->flags |= flags;
 		init->size++;
 		list_move_tail(&i->list, &intervals);
 
src/monitor.c +1 -1
index a787db8cbf5a..d70bf2957dd3 100644
@@ -376,7 +376,7 @@ static bool set_elem_is_open_interval(struct expr *elem)
 {
 	switch (elem->etype) {
 	case EXPR_SET_ELEM:
-		return elem->elem_flags & NFTNL_SET_ELEM_F_INTERVAL_OPEN;
+		return elem->flags & EXPR_F_INTERVAL_OPEN;
 	case EXPR_MAPPING:
 		return set_elem_is_open_interval(elem->left);
 	default:
src/netlink.c +10 -5
index 36140fb63d6f..8e6e2066fe2a 100644
@@ -180,7 +180,7 @@ struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
 						netlink_gen_stmt_stateful(stmt));
 		}
 	}
-	if (elem->comment || expr->elem_flags) {
+	if (elem->comment || expr->flags & EXPR_F_INTERVAL_OPEN) {
 		udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
 		if (!udbuf)
 			memory_allocation_error();
@@ -190,9 +190,9 @@ struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
 					  elem->comment))
 			memory_allocation_error();
 	}
-	if (expr->elem_flags) {
+	if (expr->flags & EXPR_F_INTERVAL_OPEN) {
 		if (!nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_ELEM_FLAGS,
-					 expr->elem_flags))
+					 NFTNL_SET_ELEM_F_INTERVAL_OPEN))
 			memory_allocation_error();
 	}
 	if (udbuf) {
@@ -1372,9 +1372,14 @@ static void set_elem_parse_udata(struct nftnl_set_elem *nlse,
 	if (ud[NFTNL_UDATA_SET_ELEM_COMMENT])
 		expr->comment =
 			xstrdup(nftnl_udata_get(ud[NFTNL_UDATA_SET_ELEM_COMMENT]));
-	if (ud[NFTNL_UDATA_SET_ELEM_FLAGS])
-		expr->elem_flags =
+	if (ud[NFTNL_UDATA_SET_ELEM_FLAGS]) {
+		uint32_t elem_flags;
+
+		elem_flags =
 			nftnl_udata_get_u32(ud[NFTNL_UDATA_SET_ELEM_FLAGS]);
+		if (elem_flags & NFTNL_SET_ELEM_F_INTERVAL_OPEN)
+			expr->flags |= EXPR_F_INTERVAL_OPEN;
+	}
 }
 
 int netlink_delinearize_setelem(struct nftnl_set_elem *nlse,
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 src: remove unused token_offset from struct location

This saves 8 bytes in x86_64 in struct location which is embedded in every expression.

This shrinks struct expr to 120 bytes according to pahole.

---
low hanging fruit to shrink struct expr.

 include/nftables.h | 1 -
 src/parser_bison.y | 2 --
 src/scanner.l      | 1 -
 3 files changed, 4 deletions(-)
include/nftables.h +0 -1
index c25deb3676dd..7d891b439a2d 100644
@@ -158,7 +158,6 @@ struct location {
 	const struct input_descriptor		*indesc;
 	union {
 		struct {
-			off_t			token_offset;
 			off_t			line_offset;
 
 			unsigned int		first_line;
src/parser_bison.y +0 -2
index 6e6f3cf8335d..6e8b639104fc 100644
@@ -102,7 +102,6 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 {
 	if (n) {
 		loc->indesc       = rhs[n].indesc;
-		loc->token_offset = rhs[1].token_offset;
 		loc->line_offset  = rhs[1].line_offset;
 		loc->first_line   = rhs[1].first_line;
 		loc->first_column = rhs[1].first_column;
@@ -110,7 +109,6 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 		loc->last_column  = rhs[n].last_column;
 	} else {
 		loc->indesc       = rhs[0].indesc;
-		loc->token_offset = rhs[0].token_offset;
 		loc->line_offset  = rhs[0].line_offset;
 		loc->first_line   = loc->last_line   = rhs[0].last_line;
 		loc->first_column = loc->last_column = rhs[0].last_column;
src/scanner.l +0 -1
index c825fa79cfd9..ecdba404b2cd 100644
@@ -90,7 +90,6 @@ static void update_offset(struct parser_state *state, struct location *loc,
 			  unsigned int len)
 {
 	state->indesc->token_offset	+= len;
-	loc->token_offset		= state->indesc->token_offset;
 	loc->line_offset		= state->indesc->line_offset;
 }
 
Signature · 1 line
2.30.2