From bdda941585406c6dd319dabbc8c83f169781b1c8 Mon Sep 17 00:00:00 2001 From: Yifan Hao Date: Wed, 24 Nov 2021 12:25:07 -0800 Subject: [PATCH] Hash table cleanup [8/n] Simplify implementation of taosHashTableResize. Remove unnecessary while loop. * Testing sample mysql run taos> create database db; Query OK, 0 of 0 row(s) in database (0.002273s) taos> use db; Query OK, 0 of 0 row(s) in database (0.000214s) taos> create table t (ts timestamp, a int); Query OK, 0 of 0 row(s) in database (0.003265s) taos> insert into t values ('2019-07-15 00:00:00', 1); Query OK, 1 of 1 row(s) in database (0.001309s) taos> insert into t values ('2019-07-15 01:00:00', 2); Query OK, 1 of 1 row(s) in database (0.000238s) taos> select * from t; ts | a | ======================================== 2019-07-15 00:00:00.000 | 1 | 2019-07-15 01:00:00.000 | 2 | Query OK, 2 row(s) in set (0.000751s) taos> drop database db; Query OK, 0 of 0 row(s) in database (0.002543s) --- src/util/src/hash.c | 70 ++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 51 deletions(-) diff --git a/src/util/src/hash.c b/src/util/src/hash.c index 9aa33b0999..1dfc411d74 100644 --- a/src/util/src/hash.c +++ b/src/util/src/hash.c @@ -677,10 +677,6 @@ void taosHashTableResize(SHashObj *pHashObj) { return; } - // double the original capacity - SHashNode *pNode = NULL; - SHashNode *pNext = NULL; - int32_t newCapacity = (int32_t)(pHashObj->capacity << 1u); if (newCapacity > HASH_MAX_CAPACITY) { uDebug("current capacity:%lu, maximum capacity:%d, no resize applied due to limitation is reached", @@ -707,73 +703,45 @@ void taosHashTableResize(SHashObj *pHashObj) { taosArrayPush(pHashObj->pMemBlock, &p); pHashObj->capacity = newCapacity; - for (int32_t i = 0; i < pHashObj->capacity; ++i) { - SHashEntry *pe = pHashObj->hashList[i]; + for (int32_t idx = 0; idx < pHashObj->capacity; ++idx) { + SHashEntry *pe = pHashObj->hashList[idx]; + SHashNode *pNode; + SHashNode *pNext; + SHashNode *pPrev = NULL; if (pe->num == 0) { assert(pe->next == NULL); continue; } - assert(pe->next != NULL); + pNode = pe->next; - while ((pNode = pe->next) != NULL) { - int32_t j = HASH_INDEX(pNode->hashVal, pHashObj->capacity); - if (j != i) { - pe->num -= 1; - pe->next = pNode->next; + assert(pNode != NULL); - if (pe->num == 0) { - assert(pe->next == NULL); + while (pNode != NULL) { + int32_t newIdx = HASH_INDEX(pNode->hashVal, pHashObj->capacity); + pNext = pNode->next; + if (newIdx != idx) { + pe->num -= 1; + if (pPrev == NULL) { + pe->next = pNext; } else { - assert(pe->next != NULL); + pPrev->next = pNext; } - SHashEntry *pNewEntry = pHashObj->hashList[j]; + SHashEntry *pNewEntry = pHashObj->hashList[newIdx]; pushfrontNodeInEntryList(pNewEntry, pNode); } else { - break; - } - } - - if (pNode != NULL) { - while ((pNext = pNode->next) != NULL) { - int32_t j = HASH_INDEX(pNext->hashVal, pHashObj->capacity); - if (j != i) { - pe->num -= 1; - - pNode->next = pNext->next; - pNext->next = NULL; - - // added into new slot - SHashEntry *pNewEntry = pHashObj->hashList[j]; - - if (pNewEntry->num == 0) { - assert(pNewEntry->next == NULL); - } else { - assert(pNewEntry->next != NULL); - } - - pushfrontNodeInEntryList(pNewEntry, pNext); - } else { - pNode = pNext; - } - } - - if (pe->num == 0) { - assert(pe->next == NULL); - } else { - assert(pe->next != NULL); + pPrev = pNode; } - + pNode = pNext; } - } int64_t et = taosGetTimestampUs(); uDebug("hash table resize completed, new capacity:%d, load factor:%f, elapsed time:%fms", (int32_t)pHashObj->capacity, - ((double)pHashObj->size) / pHashObj->capacity, (et - st) / 1000.0); + ((double)pHashObj->size) / pHashObj->capacity, (et - st) / 1000.0); } SHashNode *doCreateHashNode(const void *key, size_t keyLen, const void *pData, size_t dsize, uint32_t hashVal) { -- GitLab