[ovs-dev] [PATCH 1/2] json: Inline clone and destroy functions.

Ilya Maximets i.maximets at ovn.org
Mon Nov 22 00:09:31 UTC 2021


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.

Signed-off-by: Ilya Maximets <i.maximets at ovn.org>
---
 include/openvswitch/json.h | 26 +++++++++++++++++--
 lib/json.c                 | 53 +++++++++++++++-----------------------
 2 files changed, 45 insertions(+), 34 deletions(-)

diff --git a/include/openvswitch/json.h b/include/openvswitch/json.h
index 0831a9cee..35b403c29 100644
--- a/include/openvswitch/json.h
+++ b/include/openvswitch/json.h
@@ -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 *);
@@ -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
 }
diff --git a/lib/json.c b/lib/json.c
index 0baf7c622..720c73d94 100644
--- a/lib/json.c
+++ b/lib/json.c
@@ -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
@@ -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)
 {
-- 
2.31.1



More information about the dev mailing list