Unverified Commit eef2d830 authored by kmy2001's avatar kmy2001 Committed by GitHub
Browse files

Optimization in t_hash.c: Avoid looking for a same field twice by using...

Optimization in t_hash.c: Avoid looking for a same field twice by using dictAddRaw() instead of dictFind() and dictAdd() (#11110)

Before this change in hashTypeSet() function, we first use dictFind()
to look for the field and if it does not exist, we use dictAdd() to add it.
In dictAdd() function the dictionary will look for the field again and I
think this is meaningless as we already know that the field does not exist.

An optimization is to use dictAddRaw() instead of dictFind() and dictAdd().
If we use dictAddRaw(), a new entry will be added when the field does not
exist, and what we should do then is just set the value of that entry, and set
its key to 'sdsdup(field)' in the case that 'HASH_SET_TAKE_FIELD' flag wasn't set.
parent c5ff163d
......@@ -227,31 +227,27 @@ int hashTypeSet(robj *o, sds field, sds value, int flags) {
if (hashTypeLength(o) > server.hash_max_listpack_entries)
hashTypeConvert(o, OBJ_ENCODING_HT);
} else if (o->encoding == OBJ_ENCODING_HT) {
dictEntry *de = dictFind(o->ptr,field);
if (de) {
sdsfree(dictGetVal(de));
dict *ht = o->ptr;
dictEntry *de, *existing;
sds v;
if (flags & HASH_SET_TAKE_VALUE) {
dictGetVal(de) = value;
v = value;
value = NULL;
} else {
dictGetVal(de) = sdsdup(value);
v = sdsdup(value);
}
update = 1;
} else {
sds f,v;
de = dictAddRaw(ht, field, &existing);
if (de) {
dictSetVal(ht, de, v);
if (flags & HASH_SET_TAKE_FIELD) {
f = field;
field = NULL;
} else {
f = sdsdup(field);
dictSetKey(ht, de, sdsdup(field));
}
if (flags & HASH_SET_TAKE_VALUE) {
v = value;
value = NULL;
} else {
v = sdsdup(value);
}
dictAdd(o->ptr,f,v);
sdsfree(dictGetVal(existing));
dictSetVal(ht, existing, v);
update = 1;
}
} else {
serverPanic("Unknown hash encoding");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment