H
Haomai Wang
In the implement of ziplist in redis, author use a way to reduce space to store different size integer.
Such as the integer in the range of -8388608~8388607, ziplist use 3 bytes to store it avoid 4bytes.
The function is below:
static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
....
} else if (encoding == ZIP_INT_24B) {
i32 = value<<8;
memrev32ifbe(&i32);
memcpy(p,((uint8_t*)&i32)+1,sizeof(i32)-sizeof(uint8_t));
}...
`p` is the memory stored to, `value` is the integer will be encoded, `encoding` is the encoding way to use determining the size 1 byte, 2bytes, 3bytes or 4bytes etc.
Here we can find if encode integer in 3 bytes, firstly right shift 8 bits. I can't understand why?
The page below may help understand the problem:
https://github.com/antirez/redis/issues/469
This issue shows author concerns the two-complement representation so right shift 8 bits.
Such as the integer in the range of -8388608~8388607, ziplist use 3 bytes to store it avoid 4bytes.
The function is below:
static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
....
} else if (encoding == ZIP_INT_24B) {
i32 = value<<8;
memrev32ifbe(&i32);
memcpy(p,((uint8_t*)&i32)+1,sizeof(i32)-sizeof(uint8_t));
}...
`p` is the memory stored to, `value` is the integer will be encoded, `encoding` is the encoding way to use determining the size 1 byte, 2bytes, 3bytes or 4bytes etc.
Here we can find if encode integer in 3 bytes, firstly right shift 8 bits. I can't understand why?
The page below may help understand the problem:
https://github.com/antirez/redis/issues/469
This issue shows author concerns the two-complement representation so right shift 8 bits.