Avoid unnecessary decoding in ziplist.c

Closes #1519
This commit is contained in:
Xiaojie Zhang 2014-01-22 21:23:54 +08:00 committed by antirez
parent 7e9f24d694
commit 4bb6844e43

View File

@ -576,19 +576,19 @@ static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsig
/* Insert item at "p". */ /* Insert item at "p". */
static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) { static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen, prevlen = 0; size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen;
unsigned int prevlensize, prevlen = 0;
size_t offset; size_t offset;
int nextdiff = 0; int nextdiff = 0;
unsigned char encoding = 0; unsigned char encoding = 0;
long long value = 123456789; /* initialized to avoid warning. Using a value long long value = 123456789; /* initialized to avoid warning. Using a value
that is easy to see if for some reason that is easy to see if for some reason
we use it uninitialized. */ we use it uninitialized. */
zlentry entry, tail; zlentry tail;
/* Find out prevlen for the entry that is inserted. */ /* Find out prevlen for the entry that is inserted. */
if (p[0] != ZIP_END) { if (p[0] != ZIP_END) {
entry = zipEntry(p); ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
prevlen = entry.prevrawlen;
} else { } else {
unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl); unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
if (ptail[0] != ZIP_END) { if (ptail[0] != ZIP_END) {
@ -676,15 +676,15 @@ unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int sle
* doesn't contain an element at the provided index, NULL is returned. */ * doesn't contain an element at the provided index, NULL is returned. */
unsigned char *ziplistIndex(unsigned char *zl, int index) { unsigned char *ziplistIndex(unsigned char *zl, int index) {
unsigned char *p; unsigned char *p;
zlentry entry; unsigned int prevlensize, prevlen = 0;
if (index < 0) { if (index < 0) {
index = (-index)-1; index = (-index)-1;
p = ZIPLIST_ENTRY_TAIL(zl); p = ZIPLIST_ENTRY_TAIL(zl);
if (p[0] != ZIP_END) { if (p[0] != ZIP_END) {
entry = zipEntry(p); ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
while (entry.prevrawlen > 0 && index--) { while (prevlen > 0 && index--) {
p -= entry.prevrawlen; p -= prevlen;
entry = zipEntry(p); ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
} }
} }
} else { } else {
@ -722,7 +722,7 @@ unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
/* Return pointer to previous entry in ziplist. */ /* Return pointer to previous entry in ziplist. */
unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) { unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
zlentry entry; unsigned int prevlensize, prevlen = 0;
/* Iterating backwards from ZIP_END should return the tail. When "p" is /* Iterating backwards from ZIP_END should return the tail. When "p" is
* equal to the first element of the list, we're already at the head, * equal to the first element of the list, we're already at the head,
@ -733,9 +733,9 @@ unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
} else if (p == ZIPLIST_ENTRY_HEAD(zl)) { } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
return NULL; return NULL;
} else { } else {
entry = zipEntry(p); ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
assert(entry.prevrawlen > 0); assert(prevlen > 0);
return p-entry.prevrawlen; return p-prevlen;
} }
} }