From e25b407c5a6379cf1dd0062f0b795220231f76a5 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 1 Apr 2022 08:00:46 +0000 Subject: [PATCH] optimize search process --- source/libs/tdb/src/db/tdbBtree.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 740e665cd2..f4e1621742 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -1380,6 +1380,30 @@ static int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) { ASSERT(nCells > 0); ASSERT(pBtc->idx == -1); + // compare first cell + midx = lidx; + pCell = tdbPageGetCell(pPage, midx); + tdbBtreeDecodeCell(pPage, pCell, &cd); + c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); + if (c <= 0) { + ridx = lidx - 1; + } else { + lidx = lidx + 1; + } + + // compare last cell + if (lidx <= ridx) { + midx = ridx; + pCell = tdbPageGetCell(pPage, midx); + tdbBtreeDecodeCell(pPage, pCell, &cd); + c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen); + if (c >= 0) { + lidx = ridx + 1; + } else { + ridx = ridx - 1; + } + } + // binary search for (;;) { if (lidx > ridx) break;