Skip to content

Commit

Permalink
json: Inline clone and destroy functions.
Browse files Browse the repository at this point in the history
With the next commit reference counting of json objects will take
significant part of the CPU time for ovsdb-server.  Inlining them
to reduce the cost of a function call.

Acked-by: Mike Pattrick <[email protected]>
Acked-by: Dumitru Ceara <[email protected]>
Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
igsilya committed Nov 30, 2021
1 parent 19aa701 commit 9d29990
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
26 changes: 24 additions & 2 deletions include/openvswitch/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ double json_real(const struct json *);
int64_t json_integer(const struct json *);

struct json *json_deep_clone(const struct json *);
struct json *json_clone(const struct json *);
static inline struct json *json_clone(const struct json *);
struct json *json_nullable_clone(const struct json *);
void json_destroy(struct json *);
static inline void json_destroy(struct json *);

size_t json_hash(const struct json *, size_t basis);
bool json_equal(const struct json *, const struct json *);
Expand Down Expand Up @@ -146,6 +146,28 @@ void json_to_ds(const struct json *, int flags, struct ds *);

bool json_string_unescape(const char *in, size_t in_len, char **outp);
void json_string_escape(const char *in, struct ds *out);

/* Inline functions. */

/* Returns 'json', with the reference count incremented. */
static inline struct json *
json_clone(const struct json *json_)
{
struct json *json = CONST_CAST(struct json *, json_);
json->count++;
return json;
}

void json_destroy__(struct json *json);

/* Frees 'json' and everything it points to, recursively. */
static inline void
json_destroy(struct json *json)
{
if (json && !--json->count) {
json_destroy__(json);
}
}

#ifdef __cplusplus
}
Expand Down
53 changes: 21 additions & 32 deletions lib/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,35 +365,33 @@ static void json_destroy_array(struct json_array *array);

/* Frees 'json' and everything it points to, recursively. */
void
json_destroy(struct json *json)
json_destroy__(struct json *json)
{
if (json && !--json->count) {
switch (json->type) {
case JSON_OBJECT:
json_destroy_object(json->object);
break;
switch (json->type) {
case JSON_OBJECT:
json_destroy_object(json->object);
break;

case JSON_ARRAY:
json_destroy_array(&json->array);
break;
case JSON_ARRAY:
json_destroy_array(&json->array);
break;

case JSON_STRING:
case JSON_SERIALIZED_OBJECT:
free(json->string);
break;
case JSON_STRING:
case JSON_SERIALIZED_OBJECT:
free(json->string);
break;

case JSON_NULL:
case JSON_FALSE:
case JSON_TRUE:
case JSON_INTEGER:
case JSON_REAL:
break;
case JSON_NULL:
case JSON_FALSE:
case JSON_TRUE:
case JSON_INTEGER:
case JSON_REAL:
break;

case JSON_N_TYPES:
OVS_NOT_REACHED();
}
free(json);
case JSON_N_TYPES:
OVS_NOT_REACHED();
}
free(json);
}

static void
Expand Down Expand Up @@ -459,15 +457,6 @@ json_deep_clone(const struct json *json)
}
}

/* Returns 'json', with the reference count incremented. */
struct json *
json_clone(const struct json *json_)
{
struct json *json = CONST_CAST(struct json *, json_);
json->count++;
return json;
}

struct json *
json_nullable_clone(const struct json *json)
{
Expand Down

0 comments on commit 9d29990

Please sign in to comment.