Performs N steps of incremental rehashing. Returns 1 if there are still * keys to move from the old to the new hash table, otherwise 0 is returned. * * Note that a rehashing step consists in moving a bucket (that may have more * than one key as we use chaining) from the old to the new hash table, however * since part of the hash table may be composed of empty spaces, it is not * guaranteed t
| 209 | * will visit at max N*10 empty buckets in total, otherwise the amount of |
| 210 | * work it does would be unbound and the function may block for a long time. */ |
| 211 | int dictRehash(dict *d, int n) { |
| 212 | int empty_visits = n*10; /* Max number of empty buckets to visit. */ |
| 213 | if (!dictIsRehashing(d)) return 0; |
| 214 | |
| 215 | while(n-- && d->ht[0].used != 0) { |
| 216 | dictEntry *de, *nextde; |
| 217 | |
| 218 | /* Note that rehashidx can't overflow as we are sure there are more |
| 219 | * elements because ht[0].used != 0 */ |
| 220 | assert(d->ht[0].size > (unsigned long)d->rehashidx); |
| 221 | while(d->ht[0].table[d->rehashidx] == NULL) { |
| 222 | d->rehashidx++; |
| 223 | if (--empty_visits == 0) return 1; |
| 224 | } |
| 225 | de = d->ht[0].table[d->rehashidx]; |
| 226 | /* Move all the keys in this bucket from the old to the new hash HT */ |
| 227 | while(de) { |
| 228 | uint64_t h; |
| 229 | |
| 230 | nextde = de->next; |
| 231 | /* Get the index in the new hash table */ |
| 232 | h = dictHashKey(d, de->key) & d->ht[1].sizemask; |
| 233 | de->next = d->ht[1].table[h]; |
| 234 | d->ht[1].table[h] = de; |
| 235 | d->ht[0].used--; |
| 236 | d->ht[1].used++; |
| 237 | de = nextde; |
| 238 | } |
| 239 | d->ht[0].table[d->rehashidx] = NULL; |
| 240 | d->rehashidx++; |
| 241 | } |
| 242 | |
| 243 | /* Check if we already rehashed the whole table... */ |
| 244 | if (d->ht[0].used == 0) { |
| 245 | zfree(d->ht[0].table); |
| 246 | d->ht[0] = d->ht[1]; |
| 247 | _dictReset(&d->ht[1]); |
| 248 | d->rehashidx = -1; |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /* More to rehash... */ |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | long long timeInMilliseconds(void) { |
| 257 | struct timeval tv; |
no test coverage detected