Skip to content

Commit

Permalink
Remove empty DB check branch in KEYS command (valkey-io#1259)
Browse files Browse the repository at this point in the history
We don't think we really care about optimizing for the empty DB case,
which should be uncommon. Adding branches hurts branch prediction.

Signed-off-by: Binbin <[email protected]>
  • Loading branch information
enjoy-binbin authored Nov 6, 2024
1 parent 48ebe21 commit 12c5af0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,11 +830,6 @@ void keysCommand(client *c) {
kvstoreDictIterator *kvs_di = NULL;
kvstoreIterator *kvs_it = NULL;
if (pslot != -1) {
if (!kvstoreDictSize(c->db->keys, pslot)) {
/* Requested slot is empty */
setDeferredArrayLen(c, replylen, 0);
return;
}
kvs_di = kvstoreGetDictSafeIterator(c->db->keys, pslot);
} else {
kvs_it = kvstoreIteratorInit(c->db->keys);
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/keyspace.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ start_server {tags {"keyspace"}} {
r dbsize
} {0}

test {KEYS with empty DB} {
assert_equal {} [r keys *]
}

test "DEL against expired key" {
r debug set-active-expire 0
r setex keyExpire 1 valExpire
Expand Down Expand Up @@ -554,3 +558,14 @@ foreach {type large} [array get largevalue] {
r KEYS [string repeat "*?" 50000]
} {}
}

start_cluster 1 0 {tags {"keyspace external:skip cluster"}} {
test {KEYS with empty DB in cluster mode} {
assert_equal {} [r keys *]
assert_equal {} [r keys foo*]
}

test {KEYS with empty slot in cluster mode} {
assert_equal {} [r keys foo]
}
}

0 comments on commit 12c5af0

Please sign in to comment.