Remove empty DB check branch in KEYS command (#1259)

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 <binloveplay1314@qq.com>
This commit is contained in:
Binbin 2024-11-06 10:32:00 +08:00 committed by GitHub
parent 48ebe21ad1
commit 12c5af03b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 5 deletions

View File

@ -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);

View File

@ -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
@ -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]
}
}