@@ -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;
}
@@ -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;
"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(-)