Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Optimize the checking logic for conversion to skiplist in advance. #806

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/t_zset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1204,8 +1204,9 @@ robj *zsetTypeCreate(size_t size_hint, size_t val_len_hint) {

/* Check if the existing zset should be converted to another encoding based off the
* the size hint. */
void zsetTypeMaybeConvert(robj *zobj, size_t size_hint) {
if (zobj->encoding == OBJ_ENCODING_LISTPACK && size_hint > server.zset_max_listpack_entries) {
void zsetTypeMaybeConvert(robj *zobj, size_t size_hint, size_t value_len_hint) {
if (zobj->encoding == OBJ_ENCODING_LISTPACK &&
(size_hint > server.zset_max_listpack_entries || value_len_hint > server.zset_max_listpack_value)) {
zsetConvertAndExpand(zobj, OBJ_ENCODING_SKIPLIST, size_hint);
}
}
Expand Down Expand Up @@ -1720,6 +1721,7 @@ void zaddGenericCommand(client *c, int flags) {
sds ele;
double score = 0, *scores = NULL;
int j, elements, ch = 0;
size_t maxelelen = 0;
int scoreidx = 0;
/* The following vars are used in order to track what the command actually
* did during the execution, to reply to the client and to trigger the
Expand Down Expand Up @@ -1790,17 +1792,20 @@ void zaddGenericCommand(client *c, int flags) {
scores = zmalloc(sizeof(double) * elements);
for (j = 0; j < elements; j++) {
if (getDoubleFromObjectOrReply(c, c->argv[scoreidx + j * 2], &scores[j], NULL) != C_OK) goto cleanup;
ele = c->argv[scoreidx + 1 + j * 2]->ptr;
size_t elelen = sdslen(ele);
if (maxelelen < elelen) maxelelen = elelen;
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved
}

/* Lookup the key and create the sorted set if does not exist. */
zobj = lookupKeyWrite(c->db, key);
if (checkType(c, zobj, OBJ_ZSET)) goto cleanup;
if (zobj == NULL) {
if (xx) goto reply_to_client; /* No key + XX option: nothing to do. */
zobj = zsetTypeCreate(elements, sdslen(c->argv[scoreidx + 1]->ptr));
zobj = zsetTypeCreate(elements, maxelelen);
dbAdd(c->db, key, zobj);
} else {
zsetTypeMaybeConvert(zobj, elements);
zsetTypeMaybeConvert(zobj, elements, maxelelen);
}

for (j = 0; j < elements; j++) {
Expand Down
Loading