]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Reduce noise in JSON code by marking up constant keys as constant
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 6 Apr 2021 21:02:51 +0000 (22:02 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 7 Apr 2021 00:02:18 +0000 (01:02 +0100)
We also get a performance boost as they're not strdupd

src/lib/json/json.c
src/modules/rlm_couchbase/mod.c
src/modules/rlm_couchbase/rlm_couchbase.c

index b244fa447c516a0dee3e619867961261ad63b122..8128d0033164f09a9a1971d6990a4e6c10b41f91 100644 (file)
@@ -583,21 +583,21 @@ static json_object *json_object_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_list_t
                         */
                        MEM(type_name = json_object_new_string(fr_table_str_by_value(fr_value_box_type_table,
                                                                                     vp->vp_type, "<INVALID>")));
-                       json_object_object_add(vp_object, "type", type_name);
+                       json_object_object_add_ex(vp_object, "type", type_name, JSON_C_OBJECT_KEY_IS_CONSTANT);
 
                        /*
                         *      Create a "value" array to hold any attribute values for this attribute...
                         */
                        if (format->value.value_as_array) {
                                MEM(values = json_object_new_array());
-                               json_object_object_add(vp_object, "value", values);
+                               json_object_object_add_ex(vp_object, "value", values, JSON_C_OBJECT_KEY_IS_CONSTANT);
                        } else {
                                /*
                                 *      ...unless this is the first time we've seen the attribute and
                                 *      value_as_array is false, in which case just add the value directly
                                 *      and move on to the next attribute.
                                 */
-                               json_object_object_add(vp_object, "value", value);
+                               json_object_object_add_ex(vp_object, "value", value, JSON_C_OBJECT_KEY_IS_CONSTANT);
                                continue;
                        }
                } else {
@@ -625,7 +625,8 @@ static json_object *json_object_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_list_t
                                        MEM(values = json_object_new_array());
                                        json_object_array_add(values, json_object_get(convert_value));
                                        json_object_object_del(vp_object, "value");
-                                       json_object_object_add(vp_object, "value", values);
+                                       json_object_object_add_ex(vp_object, "value", values,
+                                                                 JSON_C_OBJECT_KEY_IS_CONSTANT);
                                }
                        }
                }
@@ -721,7 +722,11 @@ static json_object *json_smplobj_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_list_t
                                 */
                                MEM(values = json_object_new_array());
                                json_object_array_add(values, json_object_get(vp_object));
-                               json_object_object_del(obj, attr_name);
+
+                               /*
+                                *      Existing key will have refcount decremented
+                                *      and will be freed if thise drops to zero.
+                                */
                                json_object_object_add(obj, attr_name, values);
                        }
                }
@@ -824,11 +829,11 @@ static struct json_object *json_array_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_l
                         * Add the attribute name in the "name" key and the type in the "type" key
                         */
                        MEM(name = json_object_new_string(attr_name));
-                       json_object_object_add(attrobj, "name", name);
+                       json_object_object_add_ex(attrobj, "name", name, JSON_C_OBJECT_KEY_IS_CONSTANT);
 
                        MEM(type_name = json_object_new_string(fr_table_str_by_value(fr_value_box_type_table,
                                                                                     vp->vp_type, "<INVALID>")));
-                       json_object_object_add(attrobj, "type", type_name);
+                       json_object_object_add_ex(attrobj, "type", type_name, JSON_C_OBJECT_KEY_IS_CONSTANT);
                }
 
                if (format->value.value_as_array) {
@@ -841,7 +846,7 @@ static struct json_object *json_array_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_l
                                /*
                                 * Add "value":[] key to the attribute object
                                 */
-                               json_object_object_add(attrobj, "value", values);
+                               json_object_object_add_ex(attrobj, "value", values, JSON_C_OBJECT_KEY_IS_CONSTANT);
 
                                /*
                                 * Also add to "seen_attributes" to check later
@@ -857,7 +862,7 @@ static struct json_object *json_array_afrom_pair_list(TALLOC_CTX *ctx, fr_pair_l
                        /*
                         * This is simpler; just add a "value": key to the attribute object.
                         */
-                       json_object_object_add(attrobj, "value", value);
+                       json_object_object_add_ex(attrobj, "value", value, JSON_C_OBJECT_KEY_IS_CONSTANT);
                }
 
        }
index beab3a4148e96501b49caf0a26296f9f76180145..8662bd30dff366a3b5a3039b31b7822c022373f6 100644 (file)
@@ -651,7 +651,8 @@ int mod_ensure_start_timestamp(json_object *json, fr_pair_list_t *vps)
                        /* debugging */
                        DEBUG("calculated start timestamp: %s", value);
                        /* store new value in json body */
-                       json_object_object_add(json, "startTimestamp", json_object_new_string(value));
+                       json_object_object_add_ex(json, "startTimestamp", json_object_new_string(value),
+                                                 JSON_C_OBJECT_KEY_IS_CONSTANT);
                } else {
                        /* debugging */
                        DEBUG("failed to format calculated timestamp");
index 35d992502d5215470121d6a23889ddb31ed98ab5..ccd0311d2b3e248dd2df9597bd00391f0de5ccdf 100644 (file)
@@ -323,10 +323,11 @@ static unlang_action_t mod_accounting(rlm_rcode_t *p_result, module_ctx_t const
                /* create new json object */
                cookie->jobj = json_object_new_object();
                /* set 'docType' element for new document */
-               json_object_object_add(cookie->jobj, "docType", json_object_new_string(inst->doctype));
+               json_object_object_add_ex(cookie->jobj, "docType", json_object_new_string(inst->doctype),
+                                         JSON_C_OBJECT_KEY_IS_CONSTANT);
                /* default startTimestamp and stopTimestamp to null values */
-               json_object_object_add(cookie->jobj, "startTimestamp", NULL);
-               json_object_object_add(cookie->jobj, "stopTimestamp", NULL);
+               json_object_object_add_ex(cookie->jobj, "startTimestamp", NULL, JSON_C_OBJECT_KEY_IS_CONSTANT);
+               json_object_object_add_ex(cookie->jobj, "stopTimestamp", NULL, JSON_C_OBJECT_KEY_IS_CONSTANT);
        }
 
        /* status specific replacements for start/stop time */
@@ -335,8 +336,9 @@ static unlang_action_t mod_accounting(rlm_rcode_t *p_result, module_ctx_t const
                /* add start time */
                if ((vp = fr_pair_find_by_da(&request->request_pairs, attr_acct_status_type)) != NULL) {
                        /* add to json object */
-                       json_object_object_add(cookie->jobj, "startTimestamp",
-                                              mod_value_pair_to_json_object(request, vp));
+                       json_object_object_add_ex(cookie->jobj, "startTimestamp",
+                                                 mod_value_pair_to_json_object(request, vp),
+                                                 JSON_C_OBJECT_KEY_IS_CONSTANT);
                }
                break;
 
@@ -344,8 +346,9 @@ static unlang_action_t mod_accounting(rlm_rcode_t *p_result, module_ctx_t const
                /* add stop time */
                if ((vp = fr_pair_find_by_da(&request->request_pairs, attr_event_timestamp)) != NULL) {
                        /* add to json object */
-                       json_object_object_add(cookie->jobj, "stopTimestamp",
-                                              mod_value_pair_to_json_object(request, vp));
+                       json_object_object_add_ex(cookie->jobj, "stopTimestamp",
+                                                 mod_value_pair_to_json_object(request, vp),
+                                                 JSON_C_OBJECT_KEY_IS_CONSTANT);
                }
                /* check start timestamp and adjust if needed */
                mod_ensure_start_timestamp(cookie->jobj, &request->request_pairs);