From 658873bdb343e7750863b4292f53d468821c3ded Mon Sep 17 00:00:00 2001 From: drh Date: Mon, 22 Jun 2015 20:02:04 +0000 Subject: [PATCH 1/8] Change the way that balance_nonroot() partitions cells between the sibling pages such that a scan of the cell size array is not required. FossilOrigin-Name: 168728715156d756ac8c0df45710d054eee027ec --- manifest | 15 ++++++---- manifest.uuid | 2 +- src/btree.c | 81 ++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 70 insertions(+), 28 deletions(-) diff --git a/manifest b/manifest index 9332d172a2..0d513dcffa 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Update\sthe\sfuzztest\sdata\susing\sthe\slatest\stest\svectors\sdiscovered\sby\sAFL. -D 2015-06-20T14:11:56.166 +C Change\sthe\sway\sthat\sbalance_nonroot()\spartitions\scells\sbetween\sthe\ssibling\npages\ssuch\sthat\sa\sscan\sof\sthe\scell\ssize\sarray\sis\snot\srequired. +D 2015-06-22T20:02:04.079 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1063c58075b7400d93326b0eb332b48a54f53025 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c 173c2ba1b8cf941971683f584965369791125f12 +F src/btree.c 8f65725c7c138aecb1a6339ef2b276b4b3632a20 F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btreeInt.h 6ece2dd9c8e2eac05f0a8ded8772a44e96486c65 F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70 @@ -1286,7 +1286,10 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 7cdbae625eb029538a693d2bebec465a6f65fb90 -R 5add07a39efb3611e2cfa0ac20a2c5f5 +P b97f9cf73e503c7285ba3a801e1f932f222d96b2 +R d9f18f053d77ccfca6ce052159dd7203 +T *branch * btree-opt2 +T *sym-btree-opt2 * +T -sym-trunk * U drh -Z 71db4d574368380e4b14cadc01f6d8d0 +Z ffa77b78cbb43ac17ffd24e89fe96b6a diff --git a/manifest.uuid b/manifest.uuid index ad06e9f3b1..49c14f7000 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -b97f9cf73e503c7285ba3a801e1f932f222d96b2 \ No newline at end of file +168728715156d756ac8c0df45710d054eee027ec \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index e64139b800..ad86c36e78 100644 --- a/src/btree.c +++ b/src/btree.c @@ -3206,6 +3206,7 @@ static int setChildPtrmaps(MemPage *pPage){ if( !pPage->leaf ){ Pgno childPgno = get4byte(pCell); ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); + if( rc ) return rc; } } @@ -6295,7 +6296,7 @@ static void insertCell( ** The MemPage.nFree field is invalidated by this function. It is the ** responsibility of the caller to set it correctly. */ -static void rebuildPage( +static int rebuildPage( MemPage *pPg, /* Edit this page */ int nCell, /* Final number of cells on page */ u8 **apCell, /* Array of cells */ @@ -6320,9 +6321,10 @@ static void rebuildPage( pCell = &pTmp[pCell - aData]; } pData -= szCell[i]; - memcpy(pData, pCell, szCell[i]); put2byte(pCellptr, (pData - aData)); pCellptr += 2; + if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; + memcpy(pData, pCell, szCell[i]); assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); testcase( szCell[i]==pPg->xCellSize(pPg,pCell) ); } @@ -6335,6 +6337,7 @@ static void rebuildPage( put2byte(&aData[hdr+3], pPg->nCell); put2byte(&aData[hdr+5], pData - aData); aData[hdr+7] = 0x00; + return SQLITE_OK; } /* @@ -6454,7 +6457,7 @@ static int pageFreeArray( ** The pPg->nFree field is invalid when this function returns. It is the ** responsibility of the caller to set it correctly. */ -static void editPage( +static int editPage( MemPage *pPg, /* Edit this page */ int iOld, /* Index of first cell currently on page */ int iNew, /* Index of new first cell on page */ @@ -6545,10 +6548,10 @@ static void editPage( } #endif - return; + return SQLITE_OK; editpage_fail: /* Unable to edit this page. Rebuild it from scratch instead. */ - rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); + return rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); } /* @@ -6620,7 +6623,8 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ assert( sqlite3PagerIswriteable(pNew->pDbPage) ); assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); - rebuildPage(pNew, 1, &pCell, &szCell); + rc = rebuildPage(pNew, 1, &pCell, &szCell); + if( rc ) return rc; pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; /* If this is an auto-vacuum database, update the pointer map @@ -6835,7 +6839,6 @@ static int balance_nonroot( int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ int usableSpace; /* Bytes in pPage beyond the header */ int pageFlags; /* Value of pPage->aData[0] */ - int subtotal; /* Subtotal of bytes in cells on one page */ int iSpace1 = 0; /* First unused byte of aSpace1[] */ int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ int szScratch; /* Size of scratch memory requested */ @@ -7081,21 +7084,50 @@ static int balance_nonroot( ** */ usableSpace = pBt->usableSize - 12 + leafCorrection; - for(subtotal=k=i=0; i usableSpace ){ - szNew[k] = subtotal - szCell[i] - 2; - cntNew[k] = i; - if( leafData ){ i--; } - subtotal = 0; - k++; - if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } + for(i=0; inFree; + if( szNew[i]<0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } + for(j=0; jnOverflow; j++){ + szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); + } + cntNew[i] = cntOld[i]; + } + k = nOld; + for(i=0; iusableSpace ){ + if( i+1>=k ){ + k = i+2; + if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } + szNew[k-1] = 0; + cntNew[k-1] = nCell; + } + sz = 2+szCell[cntNew[i]-1]; + szNew[i] -= sz; + if( !leafData ){ + sz = cntNew[i]usableSpace ) break; + szNew[i] += sz; + cntNew[i]++; + if( !leafData ){ + sz = cntNew[i]=nCell ){ + k = i+1; + }else if( cntNew[i] - (i>0 ? cntNew[i-1] : 0) <= 0 ){ + rc = SQLITE_CORRUPT_BKPT; + goto balance_cleanup; } } - szNew[k] = subtotal; - cntNew[k] = nCell; - k++; /* ** The packing computed by the previous block is biased toward the siblings @@ -7124,6 +7156,10 @@ static int balance_nonroot( szRight += szCell[d] + 2; szLeft -= szCell[r] + 2; cntNew[i-1]--; + if( cntNew[i-1] <= 0 ){ + rc = SQLITE_CORRUPT_BKPT; + goto balance_cleanup; + } r = cntNew[i-1] - 1; d = r + 1 - leafData; } @@ -7294,9 +7330,11 @@ static int balance_nonroot( ){ if( !leafCorrection ){ ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); + if( rc ) goto balance_cleanup; } if( szCell[i]>pNew->minLocal ){ ptrmapPutOvflPtr(pNew, pCell, &rc); + if( rc ) goto balance_cleanup; } } } @@ -7404,7 +7442,8 @@ static int balance_nonroot( nNewCell = cntNew[iPg] - iNew; } - editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell); + rc = editPage(apNew[iPg], iOld, iNew, nNewCell, apCell, szCell); + if( rc ) goto balance_cleanup; abDone[iPg]++; apNew[iPg]->nFree = usableSpace-szNew[iPg]; assert( apNew[iPg]->nOverflow==0 ); From 1ffd247c0c9e01226a6803d130d00a2321daeb75 Mon Sep 17 00:00:00 2001 From: drh Date: Tue, 23 Jun 2015 02:37:30 +0000 Subject: [PATCH 2/8] Avoid computing cell sizes in balance_nonroot() until they are really needed. This gives an overall 1.7% performance gain for about 1000 extra bytes of code space. FossilOrigin-Name: 43844537e8a372953386663f8177202901ba7566 --- manifest | 15 ++-- manifest.uuid | 2 +- src/btree.c | 229 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 154 insertions(+), 92 deletions(-) diff --git a/manifest b/manifest index 0d513dcffa..2e7c75d3cc 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Change\sthe\sway\sthat\sbalance_nonroot()\spartitions\scells\sbetween\sthe\ssibling\npages\ssuch\sthat\sa\sscan\sof\sthe\scell\ssize\sarray\sis\snot\srequired. -D 2015-06-22T20:02:04.079 +C Avoid\scomputing\scell\ssizes\sin\sbalance_nonroot()\suntil\sthey\sare\sreally\sneeded.\nThis\sgives\san\soverall\s1.7%\sperformance\sgain\sfor\sabout\s1000\sextra\sbytes\sof\ncode\sspace. +D 2015-06-23T02:37:30.513 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1063c58075b7400d93326b0eb332b48a54f53025 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c 8f65725c7c138aecb1a6339ef2b276b4b3632a20 +F src/btree.c e6ac65de1742116fb5abbb106a26eb5bf48cd79e F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btreeInt.h 6ece2dd9c8e2eac05f0a8ded8772a44e96486c65 F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70 @@ -1286,10 +1286,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P b97f9cf73e503c7285ba3a801e1f932f222d96b2 -R d9f18f053d77ccfca6ce052159dd7203 -T *branch * btree-opt2 -T *sym-btree-opt2 * -T -sym-trunk * +P 168728715156d756ac8c0df45710d054eee027ec +R 9b9baae7202760bc8528aa5e0a31b7cf U drh -Z ffa77b78cbb43ac17ffd24e89fe96b6a +Z 9735e4632bc637c34becd65ed89566cb diff --git a/manifest.uuid b/manifest.uuid index 49c14f7000..e09e14ca9e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -168728715156d756ac8c0df45710d054eee027ec \ No newline at end of file +43844537e8a372953386663f8177202901ba7566 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index ad86c36e78..9693b0d393 100644 --- a/src/btree.c +++ b/src/btree.c @@ -6283,6 +6283,52 @@ static void insertCell( } } +/* +** A CellArray object contains a cache of pointers and sizes for a +** consecutive sequence of cells that might be held multiple pages. +*/ +typedef struct CellArray CellArray; +struct CellArray { + int nCell; /* Number of cells in apCell[] */ + MemPage *pRef; /* Reference page */ + u8 **apCell; /* All cells begin balanced */ + u16 *szCell; /* Local size of all cells in apCell[] */ +}; + +/* +** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been +** computed. +*/ +static void populateCellCache(CellArray *p, int idx, int N){ + assert( idx>=0 && idx+N<=p->nCell ); + while( N>0 ){ + assert( p->apCell[idx]!=0 ); + if( p->szCell[idx]==0 ){ + p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]); + }else{ + assert( CORRUPT_DB || + p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) ); + } + idx++; + N--; + } +} + +/* +** Return the size of the Nth element of the cell array +*/ +static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){ + assert( N>=0 && NnCell ); + assert( p->szCell[N]==0 ); + p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]); + return p->szCell[N]; +} +static u16 cachedCellSize(CellArray *p, int N){ + assert( N>=0 && NnCell ); + if( p->szCell[N] ) return p->szCell[N]; + return computeCellSize(p, N); +} + /* ** Array apCell[] contains pointers to nCell b-tree page cells. The ** szCell[] array contains the size in bytes of each cell. This function @@ -6462,8 +6508,7 @@ static int editPage( int iOld, /* Index of first cell currently on page */ int iNew, /* Index of new first cell on page */ int nNew, /* Final number of cells on page */ - u8 **apCell, /* Array of cells */ - u16 *szCell /* Array of cell sizes */ + CellArray *pCArray /* Array of cells and sizes */ ){ u8 * const aData = pPg->aData; const int hdr = pPg->hdrOffset; @@ -6482,15 +6527,19 @@ static int editPage( /* Remove cells from the start and end of the page */ if( iOldapCell[iOld], &pCArray->szCell[iOld] ); memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); nCell -= nShift; } if( iNewEnd < iOldEnd ){ + populateCellCache(pCArray, iNewEnd, iOldEnd-iNewEnd); nCell -= pageFreeArray( - pPg, iOldEnd-iNewEnd, &apCell[iNewEnd], &szCell[iNewEnd] + pPg, iOldEnd-iNewEnd, + &pCArray->apCell[iNewEnd], &pCArray->szCell[iNewEnd] ); } @@ -6503,9 +6552,10 @@ static int editPage( assert( (iOld-iNew)aCellIdx; memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); + populateCellCache(pCArray, iNew, nAdd); if( pageInsertArray( pPg, pBegin, &pData, pCellptr, - nAdd, &apCell[iNew], &szCell[iNew] + nAdd, &pCArray->apCell[iNew], &pCArray->szCell[iNew] ) ) goto editpage_fail; nCell += nAdd; } @@ -6517,18 +6567,20 @@ static int editPage( pCellptr = &pPg->aCellIdx[iCell * 2]; memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); nCell++; + (void)cachedCellSize(pCArray, iCell + iNew); if( pageInsertArray( pPg, pBegin, &pData, pCellptr, - 1, &apCell[iCell + iNew], &szCell[iCell + iNew] + 1, &pCArray->apCell[iCell + iNew], &pCArray->szCell[iCell + iNew] ) ) goto editpage_fail; } } /* Append cells to the end of the page */ pCellptr = &pPg->aCellIdx[nCell*2]; + populateCellCache(pCArray, iNew+nCell, nNew-nCell); if( pageInsertArray( pPg, pBegin, &pData, pCellptr, - nNew-nCell, &apCell[iNew+nCell], &szCell[iNew+nCell] + nNew-nCell, &pCArray->apCell[iNew+nCell], &pCArray->szCell[iNew+nCell] ) ) goto editpage_fail; pPg->nCell = nNew; @@ -6539,19 +6591,21 @@ static int editPage( #ifdef SQLITE_DEBUG for(i=0; iapCell[i+iNew]; int iOff = get2byte(&pPg->aCellIdx[i*2]); if( pCell>=aData && pCell<&aData[pPg->pBt->usableSize] ){ pCell = &pTmp[pCell - aData]; } - assert( 0==memcmp(pCell, &aData[iOff], szCell[i+iNew]) ); + assert( 0==memcmp(pCell, &aData[iOff], + pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) ); } #endif return SQLITE_OK; editpage_fail: /* Unable to edit this page. Rebuild it from scratch instead. */ - return rebuildPage(pPg, nNew, &apCell[iNew], &szCell[iNew]); + populateCellCache(pCArray, iNew, nNew); + return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]); } /* @@ -6828,7 +6882,6 @@ static int balance_nonroot( int bBulk /* True if this call is part of a bulk load */ ){ BtShared *pBt; /* The whole database */ - int nCell = 0; /* Number of cells in apCell[] */ int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ int nNew = 0; /* Number of pages in apNew[] */ int nOld; /* Number of pages in apOld[] */ @@ -6846,19 +6899,20 @@ static int balance_nonroot( MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ u8 *pRight; /* Location in parent of right-sibling pointer */ u8 *apDiv[NB-1]; /* Divider cells in pParent */ - int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ - int cntOld[NB+2]; /* Old index in aCell[] after i-th page */ + int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */ + int cntOld[NB+2]; /* Old index in b.apCell[] */ int szNew[NB+2]; /* Combined size of cells placed on i-th page */ - u8 **apCell = 0; /* All cells begin balanced */ - u16 *szCell; /* Local size of all cells in apCell[] */ u8 *aSpace1; /* Space for copies of dividers cells */ Pgno pgno; /* Temp var to store a page number in */ u8 abDone[NB+2]; /* True after i'th new page is populated */ Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ + CellArray b; /* Parsed information on cells being balanced */ memset(abDone, 0, sizeof(abDone)); + b.nCell = 0; + b.apCell = 0; pBt = pParent->pBt; assert( sqlite3_mutex_held(pBt->mutex) ); assert( sqlite3PagerIswriteable(pParent->pDbPage) ); @@ -6967,40 +7021,41 @@ static int balance_nonroot( ** Allocate space for memory structures */ szScratch = - nMaxCells*sizeof(u8*) /* apCell */ - + nMaxCells*sizeof(u16) /* szCell */ + nMaxCells*sizeof(u8*) /* b.apCell */ + + nMaxCells*sizeof(u16) /* b.szCell */ + pBt->pageSize; /* aSpace1 */ /* EVIDENCE-OF: R-28375-38319 SQLite will never request a scratch buffer ** that is more than 6 times the database page size. */ assert( szScratch<=6*(int)pBt->pageSize ); - apCell = sqlite3ScratchMalloc( szScratch ); - if( apCell==0 ){ + b.apCell = sqlite3ScratchMalloc( szScratch ); + if( b.apCell==0 ){ rc = SQLITE_NOMEM; goto balance_cleanup; } - szCell = (u16*)&apCell[nMaxCells]; - aSpace1 = (u8*)&szCell[nMaxCells]; + b.szCell = (u16*)&b.apCell[nMaxCells]; + aSpace1 = (u8*)&b.szCell[nMaxCells]; assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); /* ** Load pointers to all cells on sibling pages and the divider cells - ** into the local apCell[] array. Make copies of the divider cells + ** into the local b.apCell[] array. Make copies of the divider cells ** into space obtained from aSpace1[]. The divider cells have already ** been removed from pParent. ** ** If the siblings are on leaf pages, then the child pointers of the ** divider cells are stripped from the cells before they are copied - ** into aSpace1[]. In this way, all cells in apCell[] are without + ** into aSpace1[]. In this way, all cells in b.apCell[] are without ** child pointers. If siblings are not leaves, then all cell in - ** apCell[] include child pointers. Either way, all cells in apCell[] + ** b.apCell[] include child pointers. Either way, all cells in b.apCell[] ** are alike. ** ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. ** leafData: 1 if pPage holds key+data and pParent holds only keys. */ - leafCorrection = apOld[0]->leaf*4; - leafData = apOld[0]->intKeyLeaf; + b.pRef = apOld[0]; + leafCorrection = b.pRef->leaf*4; + leafData = b.pRef->intKeyLeaf; for(i=0; inCell+pOld->nOverflow; + memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit); if( pOld->nOverflow>0 ){ for(j=0; jxCellSize(pOld, apCell[nCell]); - nCell++; + assert( b.nCellaData; u16 maskPage = pOld->maskPage; u16 cellOffset = pOld->cellOffset; for(j=0; jxCellSize(pOld, apCell[nCell]); - nCell++; + assert( b.nCellmaxLocal+23 ); assert( iSpace1 <= (int)pBt->pageSize ); memcpy(pTemp, apDiv[i], sz); - apCell[nCell] = pTemp+leafCorrection; + b.apCell[b.nCell] = pTemp+leafCorrection; assert( leafCorrection==0 || leafCorrection==4 ); - szCell[nCell] = szCell[nCell] - leafCorrection; + b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection; if( !pOld->leaf ){ assert( leafCorrection==0 ); assert( pOld->hdrOffset==0 ); /* The right pointer of the child page pOld becomes the left ** pointer of the divider cell */ - memcpy(apCell[nCell], &pOld->aData[8], 4); + memcpy(b.apCell[b.nCell], &pOld->aData[8], 4); }else{ assert( leafCorrection==4 ); - while( szCell[nCell]<4 ){ + while( b.szCell[b.nCell]<4 ){ /* Do not allow any cells smaller than 4 bytes. If a smaller cell ** does exist, pad it with 0x00 bytes. */ - assert( szCell[nCell]==3 || CORRUPT_DB ); - assert( apCell[nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB ); + assert( b.szCell[b.nCell]==3 || CORRUPT_DB ); + assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB ); aSpace1[iSpace1++] = 0x00; - szCell[nCell]++; + b.szCell[b.nCell]++; } } - nCell++; + b.nCell++; } } /* - ** Figure out the number of pages needed to hold all nCell cells. + ** Figure out the number of pages needed to hold all b.nCell cells. ** Store this number in "k". Also compute szNew[] which is the total ** size of all cells on the i-th page and cntNew[] which is the index - ** in apCell[] of the cell that divides page i from page i+1. - ** cntNew[k] should equal nCell. + ** in b.apCell[] of the cell that divides page i from page i+1. + ** cntNew[k] should equal b.nCell. ** ** Values computed by this block: ** ** k: The total number of sibling pages ** szNew[i]: Spaced used on the i-th sibling page. - ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to + ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to ** the right of the i-th sibling page. ** usableSpace: Number of bytes of space available on each sibling. ** @@ -7101,27 +7155,35 @@ static int balance_nonroot( k = i+2; if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } szNew[k-1] = 0; - cntNew[k-1] = nCell; + cntNew[k-1] = b.nCell; } - sz = 2+szCell[cntNew[i]-1]; + sz = 2 + cachedCellSize(&b, cntNew[i]-1); szNew[i] -= sz; if( !leafData ){ - sz = cntNew[i]usableSpace ) break; szNew[i] += sz; cntNew[i]++; if( !leafData ){ - sz = cntNew[i]=nCell ){ + if( cntNew[i]>=b.nCell ){ k = i+1; }else if( cntNew[i] - (i>0 ? cntNew[i-1] : 0) <= 0 ){ rc = SQLITE_CORRUPT_BKPT; @@ -7146,22 +7208,24 @@ static int balance_nonroot( int r; /* Index of right-most cell in left sibling */ int d; /* Index of first cell to the left of right sibling */ - r = cntNew[i-1] - 1; - d = r + 1 - leafData; - assert( d szLeft-(b.szCell[r]+2)) ){ + break; + } + szRight += b.szCell[d] + 2; + szLeft -= b.szCell[r] + 2; cntNew[i-1]--; if( cntNew[i-1] <= 0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } - r = cntNew[i-1] - 1; - d = r + 1 - leafData; } szNew[i] = szRight; szNew[i-1] = szLeft; @@ -7200,7 +7264,7 @@ static int balance_nonroot( zeroPage(pNew, pageFlags); apNew[i] = pNew; nNew++; - cntOld[i] = nCell; + cntOld[i] = b.nCell; /* Set the pointer-map entry for the new sibling page. */ if( ISAUTOVACUUM ){ @@ -7305,8 +7369,8 @@ static int balance_nonroot( int iNew = 0; int iOld = 0; - for(i=0; inCell + pOld->nOverflow + !leafData; @@ -7332,7 +7396,7 @@ static int balance_nonroot( ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); if( rc ) goto balance_cleanup; } - if( szCell[i]>pNew->minLocal ){ + if( cachedCellSize(&b,i)>pNew->minLocal ){ ptrmapPutOvflPtr(pNew, pCell, &rc); if( rc ) goto balance_cleanup; } @@ -7349,20 +7413,21 @@ static int balance_nonroot( j = cntNew[i]; assert( jleaf ){ memcpy(&pNew->aData[8], pCell, 4); }else if( leafData ){ /* If the tree is a leaf-data tree, and the siblings are leaves, - ** then there is no divider cell in apCell[]. Instead, the divider + ** then there is no divider cell in b.apCell[]. Instead, the divider ** cell consists of the integer key for the right-most cell of ** the sibling-page assembled above only. */ CellInfo info; j--; - pNew->xParseCell(pNew, apCell[j], &info); + pNew->xParseCell(pNew, b.apCell[j], &info); pCell = pTemp; sz = 4 + putVarint(&pCell[4], info.nKey); pTemp = 0; @@ -7379,7 +7444,7 @@ static int balance_nonroot( ** cells are at least 4 bytes. It only happens in b-trees used ** to evaluate "IN (SELECT ...)" and similar clauses. */ - if( szCell[j]==4 ){ + if( b.szCell[j]==4 ){ assert(leafCorrection==4); sz = pParent->xCellSize(pParent, pCell); } @@ -7437,12 +7502,12 @@ static int balance_nonroot( iNew = iOld = 0; nNewCell = cntNew[0]; }else{ - iOld = iPgnFree = usableSpace-szNew[iPg]; @@ -7494,7 +7559,7 @@ static int balance_nonroot( assert( pParent->isInit ); TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", - nOld, nNew, nCell)); + nOld, nNew, b.nCell)); /* Free any old pages that were not reused as new pages. */ @@ -7517,7 +7582,7 @@ static int balance_nonroot( ** Cleanup before returning. */ balance_cleanup: - sqlite3ScratchFree(apCell); + sqlite3ScratchFree(b.apCell); for(i=0; i Date: Tue, 23 Jun 2015 14:49:42 +0000 Subject: [PATCH 3/8] Improvements to the way balance_nonroot() constructs the b.apCell array of pointers to cells. FossilOrigin-Name: ee44bb25b2a88e25ba2afe37cf03ba199692a3a0 --- manifest | 12 ++++----- manifest.uuid | 2 +- src/btree.c | 71 +++++++++++++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/manifest b/manifest index ea07d936b3..44f122c617 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sthe\scompound\sSELECT\soperator\sfix\sfrom\strunk. -D 2015-06-23T13:02:10.077 +C Improvements\sto\sthe\sway\sbalance_nonroot()\sconstructs\sthe\sb.apCell\sarray\sof\npointers\sto\scells. +D 2015-06-23T14:49:42.852 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1063c58075b7400d93326b0eb332b48a54f53025 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c e6ac65de1742116fb5abbb106a26eb5bf48cd79e +F src/btree.c 8d772673dc249aee6b6e65e4285291f7f2565ffd F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btreeInt.h 6ece2dd9c8e2eac05f0a8ded8772a44e96486c65 F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70 @@ -1286,7 +1286,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 43844537e8a372953386663f8177202901ba7566 4df852ce26c95d5d23c83dbe9c59d2c3435acddf -R 5a31f6e2047c6b2a76413665b07fcf29 +P a7be554f4b8534fc237fa4c6defc38fcd4049707 +R 4ad5f5185dfc9700d38a61349b40bf79 U drh -Z ef941435462213fb596e80b11c30b19f +Z 77f5541f0ad6043b85ab2e262249ce69 diff --git a/manifest.uuid b/manifest.uuid index c792bcaca0..c1bc1337b1 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -a7be554f4b8534fc237fa4c6defc38fcd4049707 \ No newline at end of file +ee44bb25b2a88e25ba2afe37cf03ba199692a3a0 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 9693b0d393..adba9ec45e 100644 --- a/src/btree.c +++ b/src/btree.c @@ -958,27 +958,25 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)]))) #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I))))) - /* -** This a more complex version of findCell() that works for -** pages that do contain overflow cells. +** Sort the overflow cells of a page into index order. +** +** An O(N*N) algorithm is used. But that should not be a problem +** since N is only very rarely more than 1. */ -static u8 *findOverflowCell(MemPage *pPage, int iCell){ - int i; - assert( sqlite3_mutex_held(pPage->pBt->mutex) ); - for(i=pPage->nOverflow-1; i>=0; i--){ - int k; - k = pPage->aiOvfl[i]; - if( k<=iCell ){ - if( k==iCell ){ - return pPage->apOvfl[i]; +static void btreeSortOverflow(MemPage *p){ + int j, k; + for(j=0; jnOverflow-1; j++){ + for(k=j+1; knOverflow; k++){ + if( p->aiOvfl[j]>p->aiOvfl[k] ){ + SWAP(u16, p->aiOvfl[j], p->aiOvfl[k]); + SWAP(u8*, p->apOvfl[j], p->apOvfl[k]); } - iCell--; } } - return findCell(pPage, iCell); } + /* ** This is common tail processing for btreeParseCellPtr() and ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely @@ -7057,8 +7055,11 @@ static int balance_nonroot( leafCorrection = b.pRef->leaf*4; leafData = b.pRef->intKeyLeaf; for(i=0; inCell; + u8 *aData = pOld->aData; + u16 maskPage = pOld->maskPage; + u16 cellOffset = pOld->cellOffset; /* Verify that all sibling pages are of the same "type" (table-leaf, ** table-interior, index-leaf, or index-interior). @@ -7068,24 +7069,38 @@ static int balance_nonroot( goto balance_cleanup; } - limit = pOld->nCell+pOld->nOverflow; + /* Load b.apCell[] with pointers to all cells in pOld. Intersperse + ** overflow cells in the correct sequence. + ** + ** This must be done in advance. Once the balance starts, the cell + ** offset section of the btree page will be overwritten and we will no + ** long be able to find the cells if a pointer to each cell is not saved + ** first. + */ memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit); + j = 0; if( pOld->nOverflow>0 ){ - for(j=0; jaData; - u16 maskPage = pOld->maskPage; - u16 cellOffset = pOld->cellOffset; - for(j=0; jnOverflow); + btreeSortOverflow(pOld); + for(k=0; knOverflow; k++){ + limit = pOld->aiOvfl[k] - k; + while( japOvfl[k]; b.nCell++; } + limit = pOld->nCell; } + while( j Date: Tue, 23 Jun 2015 15:36:34 +0000 Subject: [PATCH 4/8] Change pageInsertArray() and pageFreeArray() so that they use the CellArray object and compute cell sizes as needed, resulting in smaller and faster code. FossilOrigin-Name: f7f41818119bb7bfbd1f1297d294b32f32769cd3 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/btree.c | 49 ++++++++++++++++++++++--------------------------- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/manifest b/manifest index 44f122c617..5b814adf85 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Improvements\sto\sthe\sway\sbalance_nonroot()\sconstructs\sthe\sb.apCell\sarray\sof\npointers\sto\scells. -D 2015-06-23T14:49:42.852 +C Change\spageInsertArray()\sand\spageFreeArray()\sso\sthat\sthey\suse\sthe\sCellArray\nobject\sand\scompute\scell\ssizes\sas\sneeded,\sresulting\sin\ssmaller\sand\sfaster\scode. +D 2015-06-23T15:36:34.487 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1063c58075b7400d93326b0eb332b48a54f53025 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c 8d772673dc249aee6b6e65e4285291f7f2565ffd +F src/btree.c d3299e9237047e7aaf2812f394dda4889f2e3aa9 F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btreeInt.h 6ece2dd9c8e2eac05f0a8ded8772a44e96486c65 F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70 @@ -1286,7 +1286,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P a7be554f4b8534fc237fa4c6defc38fcd4049707 -R 4ad5f5185dfc9700d38a61349b40bf79 +P ee44bb25b2a88e25ba2afe37cf03ba199692a3a0 +R 5ed6484269ac715a433769ddc5808d8c U drh -Z 77f5541f0ad6043b85ab2e262249ce69 +Z fb1540dd92109cdc5f0c16d0ac49412f diff --git a/manifest.uuid b/manifest.uuid index c1bc1337b1..afa0278d06 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ee44bb25b2a88e25ba2afe37cf03ba199692a3a0 \ No newline at end of file +f7f41818119bb7bfbd1f1297d294b32f32769cd3 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index adba9ec45e..975ea1c770 100644 --- a/src/btree.c +++ b/src/btree.c @@ -6414,25 +6414,26 @@ static int pageInsertArray( u8 *pBegin, /* End of cell-pointer array */ u8 **ppData, /* IN/OUT: Page content -area pointer */ u8 *pCellptr, /* Pointer to cell-pointer area */ + int iFirst, /* Index of first cell to add */ int nCell, /* Number of cells to add to pPg */ - u8 **apCell, /* Array of cells */ - u16 *szCell /* Array of cell sizes */ + CellArray *pCArray /* Array of cells */ ){ int i; u8 *aData = pPg->aData; u8 *pData = *ppData; const int bFreelist = aData[1] || aData[2]; + int iEnd = iFirst + nCell; assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ - for(i=0; iapCell[i], sz); put2byte(pCellptr, (pSlot - aData)); pCellptr += 2; } @@ -6451,22 +6452,27 @@ static int pageInsertArray( */ static int pageFreeArray( MemPage *pPg, /* Page to edit */ + int iFirst, /* First cell to delete */ int nCell, /* Cells to delete */ - u8 **apCell, /* Array of cells */ - u16 *szCell /* Array of cell sizes */ + CellArray *pCArray /* Array of cells */ ){ u8 * const aData = pPg->aData; u8 * const pEnd = &aData[pPg->pBt->usableSize]; u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; int nRet = 0; int i; + int iEnd = iFirst + nCell; u8 *pFree = 0; int szFree = 0; - for(i=0; iapCell[i]; if( pCell>=pStart && pCellszCell[i]; assert( sz>0 ); if( pFree!=(pCell + sz) ){ if( pFree ){ assert( pFree>aData && (pFree - aData)<65536 ); @@ -6525,20 +6531,12 @@ static int editPage( /* Remove cells from the start and end of the page */ if( iOldapCell[iOld], &pCArray->szCell[iOld] - ); + int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); nCell -= nShift; } if( iNewEnd < iOldEnd ){ - populateCellCache(pCArray, iNewEnd, iOldEnd-iNewEnd); - nCell -= pageFreeArray( - pPg, iOldEnd-iNewEnd, - &pCArray->apCell[iNewEnd], &pCArray->szCell[iNewEnd] - ); + nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); } pData = &aData[get2byteNotZero(&aData[hdr+5])]; @@ -6550,10 +6548,9 @@ static int editPage( assert( (iOld-iNew)aCellIdx; memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); - populateCellCache(pCArray, iNew, nAdd); if( pageInsertArray( pPg, pBegin, &pData, pCellptr, - nAdd, &pCArray->apCell[iNew], &pCArray->szCell[iNew] + iNew, nAdd, pCArray ) ) goto editpage_fail; nCell += nAdd; } @@ -6565,20 +6562,18 @@ static int editPage( pCellptr = &pPg->aCellIdx[iCell * 2]; memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); nCell++; - (void)cachedCellSize(pCArray, iCell + iNew); if( pageInsertArray( pPg, pBegin, &pData, pCellptr, - 1, &pCArray->apCell[iCell + iNew], &pCArray->szCell[iCell + iNew] + iCell+iNew, 1, pCArray ) ) goto editpage_fail; } } /* Append cells to the end of the page */ pCellptr = &pPg->aCellIdx[nCell*2]; - populateCellCache(pCArray, iNew+nCell, nNew-nCell); if( pageInsertArray( pPg, pBegin, &pData, pCellptr, - nNew-nCell, &pCArray->apCell[iNew+nCell], &pCArray->szCell[iNew+nCell] + iNew+nCell, nNew-nCell, pCArray ) ) goto editpage_fail; pPg->nCell = nNew; From 008d64c23ab2d1d2ca5feb1747407fd0dc120701 Mon Sep 17 00:00:00 2001 From: drh Date: Tue, 23 Jun 2015 16:00:24 +0000 Subject: [PATCH 5/8] Avoid unnecessary cachedCellSize() calls in the cell partition adjustment phase of balance_nonroot(). FossilOrigin-Name: 6319ee1256b1f7c39dd39a536a39b77b3d10dcac --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/btree.c | 10 ++++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/manifest b/manifest index 5b814adf85..a474ec479b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Change\spageInsertArray()\sand\spageFreeArray()\sso\sthat\sthey\suse\sthe\sCellArray\nobject\sand\scompute\scell\ssizes\sas\sneeded,\sresulting\sin\ssmaller\sand\sfaster\scode. -D 2015-06-23T15:36:34.487 +C Avoid\sunnecessary\scachedCellSize()\scalls\sin\sthe\scell\spartition\sadjustment\nphase\sof\sbalance_nonroot(). +D 2015-06-23T16:00:24.052 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1063c58075b7400d93326b0eb332b48a54f53025 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c d3299e9237047e7aaf2812f394dda4889f2e3aa9 +F src/btree.c 57f16870792abb0cc921124f3ee064d6379d5c8d F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btreeInt.h 6ece2dd9c8e2eac05f0a8ded8772a44e96486c65 F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70 @@ -1286,7 +1286,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P ee44bb25b2a88e25ba2afe37cf03ba199692a3a0 -R 5ed6484269ac715a433769ddc5808d8c +P f7f41818119bb7bfbd1f1297d294b32f32769cd3 +R 37fa05f0f572512a3f64ce71dc3bcdd6 U drh -Z fb1540dd92109cdc5f0c16d0ac49412f +Z c91e3b72c83e00e6480937885f4355e3 diff --git a/manifest.uuid b/manifest.uuid index afa0278d06..7d0c30b13e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f7f41818119bb7bfbd1f1297d294b32f32769cd3 \ No newline at end of file +6319ee1256b1f7c39dd39a536a39b77b3d10dcac \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 975ea1c770..ecf156d7d1 100644 --- a/src/btree.c +++ b/src/btree.c @@ -7218,12 +7218,12 @@ static int balance_nonroot( int r; /* Index of right-most cell in left sibling */ int d; /* Index of first cell to the left of right sibling */ + r = cntNew[i-1] - 1; + d = r + 1 - leafData; + (void)cachedCellSize(&b, d); while(1){ - r = cntNew[i-1] - 1; - d = r + 1 - leafData; assert( d szLeft-(b.szCell[r]+2)) ){ @@ -7231,11 +7231,13 @@ static int balance_nonroot( } szRight += b.szCell[d] + 2; szLeft -= b.szCell[r] + 2; - cntNew[i-1]--; + cntNew[i-1] = r; if( cntNew[i-1] <= 0 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } + r--; + d--; } szNew[i] = szRight; szNew[i-1] = szLeft; From 4f4bf7747a77d0fc2891fd98fec98a615c2bdbd5 Mon Sep 17 00:00:00 2001 From: drh Date: Tue, 23 Jun 2015 17:09:53 +0000 Subject: [PATCH 6/8] Faster loading of cell pointers into the b.apCell array in balance_nonroot. FossilOrigin-Name: fda89b0512477f9da09fd0f4e548ed4b13efd49d --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/btree.c | 18 +++++++++--------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/manifest b/manifest index a474ec479b..43f633e6d9 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Avoid\sunnecessary\scachedCellSize()\scalls\sin\sthe\scell\spartition\sadjustment\nphase\sof\sbalance_nonroot(). -D 2015-06-23T16:00:24.052 +C Faster\sloading\sof\scell\spointers\sinto\sthe\sb.apCell\sarray\sin\sbalance_nonroot. +D 2015-06-23T17:09:53.066 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1063c58075b7400d93326b0eb332b48a54f53025 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c 57f16870792abb0cc921124f3ee064d6379d5c8d +F src/btree.c 0b20e8281ea9b2add9eef6f6c3f18433f2f47b53 F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btreeInt.h 6ece2dd9c8e2eac05f0a8ded8772a44e96486c65 F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70 @@ -1286,7 +1286,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P f7f41818119bb7bfbd1f1297d294b32f32769cd3 -R 37fa05f0f572512a3f64ce71dc3bcdd6 +P 6319ee1256b1f7c39dd39a536a39b77b3d10dcac +R ed68212da4042fbfd3887c9cc6d179ba U drh -Z c91e3b72c83e00e6480937885f4355e3 +Z 1ac31720ec363831ce0715d2977b6c52 diff --git a/manifest.uuid b/manifest.uuid index 7d0c30b13e..5115d10516 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -6319ee1256b1f7c39dd39a536a39b77b3d10dcac \ No newline at end of file +fda89b0512477f9da09fd0f4e548ed4b13efd49d \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index ecf156d7d1..3a0a5e7f59 100644 --- a/src/btree.c +++ b/src/btree.c @@ -956,7 +956,6 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ */ #define findCell(P,I) \ ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)]))) -#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I))))) /* ** Sort the overflow cells of a page into index order. @@ -7054,7 +7053,7 @@ static int balance_nonroot( int limit = pOld->nCell; u8 *aData = pOld->aData; u16 maskPage = pOld->maskPage; - u16 cellOffset = pOld->cellOffset; + u8 *piCell = aData + pOld->cellOffset; /* Verify that all sibling pages are of the same "type" (table-leaf, ** table-interior, index-leaf, or index-interior). @@ -7073,27 +7072,28 @@ static int balance_nonroot( ** first. */ memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit); - j = 0; if( pOld->nOverflow>0 ){ memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow); btreeSortOverflow(pOld); - for(k=0; knOverflow; k++){ + for(j=k=0; knOverflow; k++){ limit = pOld->aiOvfl[k] - k; while( japOvfl[k]; b.nCell++; } - limit = pOld->nCell; + limit = pOld->nCell - j; } - while( j Date: Tue, 23 Jun 2015 18:24:25 +0000 Subject: [PATCH 7/8] Multiple overflow cells are always adjacent and sequential. Exploit this invariant for a small size reduction and performance increase and add assert()s to prove the invariant. FossilOrigin-Name: f77f2f48f48e374a72b6c054142f7a3ec0b1483c --- manifest | 12 +++++----- manifest.uuid | 2 +- src/btree.c | 64 +++++++++++++++++++++++++-------------------------- 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/manifest b/manifest index 43f633e6d9..aa8d0d2d87 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Faster\sloading\sof\scell\spointers\sinto\sthe\sb.apCell\sarray\sin\sbalance_nonroot. -D 2015-06-23T17:09:53.066 +C Multiple\soverflow\scells\sare\salways\sadjacent\sand\ssequential.\s\sExploit\sthis\ninvariant\sfor\sa\ssmall\ssize\sreduction\sand\sperformance\sincrease\sand\sadd\nassert()s\sto\sprove\sthe\sinvariant. +D 2015-06-23T18:24:25.456 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1063c58075b7400d93326b0eb332b48a54f53025 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c 0b20e8281ea9b2add9eef6f6c3f18433f2f47b53 +F src/btree.c ca394997680c8068a3ef87fd8ad55b1a7dad08a4 F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btreeInt.h 6ece2dd9c8e2eac05f0a8ded8772a44e96486c65 F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70 @@ -1286,7 +1286,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 6319ee1256b1f7c39dd39a536a39b77b3d10dcac -R ed68212da4042fbfd3887c9cc6d179ba +P fda89b0512477f9da09fd0f4e548ed4b13efd49d +R 8408184ce1eeef4fb81f9e80cd68a3ff U drh -Z 1ac31720ec363831ce0715d2977b6c52 +Z f70e49c696aab72ebd6898ae9c8ebc72 diff --git a/manifest.uuid b/manifest.uuid index 5115d10516..8e86c416da 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -fda89b0512477f9da09fd0f4e548ed4b13efd49d \ No newline at end of file +f77f2f48f48e374a72b6c054142f7a3ec0b1483c \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 3a0a5e7f59..d867a661a8 100644 --- a/src/btree.c +++ b/src/btree.c @@ -957,25 +957,6 @@ static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ #define findCell(P,I) \ ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)]))) -/* -** Sort the overflow cells of a page into index order. -** -** An O(N*N) algorithm is used. But that should not be a problem -** since N is only very rarely more than 1. -*/ -static void btreeSortOverflow(MemPage *p){ - int j, k; - for(j=0; jnOverflow-1; j++){ - for(k=j+1; knOverflow; k++){ - if( p->aiOvfl[j]>p->aiOvfl[k] ){ - SWAP(u16, p->aiOvfl[j], p->aiOvfl[k]); - SWAP(u8*, p->apOvfl[j], p->apOvfl[k]); - } - } - } -} - - /* ** This is common tail processing for btreeParseCellPtr() and ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely @@ -6243,6 +6224,14 @@ static void insertCell( assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) ); pPage->apOvfl[j] = pCell; pPage->aiOvfl[j] = (u16)i; + + /* When multiple overflows occur, they are always sequential and in + ** sorted order. This invariants arise because multiple overflows can + ** only occur when inserting divider cells into the parent page during + ** balancing, and the dividers are adjacent and sorted. + */ + assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ + assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ }else{ int rc = sqlite3PagerWrite(pPage->pDbPage); if( rc!=SQLITE_OK ){ @@ -7054,6 +7043,7 @@ static int balance_nonroot( u8 *aData = pOld->aData; u16 maskPage = pOld->maskPage; u8 *piCell = aData + pOld->cellOffset; + u8 *piEnd; /* Verify that all sibling pages are of the same "type" (table-leaf, ** table-interior, index-leaf, or index-interior). @@ -7063,8 +7053,17 @@ static int balance_nonroot( goto balance_cleanup; } - /* Load b.apCell[] with pointers to all cells in pOld. Intersperse - ** overflow cells in the correct sequence. + /* Load b.apCell[] with pointers to all cells in pOld. If pOld + ** constains overflow cells, include them in the b.apCell[] array + ** in the correct spot. + ** + ** Note that when there are multiple overflow cells, it is always the + ** case that they are sequential and adjacent. This invariant arises + ** because multiple overflows can only occurs when inserting divider + ** cells into a parent on a prior balance, and divider cells are always + ** adjacent and are inserted in order. There is an assert() tagged + ** with "NOTE 1" in the overflow cell insertion loop to prove this + ** invariant. ** ** This must be done in advance. Once the balance starts, the cell ** offset section of the btree page will be overwritten and we will no @@ -7074,22 +7073,21 @@ static int balance_nonroot( memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*limit); if( pOld->nOverflow>0 ){ memset(&b.szCell[b.nCell+limit], 0, sizeof(b.szCell[0])*pOld->nOverflow); - btreeSortOverflow(pOld); - for(j=k=0; knOverflow; k++){ - limit = pOld->aiOvfl[k] - k; - while( jaiOvfl[0]; + for(j=0; jnOverflow; k++){ + assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */ b.apCell[b.nCell] = pOld->apOvfl[k]; b.nCell++; } - limit = pOld->nCell - j; + limit = pOld->nCell - pOld->aiOvfl[0]; } - limit += b.nCell; - while( b.nCellcellOffset + 2*pOld->nCell; + while( piCell Date: Tue, 23 Jun 2015 21:35:28 +0000 Subject: [PATCH 8/8] Testability improvement. FossilOrigin-Name: eed6a3314518cdab809264284d7680385f0d5d2a --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/btree.c | 12 +++++------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/manifest b/manifest index aa8d0d2d87..718b155db2 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Multiple\soverflow\scells\sare\salways\sadjacent\sand\ssequential.\s\sExploit\sthis\ninvariant\sfor\sa\ssmall\ssize\sreduction\sand\sperformance\sincrease\sand\sadd\nassert()s\sto\sprove\sthe\sinvariant. -D 2015-06-23T18:24:25.456 +C Testability\simprovement. +D 2015-06-23T21:35:28.724 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1063c58075b7400d93326b0eb332b48a54f53025 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -192,7 +192,7 @@ F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 F src/backup.c ff743689c4d6c5cb55ad42ed9d174b2b3e71f1e3 F src/bitvec.c 5eb7958c3bf65210211cbcfc44eff86d0ded7c9d F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c ca394997680c8068a3ef87fd8ad55b1a7dad08a4 +F src/btree.c 587dd00c675e99d61614c9537feb73bfc2e9683d F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 F src/btreeInt.h 6ece2dd9c8e2eac05f0a8ded8772a44e96486c65 F src/build.c b3f15255d5b16e42dafeaa638fd4f8a47c94ed70 @@ -1286,7 +1286,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P fda89b0512477f9da09fd0f4e548ed4b13efd49d -R 8408184ce1eeef4fb81f9e80cd68a3ff +P f77f2f48f48e374a72b6c054142f7a3ec0b1483c +R 4d132507809128fd95303043fe3c27fc U drh -Z f70e49c696aab72ebd6898ae9c8ebc72 +Z 2d888246a6aeb99138896a0f8b9aa3e8 diff --git a/manifest.uuid b/manifest.uuid index 8e86c416da..62fd6afdb3 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f77f2f48f48e374a72b6c054142f7a3ec0b1483c \ No newline at end of file +eed6a3314518cdab809264284d7680385f0d5d2a \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index d867a661a8..77d9833f91 100644 --- a/src/btree.c +++ b/src/btree.c @@ -1553,7 +1553,7 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ nFrag = iFreeBlk - iEnd; if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT; iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); - if( iEnd > pPage->pBt->usableSize ) return SQLITE_CORRUPT_BKPT; + if( NEVER(iEnd > pPage->pBt->usableSize) ) return SQLITE_CORRUPT_BKPT; iSize = iEnd - iStart; iFreeBlk = get2byte(&data[iFreeBlk]); } @@ -3184,7 +3184,6 @@ static int setChildPtrmaps(MemPage *pPage){ if( !pPage->leaf ){ Pgno childPgno = get4byte(pCell); ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); - if( rc ) return rc; } } @@ -6358,7 +6357,7 @@ static int rebuildPage( if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; memcpy(pData, pCell, szCell[i]); assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); - testcase( szCell[i]==pPg->xCellSize(pPg,pCell) ); + testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) ); } /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ @@ -6659,7 +6658,7 @@ static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); rc = rebuildPage(pNew, 1, &pCell, &szCell); - if( rc ) return rc; + if( NEVER(rc) ) return rc; pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; /* If this is an auto-vacuum database, update the pointer map @@ -7404,12 +7403,11 @@ static int balance_nonroot( ){ if( !leafCorrection ){ ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); - if( rc ) goto balance_cleanup; } if( cachedCellSize(&b,i)>pNew->minLocal ){ ptrmapPutOvflPtr(pNew, pCell, &rc); - if( rc ) goto balance_cleanup; } + if( rc ) goto balance_cleanup; } } } @@ -8043,7 +8041,7 @@ int sqlite3BtreeDelete(BtCursor *pCur){ unsigned char *pTmp; pCell = findCell(pLeaf, pLeaf->nCell-1); - if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT; + if( NEVER(pCell<&pLeaf->aData[4]) ) return SQLITE_CORRUPT_BKPT; nCell = pLeaf->xCellSize(pLeaf, pCell); assert( MX_CELL_SIZE(pBt) >= nCell ); pTmp = pBt->pTmpSpace;