mirror of
https://github.com/fluencelabs/redis
synced 2025-03-19 09:00:51 +00:00
merged memory reduction patch
This commit is contained in:
commit
a50ea45c84
45
redis.c
45
redis.c
@ -4844,7 +4844,8 @@ static zskiplistNode *zslCreateNode(int level, double score, robj *obj) {
|
|||||||
zskiplistNode *zn = zmalloc(sizeof(*zn));
|
zskiplistNode *zn = zmalloc(sizeof(*zn));
|
||||||
|
|
||||||
zn->forward = zmalloc(sizeof(zskiplistNode*) * level);
|
zn->forward = zmalloc(sizeof(zskiplistNode*) * level);
|
||||||
zn->span = zmalloc(sizeof(unsigned int) * level);
|
if (level > 0)
|
||||||
|
zn->span = zmalloc(sizeof(unsigned int) * (level - 1));
|
||||||
zn->score = score;
|
zn->score = score;
|
||||||
zn->obj = obj;
|
zn->obj = obj;
|
||||||
return zn;
|
return zn;
|
||||||
@ -4897,19 +4898,19 @@ static int zslRandomLevel(void) {
|
|||||||
|
|
||||||
static void zslInsert(zskiplist *zsl, double score, robj *obj) {
|
static void zslInsert(zskiplist *zsl, double score, robj *obj) {
|
||||||
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
|
zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
|
||||||
unsigned int span[ZSKIPLIST_MAXLEVEL];
|
unsigned int rank[ZSKIPLIST_MAXLEVEL];
|
||||||
int i, level;
|
int i, level;
|
||||||
|
|
||||||
x = zsl->header;
|
x = zsl->header;
|
||||||
for (i = zsl->level-1; i >= 0; i--) {
|
for (i = zsl->level-1; i >= 0; i--) {
|
||||||
/* store span that is crossed to reach the insert position */
|
/* store rank that is crossed to reach the insert position */
|
||||||
span[i] = i == (zsl->level-1) ? 0 : span[i+1];
|
rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];
|
||||||
|
|
||||||
while (x->forward[i] &&
|
while (x->forward[i] &&
|
||||||
(x->forward[i]->score < score ||
|
(x->forward[i]->score < score ||
|
||||||
(x->forward[i]->score == score &&
|
(x->forward[i]->score == score &&
|
||||||
compareStringObjects(x->forward[i]->obj,obj) < 0))) {
|
compareStringObjects(x->forward[i]->obj,obj) < 0))) {
|
||||||
span[i] += x->span[i];
|
rank[i] += i > 0 ? x->span[i-1] : 1;
|
||||||
x = x->forward[i];
|
x = x->forward[i];
|
||||||
}
|
}
|
||||||
update[i] = x;
|
update[i] = x;
|
||||||
@ -4921,9 +4922,9 @@ static void zslInsert(zskiplist *zsl, double score, robj *obj) {
|
|||||||
level = zslRandomLevel();
|
level = zslRandomLevel();
|
||||||
if (level > zsl->level) {
|
if (level > zsl->level) {
|
||||||
for (i = zsl->level; i < level; i++) {
|
for (i = zsl->level; i < level; i++) {
|
||||||
span[i] = 0;
|
rank[i] = 0;
|
||||||
update[i] = zsl->header;
|
update[i] = zsl->header;
|
||||||
update[i]->span[i] = zsl->length;
|
update[i]->span[i-1] = zsl->length;
|
||||||
}
|
}
|
||||||
zsl->level = level;
|
zsl->level = level;
|
||||||
}
|
}
|
||||||
@ -4933,13 +4934,15 @@ static void zslInsert(zskiplist *zsl, double score, robj *obj) {
|
|||||||
update[i]->forward[i] = x;
|
update[i]->forward[i] = x;
|
||||||
|
|
||||||
/* update span covered by update[i] as x is inserted here */
|
/* update span covered by update[i] as x is inserted here */
|
||||||
x->span[i] = update[i]->span[i] - (span[0] - span[i]);
|
if (i > 0) {
|
||||||
update[i]->span[i] = (span[0] - span[i]) + 1;
|
x->span[i-1] = update[i]->span[i-1] - (rank[0] - rank[i]);
|
||||||
|
update[i]->span[i-1] = (rank[0] - rank[i]) + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* increment span for untouched levels */
|
/* increment span for untouched levels */
|
||||||
for (i = level; i < zsl->level; i++) {
|
for (i = level; i < zsl->level; i++) {
|
||||||
update[i]->span[i]++;
|
update[i]->span[i-1]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
x->backward = (update[0] == zsl->header) ? NULL : update[0];
|
x->backward = (update[0] == zsl->header) ? NULL : update[0];
|
||||||
@ -4970,10 +4973,14 @@ static int zslDelete(zskiplist *zsl, double score, robj *obj) {
|
|||||||
if (x && score == x->score && compareStringObjects(x->obj,obj) == 0) {
|
if (x && score == x->score && compareStringObjects(x->obj,obj) == 0) {
|
||||||
for (i = 0; i < zsl->level; i++) {
|
for (i = 0; i < zsl->level; i++) {
|
||||||
if (update[i]->forward[i] == x) {
|
if (update[i]->forward[i] == x) {
|
||||||
update[i]->span[i] += x->span[i] - 1;
|
if (i > 0) {
|
||||||
|
update[i]->span[i-1] += x->span[i-1] - 1;
|
||||||
|
}
|
||||||
update[i]->forward[i] = x->forward[i];
|
update[i]->forward[i] = x->forward[i];
|
||||||
} else {
|
} else {
|
||||||
update[i]->span[i] -= 1;
|
/* invariant: i > 0, because update[0]->forward[0]
|
||||||
|
* is always equal to x */
|
||||||
|
update[i]->span[i-1] -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (x->forward[0]) {
|
if (x->forward[0]) {
|
||||||
@ -5015,10 +5022,14 @@ static unsigned long zslDeleteRange(zskiplist *zsl, double min, double max, dict
|
|||||||
|
|
||||||
for (i = 0; i < zsl->level; i++) {
|
for (i = 0; i < zsl->level; i++) {
|
||||||
if (update[i]->forward[i] == x) {
|
if (update[i]->forward[i] == x) {
|
||||||
update[i]->span[i] += x->span[i] - 1;
|
if (i > 0) {
|
||||||
|
update[i]->span[i-1] += x->span[i-1] - 1;
|
||||||
|
}
|
||||||
update[i]->forward[i] = x->forward[i];
|
update[i]->forward[i] = x->forward[i];
|
||||||
} else {
|
} else {
|
||||||
update[i]->span[i] -= 1;
|
/* invariant: i > 0, because update[0]->forward[0]
|
||||||
|
* is always equal to x */
|
||||||
|
update[i]->span[i-1] -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (x->forward[0]) {
|
if (x->forward[0]) {
|
||||||
@ -5069,7 +5080,7 @@ static unsigned long zslGetRank(zskiplist *zsl, double score, robj *o) {
|
|||||||
(x->forward[i]->score < score ||
|
(x->forward[i]->score < score ||
|
||||||
(x->forward[i]->score == score &&
|
(x->forward[i]->score == score &&
|
||||||
compareStringObjects(x->forward[i]->obj,o) <= 0))) {
|
compareStringObjects(x->forward[i]->obj,o) <= 0))) {
|
||||||
rank += x->span[i];
|
rank += i > 0 ? x->span[i-1] : 1;
|
||||||
x = x->forward[i];
|
x = x->forward[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5089,8 +5100,8 @@ zskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank) {
|
|||||||
|
|
||||||
x = zsl->header;
|
x = zsl->header;
|
||||||
for (i = zsl->level-1; i >= 0; i--) {
|
for (i = zsl->level-1; i >= 0; i--) {
|
||||||
while (x->forward[i] && (traversed + x->span[i]) <= rank) {
|
while (x->forward[i] && (traversed + (i > 0 ? x->span[i-1] : 1)) <= rank) {
|
||||||
traversed += x->span[i];
|
traversed += i > 0 ? x->span[i-1] : 1;
|
||||||
x = x->forward[i];
|
x = x->forward[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user