From: drh Date: Tue, 23 Jun 2015 18:24:25 +0000 (+0000) Subject: Multiple overflow cells are always adjacent and sequential. Exploit this X-Git-Tag: version-3.8.11~142^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fe647dc9eec094ed9a41a801e214076a966664f5;p=thirdparty%2Fsqlite.git 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 --- 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