Unverified Commit 828ae842 authored by ZheNing Hu's avatar ZheNing Hu Committed by GitHub
Browse files

Improve ziplistRandomPairs code logic (#8851)



After sorting, each item in picks is sorted according
to its index.

In the original code logic, we traverse from the first
element of ziplist until `zipindex == picks[pickindex].index`.

We may be able to start directly in `picks[0].index`,
this will bring small performance improvement.
Signed-off-by: default avatarZheNing Hu <adlternative@gmail.com>
parent f29a0b93
...@@ -1601,8 +1601,8 @@ void ziplistRandomPairs(unsigned char *zl, unsigned int count, ziplistEntry *key ...@@ -1601,8 +1601,8 @@ void ziplistRandomPairs(unsigned char *zl, unsigned int count, ziplistEntry *key
qsort(picks, count, sizeof(rand_pick), uintCompare); qsort(picks, count, sizeof(rand_pick), uintCompare);
/* fetch the elements form the ziplist into a output array respecting the original order. */ /* fetch the elements form the ziplist into a output array respecting the original order. */
unsigned int zipindex = 0, pickindex = 0; unsigned int zipindex = picks[0].index, pickindex = 0;
p = ziplistIndex(zl, 0); p = ziplistIndex(zl, zipindex);
while (ziplistGet(p, &key, &klen, &klval) && pickindex < count) { while (ziplistGet(p, &key, &klen, &klval) && pickindex < count) {
p = ziplistNext(zl, p); p = ziplistNext(zl, p);
assert(ziplistGet(p, &value, &vlen, &vlval)); assert(ziplistGet(p, &value, &vlen, &vlval));
......
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