From 86e230cf8a3cfb658f40185db2d7f2a821a81616 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 9 Feb 2022 13:44:16 +0800 Subject: [PATCH] [td-11818] opt perf. --- include/util/tlosertree.h | 12 +++-- source/dnode/vnode/src/tsdb/tsdbRead.c | 2 +- source/libs/executor/src/executil.c | 2 +- source/libs/executor/src/executorimpl.c | 50 +++++++++++++-------- source/libs/executor/test/executorTests.cpp | 7 +++ source/util/src/tlosertree.c | 13 +----- 6 files changed, 49 insertions(+), 37 deletions(-) diff --git a/include/util/tlosertree.h b/include/util/tlosertree.h index 736135f7e9..241647ba1e 100644 --- a/include/util/tlosertree.h +++ b/include/util/tlosertree.h @@ -22,6 +22,11 @@ extern "C" { typedef int (*__merge_compare_fn_t)(const void *, const void *, void *param); +typedef struct STreeNode { + int32_t index; + void *pData; // TODO remove it? +} STreeNode; + typedef struct SMultiwayMergeTreeInfo { int32_t numOfSources; int32_t totalSources; @@ -30,6 +35,9 @@ typedef struct SMultiwayMergeTreeInfo { struct STreeNode *pNode; } SMultiwayMergeTreeInfo; +#define tMergeTreeGetChosenIndex(t_) ((t_)->pNode[0].index) +#define tMergeTreeGetAdjustIndex(t_) (tMergeTreeGetChosenIndex(t_) + (t_)->numOfSources) + int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo **pTree, uint32_t numOfEntries, void *param, __merge_compare_fn_t compareFn); void tMergeTreeDestroy(SMultiwayMergeTreeInfo* pTree); @@ -40,10 +48,6 @@ void tMergeTreeRebuild(SMultiwayMergeTreeInfo *pTree); void tMergeTreePrint(const SMultiwayMergeTreeInfo *pTree); -int32_t tMergeTreeGetChosenIndex(const SMultiwayMergeTreeInfo* pTree); - -int32_t tMergeTreeAdjustIndex(const SMultiwayMergeTreeInfo* pTree); - #ifdef __cplusplus } #endif diff --git a/source/dnode/vnode/src/tsdb/tsdbRead.c b/source/dnode/vnode/src/tsdb/tsdbRead.c index 16d8bf74d0..181f0be4a4 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead.c @@ -2192,7 +2192,7 @@ static int32_t createDataBlocksInfo(STsdbReadHandle* pTsdbReadHandle, int32_t nu sup.blockIndexArray[pos] = sup.numOfBlocksPerTable[pos] + 1; } - tMergeTreeAdjust(pTree, tMergeTreeAdjustIndex(pTree)); + tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree)); } /* diff --git a/source/libs/executor/src/executil.c b/source/libs/executor/src/executil.c index 1ef934002a..18c4075c45 100644 --- a/source/libs/executor/src/executil.c +++ b/source/libs/executor/src/executil.c @@ -612,7 +612,7 @@ static UNUSED_FUNC int32_t mergeIntoGroupResultImpl(STaskRuntimeEnv *pRuntimeEnv } } - tMergeTreeAdjust(pTree, tMergeTreeAdjustIndex(pTree)); + tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree)); } int64_t endt = taosGetTimestampMs(); diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 6da08e8e3c..c1465023c0 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -5548,31 +5548,39 @@ int32_t msortComparFn(const void *pLeft, const void *pRight, void *param) { int32_t pRightIdx = *(int32_t *)pRight; SMsortComparParam *pParam = (SMsortComparParam *)param; - SExternalMemSource **pSources = pParam->pSources; SArray *pInfo = pParam->orderInfo; + SExternalMemSource* pLeftSource = pParam->pSources[pLeftIdx]; + SExternalMemSource* pRightSource = pParam->pSources[pRightIdx]; + // this input is exhausted, set the special value to denote this - if (pSources[pLeftIdx]->rowIndex == -1) { + if (pLeftSource->rowIndex == -1) { return 1; } - if (pSources[pRightIdx]->rowIndex == -1) { + if (pRightSource->rowIndex == -1) { return -1; } - SSDataBlock* pLeftBlock = pSources[pLeftIdx]->pBlock; - SSDataBlock* pRightBlock = pSources[pRightIdx]->pBlock; + SSDataBlock* pLeftBlock = pLeftSource->pBlock; + SSDataBlock* pRightBlock = pRightSource->pBlock; - size_t num = taosArrayGetSize(pInfo); - for(int32_t i = 0; i < num; ++i) { - SBlockOrderInfo* pOrder = taosArrayGet(pInfo, i); + for(int32_t i = 0; i < pInfo->size; ++i) { + SBlockOrderInfo* pOrder = TARRAY_GET_ELEM(pInfo, i); - SColumnInfoData* pLeftColInfoData = taosArrayGet(pLeftBlock->pDataBlock, pOrder->colIndex); - bool leftNull = colDataIsNull(pLeftColInfoData, pLeftBlock->info.rows, pSources[pLeftIdx]->rowIndex, pLeftBlock->pBlockAgg); + SColumnInfoData* pLeftColInfoData = TARRAY_GET_ELEM(pLeftBlock->pDataBlock, pOrder->colIndex); - SColumnInfoData* pRightColInfoData = taosArrayGet(pRightBlock->pDataBlock, pOrder->colIndex); - bool rightNull = colDataIsNull(pRightColInfoData, pRightBlock->info.rows, pSources[pRightIdx]->rowIndex, pRightBlock->pBlockAgg); + bool leftNull = false; + if (pLeftColInfoData->hasNull) { + leftNull = colDataIsNull(pLeftColInfoData, pLeftBlock->info.rows, pLeftSource->rowIndex, pLeftBlock->pBlockAgg); + } + + SColumnInfoData* pRightColInfoData = TARRAY_GET_ELEM(pRightBlock->pDataBlock, pOrder->colIndex); + bool rightNull = false; + if (pRightColInfoData->hasNull) { + rightNull = colDataIsNull(pRightColInfoData, pRightBlock->info.rows, pRightSource->rowIndex, pRightBlock->pBlockAgg); + } if (leftNull && rightNull) { continue; // continue to next slot @@ -5586,20 +5594,24 @@ int32_t msortComparFn(const void *pLeft, const void *pRight, void *param) { return pParam->nullFirst? -1:1; } - void* left1 = colDataGet(pLeftColInfoData, pSources[pLeftIdx]->rowIndex); - void* right1 = colDataGet(pRightColInfoData, pSources[pRightIdx]->rowIndex); + void* left1 = colDataGet(pLeftColInfoData, pLeftSource->rowIndex); + void* right1 = colDataGet(pRightColInfoData, pRightSource->rowIndex); switch(pLeftColInfoData->info.type) { - case TSDB_DATA_TYPE_INT: - if (*(int32_t*) left1 == *(int32_t*) right1) { + case TSDB_DATA_TYPE_INT: { + int32_t leftv = *(int32_t*)left1; + int32_t rightv = *(int32_t*)right1; + + if (leftv == rightv) { break; } else { if (pOrder->order == TSDB_ORDER_ASC) { - return *(int32_t*) left1 <= *(int32_t*) right1? -1:1; + return leftv < rightv? -1 : 1; } else { - return *(int32_t*) left1 <= *(int32_t*) right1? 1:-1; + return leftv < rightv? 1 : -1; } } + } default: assert(0); } @@ -5634,7 +5646,7 @@ static int32_t adjustMergeTreeForNextTuple(SExternalMemSource *pSource, SMultiwa * Adjust loser tree otherwise, according to new candidate data * if the loser tree is rebuild completed, we do not need to adjust */ - int32_t leafNodeIndex = tMergeTreeAdjustIndex(pTree); + int32_t leafNodeIndex = tMergeTreeGetAdjustIndex(pTree); #ifdef _DEBUG_VIEW printf("before adjust:\t"); diff --git a/source/libs/executor/test/executorTests.cpp b/source/libs/executor/test/executorTests.cpp index d92bf966de..bd8e8fb357 100644 --- a/source/libs/executor/test/executorTests.cpp +++ b/source/libs/executor/test/executorTests.cpp @@ -262,6 +262,9 @@ TEST(testCase, external_sort_Test) { int32_t total = 1; + int64_t s1 = taosGetTimestampUs(); + + while(1) { int64_t s = taosGetTimestampUs(); pRes = pOperator->exec(pOperator, &newgroup); @@ -281,6 +284,10 @@ TEST(testCase, external_sort_Test) { // } } + int64_t s2 = taosGetTimestampUs(); + printf("total:%ld\n", s2 - s1); + + pOperator->cleanupFn(pOperator->info, 2); tfree(exp); tfree(exp1); diff --git a/source/util/src/tlosertree.c b/source/util/src/tlosertree.c index 53dabbdbdd..80bbac2c78 100644 --- a/source/util/src/tlosertree.c +++ b/source/util/src/tlosertree.c @@ -18,10 +18,7 @@ #include "tlosertree.h" #include "taoserror.h" -typedef struct STreeNode { - int32_t index; - void *pData; // TODO remove it? -} STreeNode; + // Set the initial value of the multiway merge tree. static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) { @@ -137,11 +134,3 @@ void tMergeTreePrint(const SMultiwayMergeTreeInfo* pTree) { printf("\n"); } - -int32_t tMergeTreeGetChosenIndex(const SMultiwayMergeTreeInfo* pTree) { - return pTree->pNode[0].index; -} - -int32_t tMergeTreeAdjustIndex(const SMultiwayMergeTreeInfo* pTree) { - return tMergeTreeGetChosenIndex(pTree) + pTree->numOfSources; -} -- GitLab