All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Haller <thaller@redhat.com>
To: NetFilter <netfilter-devel@vger.kernel.org>
Cc: Thomas Haller <thaller@redhat.com>
Subject: [PATCH nft 3/5] datatype: don't clone datatype in set_datatype_alloc() if byteorder already matches
Date: Wed, 27 Sep 2023 21:57:26 +0200	[thread overview]
Message-ID: <20230927200143.3798124-4-thaller@redhat.com> (raw)
In-Reply-To: <20230927200143.3798124-1-thaller@redhat.com>

"struct datatype" instances are treated immutable and not changed after
creation.  set_datatype_alloc() returns a const pointer, indicating that
the caller must not modify the instance anymore. In particular it must
not, because for non-integer types we just return a reference to the
(already immutable) orig_dtype.

If the byteorder that we are about to set is already as-requested, there
is no need to clone the instance either. Just return a reference to
orig_dtype() too.

Also drop the same optimization from key_fix_dtype_byteorder().

Signed-off-by: Thomas Haller <thaller@redhat.com>
---
 src/datatype.c |  6 +++++-
 src/evaluate.c | 19 +++++++------------
 2 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/src/datatype.c b/src/datatype.c
index 1c557a06c751..6a35c6a76028 100644
--- a/src/datatype.c
+++ b/src/datatype.c
@@ -1321,9 +1321,13 @@ const struct datatype *set_datatype_alloc(const struct datatype *orig_dtype,
 	if (orig_dtype != &integer_type)
 		return datatype_get(orig_dtype);
 
+	if (orig_dtype->byteorder == byteorder) {
+		/* The (immutable) type instance is already as requested. */
+		return datatype_get(orig_dtype);
+	}
+
 	dtype = datatype_clone(orig_dtype);
 	dtype->byteorder = byteorder;
-
 	return dtype;
 }
 
diff --git a/src/evaluate.c b/src/evaluate.c
index c699a9bc7b86..e84895bf1610 100644
--- a/src/evaluate.c
+++ b/src/evaluate.c
@@ -74,17 +74,8 @@ static int __fmtstring(3, 4) set_error(struct eval_ctx *ctx,
 	return -1;
 }
 
-static void key_fix_dtype_byteorder(struct expr *key)
-{
-	const struct datatype *dtype = key->dtype;
-
-	if (dtype->byteorder == key->byteorder)
-		return;
-
-	__datatype_set(key, set_datatype_alloc(dtype, key->byteorder));
-}
-
 static int set_evaluate(struct eval_ctx *ctx, struct set *set);
+
 static struct expr *implicit_set_declaration(struct eval_ctx *ctx,
 					     const char *name,
 					     struct expr *key,
@@ -95,8 +86,12 @@ static struct expr *implicit_set_declaration(struct eval_ctx *ctx,
 	struct set *set;
 	struct handle h;
 
-	if (set_is_datamap(expr->set_flags))
-		key_fix_dtype_byteorder(key);
+	if (set_is_datamap(expr->set_flags)) {
+		const struct datatype *dtype;
+
+		dtype = set_datatype_alloc(key->dtype, key->byteorder);
+		__datatype_set(key, dtype);
+	}
 
 	set = set_alloc(&expr->location);
 	set->flags	= NFT_SET_ANONYMOUS | expr->set_flags;
-- 
2.41.0


  parent reply	other threads:[~2023-09-27 20:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-27 19:57 [PATCH nft 0/5] more various cleanups related to struct datatype Thomas Haller
2023-09-27 19:57 ` [PATCH nft 1/5] datatype: make "flags" field of datatype struct simple booleans Thomas Haller
2024-08-19 22:41   ` Pablo Neira Ayuso
2023-09-27 19:57 ` [PATCH nft 2/5] datatype: don't clone static name/desc strings for datatype Thomas Haller
2023-09-27 19:57 ` Thomas Haller [this message]
2023-09-27 19:57 ` [PATCH nft 4/5] datatype: extend set_datatype_alloc() to change size Thomas Haller
2023-09-27 19:57 ` [PATCH nft 5/5] datatype: use xmalloc() for allocating datatype in datatype_clone() Thomas Haller
2023-09-28 19:11   ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230927200143.3798124-4-thaller@redhat.com \
    --to=thaller@redhat.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.