Skip to content

Commit 663dac9

Browse files
authored
Prevent calling lookupkey twice in some module functions that use setkey (valkey-io#2365)
In `VM_StringSet()`, `VM_StringTruncate()`, and `VM_ModuleTypeSetValue()` we could assert the key doesnt exists, so we could set SETKEY_DOESNT_EXIST flags to reduce one call to lookupkey. Signed-off-by: wei.kukey <wei.kukey@gmail.com>
1 parent e6ea0b3 commit 663dac9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/module.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4391,7 +4391,7 @@ int VM_StringSet(ValkeyModuleKey *key, ValkeyModuleString *str) {
43914391
VM_DeleteKey(key);
43924392
/* Retain str so setKey copies it to db rather than reallocating it. */
43934393
incrRefCount(str);
4394-
setKey(key->ctx->client, key->db, key->key, &str, SETKEY_NO_SIGNAL);
4394+
setKey(key->ctx->client, key->db, key->key, &str, SETKEY_NO_SIGNAL | SETKEY_DOESNT_EXIST);
43954395
key->value = str;
43964396
return VALKEYMODULE_OK;
43974397
}
@@ -4471,7 +4471,7 @@ int VM_StringTruncate(ValkeyModuleKey *key, size_t newlen) {
44714471
if (key->value == NULL) {
44724472
/* Empty key: create it with the new size. */
44734473
robj *o = createObject(OBJ_STRING, sdsnewlen(NULL, newlen));
4474-
setKey(key->ctx->client, key->db, key->key, &o, SETKEY_NO_SIGNAL);
4474+
setKey(key->ctx->client, key->db, key->key, &o, SETKEY_NO_SIGNAL | SETKEY_DOESNT_EXIST);
44754475
key->value = o;
44764476
} else {
44774477
/* Unshare and resize. */
@@ -7038,7 +7038,7 @@ int VM_ModuleTypeSetValue(ValkeyModuleKey *key, moduleType *mt, void *value) {
70387038
if (!(key->mode & VALKEYMODULE_WRITE) || key->iter) return VALKEYMODULE_ERR;
70397039
VM_DeleteKey(key);
70407040
robj *o = createModuleObject(mt, value);
7041-
setKey(key->ctx->client, key->db, key->key, &o, SETKEY_NO_SIGNAL);
7041+
setKey(key->ctx->client, key->db, key->key, &o, SETKEY_NO_SIGNAL | SETKEY_DOESNT_EXIST);
70427042
key->value = o;
70437043
return VALKEYMODULE_OK;
70447044
}

0 commit comments

Comments
 (0)