Add new faster dictionary merging for use by snapshotting code

Former-commit-id: b6f120b3d401c92ef5cf1cc6f5e77da139e33a97
This commit is contained in:
John Sully 2020-02-01 20:17:40 -05:00
parent bb2d27661e
commit d8dfc76673
2 changed files with 39 additions and 0 deletions

View File

@ -176,6 +176,44 @@ int dictExpand(dict *d, unsigned long size)
return DICT_OK;
}
int dictMerge(dict *dst, dict *src)
{
if (dictSize(src) == 0)
return DICT_OK;
if (!dictIsRehashing(dst) && !dictIsRehashing(src))
{
dst->ht[1] = dst->ht[0];
dst->ht[0] = src->ht[0];
dst->rehashidx = 0;
_dictReset(&src->ht[0]);
return DICT_OK;
}
auto &htDst = dictIsRehashing(dst) ? dst->ht[1] : dst->ht[0];
for (int iht = 0; iht < 2; ++iht)
{
for (size_t ide = 0; ide < src->ht[iht].size; ++ide)
{
if (src->ht[iht].used == 0)
break;
dictEntry *de = src->ht[iht].table[ide];
src->ht[iht].table[ide] = nullptr;
while (de != nullptr)
{
dictEntry *deNext = de->next;
uint64_t h = dictHashKey(dst, de->key) & htDst.sizemask;
de->next = htDst.table[h];
htDst.table[h] = de;
htDst.used++;
de = deNext;
src->ht[iht].used--;
}
}
}
return DICT_OK;
}
/* 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.
*

View File

@ -190,6 +190,7 @@ unsigned long dictScan(dict *d, unsigned long v, dictScanFunction *fn, dictScanB
uint64_t dictGetHash(dict *d, const void *key);
dictEntry **dictFindEntryRefByPtrAndHash(dict *d, const void *oldptr, uint64_t hash);
void dictForceRehash(dict *d);
int dictMerge(dict *dst, dict *src);
/* Hash table types */
extern dictType dictTypeHeapStringCopyKey;