"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@xxxxxxxxxx> --- 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