summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorIlya Maximets <i.maximets@ovn.org>2021-11-22 01:09:32 +0100
committerIlya Maximets <i.maximets@ovn.org>2021-11-30 13:34:03 +0100
commitdec429168461052b62f205d211cb630c201bc28a (patch)
treef18cc07b5c8546d6ba082d95e67a9b25c3537405 /lib
parent9d29990c21578a9a28b56b2ecdf8c18a1cb16b00 (diff)
downloadopenvswitch-dec429168461052b62f205d211cb630c201bc28a.tar.gz
ovsdb-data: Consolidate ovsdb atom and json strings.
ovsdb_atom_string and json_string are basically the same data structure and ovsdb-server frequently needs to convert one to another. We can avoid that by using json_string from the beginning for all ovsdb strings. So, the conversion turns into simple json_clone(), i.e. increment of a reference counter. This change gives a moderate performance boost in some scenarios, improves the code clarity and may be useful for future development. Acked-by: Mike Pattrick <mkp@redhat.com> Acked-by: Dumitru Ceara <dceara@redhat.com> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/ovsdb-data.c27
-rw-r--r--lib/ovsdb-data.h25
2 files changed, 19 insertions, 33 deletions
diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c
index 6654ed6de..2832e94ea 100644
--- a/lib/ovsdb-data.c
+++ b/lib/ovsdb-data.c
@@ -136,7 +136,7 @@ ovsdb_atom_is_default(const union ovsdb_atom *atom,
return atom->boolean == false;
case OVSDB_TYPE_STRING:
- return atom->s->string[0] == '\0';
+ return json_string(atom->s)[0] == '\0';
case OVSDB_TYPE_UUID:
return uuid_is_zero(&atom->uuid);
@@ -172,8 +172,7 @@ ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old,
break;
case OVSDB_TYPE_STRING:
- new->s = old->s;
- new->s->n_refs++;
+ new->s = json_clone(old->s);
break;
case OVSDB_TYPE_UUID:
@@ -215,7 +214,7 @@ ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
return hash_boolean(atom->boolean, basis);
case OVSDB_TYPE_STRING:
- return hash_string(atom->s->string, basis);
+ return json_hash(atom->s, basis);
case OVSDB_TYPE_UUID:
return hash_int(uuid_hash(&atom->uuid), basis);
@@ -247,7 +246,7 @@ ovsdb_atom_compare_3way(const union ovsdb_atom *a,
return a->boolean - b->boolean;
case OVSDB_TYPE_STRING:
- return a->s == b->s ? 0 : strcmp(a->s->string, b->s->string);
+ return a->s == b->s ? 0 : strcmp(json_string(a->s), json_string(b->s));
case OVSDB_TYPE_UUID:
return uuid_compare_3way(&a->uuid, &b->uuid);
@@ -405,7 +404,7 @@ ovsdb_atom_from_json__(union ovsdb_atom *atom,
case OVSDB_TYPE_STRING:
if (json->type == JSON_STRING) {
- atom->s = ovsdb_atom_string_create(json->string);
+ atom->s = json_clone(json);
return NULL;
}
break;
@@ -474,7 +473,7 @@ ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
return json_boolean_create(atom->boolean);
case OVSDB_TYPE_STRING:
- return json_string_create(atom->s->string);
+ return json_clone(atom->s);
case OVSDB_TYPE_UUID:
return wrap_json("uuid", json_string_create_nocopy(
@@ -726,14 +725,10 @@ ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
break;
case OVSDB_TYPE_STRING:
- if (string_needs_quotes(atom->s->string)) {
- struct json json;
-
- json.type = JSON_STRING;
- json.string = atom->s->string;
- json_to_ds(&json, 0, out);
+ if (string_needs_quotes(json_string(atom->s))) {
+ json_to_ds(atom->s, 0, out);
} else {
- ds_put_cstr(out, atom->s->string);
+ ds_put_cstr(out, json_string(atom->s));
}
break;
@@ -755,7 +750,7 @@ ovsdb_atom_to_bare(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
struct ds *out)
{
if (type == OVSDB_TYPE_STRING) {
- ds_put_cstr(out, atom->s->string);
+ ds_put_cstr(out, json_string(atom->s));
} else {
ovsdb_atom_to_string(atom, type, out);
}
@@ -882,7 +877,7 @@ ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
return NULL;
case OVSDB_TYPE_STRING:
- return check_string_constraints(atom->s->string, &base->string);
+ return check_string_constraints(json_string(atom->s), &base->string);
case OVSDB_TYPE_UUID:
return NULL;
diff --git a/lib/ovsdb-data.h b/lib/ovsdb-data.h
index f66ed3472..47115a7b8 100644
--- a/lib/ovsdb-data.h
+++ b/lib/ovsdb-data.h
@@ -19,6 +19,7 @@
#include <stdlib.h>
#include "compiler.h"
#include "ovsdb-types.h"
+#include "openvswitch/json.h"
#include "openvswitch/shash.h"
#include "util.h"
@@ -32,25 +33,16 @@ struct ds;
struct ovsdb_symbol_table;
struct smap;
-struct ovsdb_atom_string {
- char *string;
- size_t n_refs;
-};
-
-static inline struct ovsdb_atom_string *
+static inline struct json *
ovsdb_atom_string_create_nocopy(char *str)
{
- struct ovsdb_atom_string *s = xzalloc(sizeof *s);
-
- s->string = str;
- s->n_refs = 1;
- return s;
+ return json_string_create_nocopy(str);
}
-static inline struct ovsdb_atom_string *
+static inline struct json *
ovsdb_atom_string_create(const char *str)
{
- return ovsdb_atom_string_create_nocopy(xstrdup(str));
+ return json_string_create(str);
}
/* One value of an atomic type (given by enum ovs_atomic_type). */
@@ -58,7 +50,7 @@ union ovsdb_atom {
int64_t integer;
double real;
bool boolean;
- struct ovsdb_atom_string *s;
+ struct json *s;
struct uuid uuid;
};
@@ -88,9 +80,8 @@ ovsdb_atom_needs_destruction(enum ovsdb_atomic_type type)
static inline void
ovsdb_atom_destroy(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
{
- if (type == OVSDB_TYPE_STRING && !--atom->s->n_refs) {
- free(atom->s->string);
- free(atom->s);
+ if (type == OVSDB_TYPE_STRING) {
+ json_destroy(atom->s);
}
}