About _dictNextPower function #1201

xuxiaoliang opened this Issue Jul 18, 2013 · 4 comments


None yet

2 participants


if size >=LONG_MAX,so the returned value doesnt match 2^n, and the trick key%size == key&sizemask????

I think that sizemask dont need exsit. beacaus sizemask = size-1;
typedef struct dictht {
dictEntry **table;
unsigned long size;
unsigned long sizemask;
unsigned long used;
} dictht;

/* Our hash table capability is a power of two */
static unsigned long _dictNextPower(unsigned long size)
unsigned long i = DICT_HT_INITIAL_SIZE;

if (size >= LONG_MAX) return LONG_MAX;
while(1) {
    if (i >= size)
        return i;
    i *= 2;



please help me...

kgcrom commented Jul 18, 2013

I think it's only save operation. ( assembly "sub ")


h & d->ht[table].sizemask
h & d->ht[table].size-1

so ,if size >=LONG_MAX, the value doesnt match 2^n....
the function is correct?

kgcrom commented Jul 19, 2013

so so. if you have a massive memory, LONG_MAX modify ULONG_MAX. ; )

size is one of the 4, 8, 16, 32, ..., 2147483638, because DICT_HT_INITIAL_SIZE is defined 4.
and it's enough to using Redis.

it used about (2G*(sizeof(struct dictEntry))) + (sizeof(struct dictht)) + (sizeof(struct dict)) + sizeof(struct dictType)... 
when used variable is 2^31. 

Above example is only key size. Adding a value size is amazing.

I think 2^31, 2^32... is......needed to sharding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment