reduce duplicate hash computation in json_object_object_add()

This can be a very considerable performance saver.
This commit is contained in:
Rainer Gerhards
2015-09-22 19:07:30 +02:00
parent ec4879ac5b
commit d8e44dc685
3 changed files with 60 additions and 9 deletions

View File

@@ -403,10 +403,11 @@ void json_object_object_add(struct json_object* jso, const char *key,
// and re-adding it, so the existing key remains valid.
json_object *existing_value = NULL;
struct lh_entry *existing_entry;
existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key);
const unsigned long hash = lh_get_hash(jso->o.c_object, (void*)key);
existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object, (void*)key, hash);
if (!existing_entry)
{
lh_table_insert(jso->o.c_object, strdup(key), val);
lh_table_insert_w_hash(jso->o.c_object, strdup(key), val, hash);
return;
}
existing_value = (void *)existing_entry->v;
@@ -415,6 +416,7 @@ void json_object_object_add(struct json_object* jso, const char *key,
existing_entry->v = val;
}
int json_object_object_length(struct json_object *jso)
{
return lh_table_length(jso->o.c_object);