when i read ziplist.c code memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff); if nextdiff is 4 in this case the pre entry data was broke? please explain it is a bug? Thx!!!
Comment From: sundb
do you mean that the next lenbytelen of previous entry goes from 5 to 1 byte? please refer to the code: this will update the pre-entry data.
if (nextdiff != 0) {
offset = p-zl;
zl = __ziplistCascadeUpdate(zl,p+reqlen);
p = zl+offset;
}
Comment From: sundb
This is not a bug, for example, the prevlensize is 5 before insertion, but after inserting a very small element, we only need 1 byte to store its length, and we need to utilize the remaining 4 bytes.
Comment From: kyangcode
int forcelarge = 0;
nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
if (nextdiff == -4 && reqlen < 4) {
nextdiff = 0;
forcelarge = 1;
}
When nextdiff is equal to -4, it means the previous prevlensize was 5. In this case, reqlen must be greater than 5 because the element to be inserted will certainly require 5 bytes to represent the size of the previous element. Therefore, this if condition will never be satisfied.
Comment From: liqinliang
memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff); "I misunderstood 'copy' as 'cut'. I thought the preceding node was damaged." if ,p-nextdiff change to p "The same effect "Can I understand it this way?"
Comment From: sundb
@liqinliang Not really, because we want to make sure that after the p element is moved, there is enough space in its prevlen to store it, p-nextdiff which you can consider as adding enough space or remove unnecessary space to the front of p and move it.
Comment From: sundb
@kyangcode please see this commit and its commit msg: https://github.com/redis/redis/commit/c495d095ae495ea5253443ee4562aaa30681a854
Comment From: kyangcode
@sundb Thx.
Comment From: liqinliang
memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
l only in the case nextdiff=4 l am thinking the memmove funtion param src p-nextdiff or p the same effect .because move lenth both curlen-offset-1+nextdiff. p-nextdiff only 4bytes value into p+reqlen start point The following code will overwrite the value. after resize have enough space.
That was just my simple understanding. Thank you.