diff mbox series

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

Message ID 20211122000932.3041390-2-i.maximets@ovn.org
State Accepted
Headers show
Series ovsdb-data: Consolidate ovsdb atom and json strings. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed

Commit Message

Ilya Maximets Nov. 22, 2021, 12:09 a.m. UTC
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@ovn.org>
---
 include/openvswitch/json.h | 26 +++++++++++++++++--
 lib/json.c                 | 53 +++++++++++++++-----------------------
 2 files changed, 45 insertions(+), 34 deletions(-)

Comments

Dumitru Ceara Nov. 29, 2021, 3:48 p.m. UTC | #1
On 11/22/21 01:09, Ilya Maximets wrote:
> 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@ovn.org>
> ---

Looks good to me, thanks!

Acked-by: Dumitru Ceara <dceara@redhat.com>
diff mbox series

Patch

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)
 {