Expand the hash table if needed */
| 1346 | |
| 1347 | /* Expand the hash table if needed */ |
| 1348 | static int _dictExpandIfNeeded(dict *d) |
| 1349 | { |
| 1350 | static const size_t SHRINK_FACTOR = 4; |
| 1351 | /* Incremental rehashing already in progress. Return. */ |
| 1352 | if (dictIsRehashing(d)) return DICT_OK; |
| 1353 | |
| 1354 | /* If the hash table is empty expand it to the initial size. */ |
| 1355 | if (d->ht[0].size == 0) return _dictExpand(d, DICT_HT_INITIAL_SIZE, false /*fShrink*/, nullptr); |
| 1356 | |
| 1357 | /* If we reached the 1:1 ratio, and we are allowed to resize the hash |
| 1358 | * table (global setting) or we should avoid it but the ratio between |
| 1359 | * elements/buckets is over the "safe" threshold, we resize doubling |
| 1360 | * the number of buckets. */ |
| 1361 | if (d->ht[0].used >= d->ht[0].size && |
| 1362 | (dict_can_resize || |
| 1363 | d->ht[0].used/d->ht[0].size > dict_force_resize_ratio) && |
| 1364 | dictTypeExpandAllowed(d)) |
| 1365 | { |
| 1366 | return _dictExpand(d, d->ht[0].used + 1, false /*fShrink*/, nullptr); |
| 1367 | } |
| 1368 | else if (d->ht[0].used > 0 && d->ht[0].size >= (1024*SHRINK_FACTOR) && (d->ht[0].used * 16) < d->ht[0].size && dict_can_resize && !d->noshrink) |
| 1369 | { |
| 1370 | // If the dictionary has shurnk a lot we'll need to shrink the hash table instead |
| 1371 | return _dictExpand(d, d->ht[0].size/SHRINK_FACTOR, true /*fShrink*/, nullptr); |
| 1372 | } |
| 1373 | return DICT_OK; |
| 1374 | } |
| 1375 | |
| 1376 | /* Our hash table capability is a power of two */ |
| 1377 | static unsigned long _dictNextPower(unsigned long size) |
no test coverage detected