1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Reimplement the freeSpace() routine in btree.c so that it runs faster.

FossilOrigin-Name: fe4fd014b42b7b158ca968f1535b5636c67769f6
This commit is contained in:
drh
2014-08-20 17:56:27 +00:00
parent 7fb91646b5
commit 5f5c753efb
3 changed files with 70 additions and 69 deletions

View File

@@ -1,5 +1,5 @@
C Refactor\slocal\svariable\snames\sin\sthe\sfreeSpace()\sroutine\sof\sbtree.c\sfor\nimproved\sunderstandability. C Reimplement\sthe\sfreeSpace()\sroutine\sin\sbtree.c\sso\sthat\sit\sruns\sfaster.
D 2014-08-20T14:37:09.167 D 2014-08-20T17:56:27.585
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 5eb79e334a5de69c87740edd56af6527dd219308 F Makefile.in 5eb79e334a5de69c87740edd56af6527dd219308
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -167,7 +167,7 @@ F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34
F src/backup.c a729e63cf5cd1829507cb7b8e89f99b95141bb53 F src/backup.c a729e63cf5cd1829507cb7b8e89f99b95141bb53
F src/bitvec.c 19a4ba637bd85f8f63fc8c9bae5ade9fb05ec1cb F src/bitvec.c 19a4ba637bd85f8f63fc8c9bae5ade9fb05ec1cb
F src/btmutex.c 976f45a12e37293e32cae0281b15a21d48a8aaa7 F src/btmutex.c 976f45a12e37293e32cae0281b15a21d48a8aaa7
F src/btree.c c1235eacb8d4de12850daccb1636763218da3381 F src/btree.c fe2d11c8c0d99e324614141ad012b37d0601cf59
F src/btree.h 4245a349bfe09611d7ff887dbc3a80cee8b7955a F src/btree.h 4245a349bfe09611d7ff887dbc3a80cee8b7955a
F src/btreeInt.h cf180d86b2e9e418f638d65baa425c4c69c0e0e3 F src/btreeInt.h cf180d86b2e9e418f638d65baa425c4c69c0e0e3
F src/build.c 5abf794fe8a605f2005b422e98a3cedad9b9ef5b F src/build.c 5abf794fe8a605f2005b422e98a3cedad9b9ef5b
@@ -1186,7 +1186,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 121308fa869ad490a6924798d276c0ff32759acc P 7e63089a191f29aefde05e89bb612f3036cfa034
R 332118792b6ef82f7eceb02b0d545622 R cbff1ae81d05ca3b78b420deaaa6083f
U drh U drh
Z accc32db7c4714707b5e7cc22312c49c Z d684bbbc1bfe7cdee532398c2232ddf8

View File

@@ -1 +1 @@
7e63089a191f29aefde05e89bb612f3036cfa034 fe4fd014b42b7b158ca968f1535b5636c67769f6

View File

@@ -1293,92 +1293,93 @@ defragment_page:
** The first byte of the new free block is pPage->aData[iStart] ** The first byte of the new free block is pPage->aData[iStart]
** and the size of the block is iSize bytes. ** and the size of the block is iSize bytes.
** **
** Most of the effort here is involved in coalesing adjacent ** Adjacent freeblocks are coalesced.
** free blocks into a single big free block. **
** Note that even though the freeblock list was checked by btreeInitPage(),
** that routine will not detect overlap between cells or freeblocks. Nor
** does it detect cells or freeblocks that encrouch into the reserved bytes
** at the end of the page. So do additional corruption checks inside this
** routine and return SQLITE_CORRUPT if any problems are found.
*/ */
static int freeSpace(MemPage *pPage, int iStart, int iSize){ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
int iPtr; /* Address of pointer to next freeblock */ u16 iPtr; /* Address of pointer to next freeblock */
int iFreeBlk; /* Address of the next freeblock */ u16 iFreeBlk; /* Address of the next freeblock */
int hdr; /* Page header size. 0 or 100 */ u8 hdr; /* Page header size. 0 or 100 */
int iLast; /* Largest possible freeblock offset */ u8 nFrag = 0; /* Reduction in fragmentation */
u16 iOrigSize = iSize; /* Original value of iSize */
u32 iLast = pPage->pBt->usableSize-4; /* Largest possible freeblock offset */
u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
unsigned char *data = pPage->aData; /* Page content */ unsigned char *data = pPage->aData; /* Page content */
assert( pPage->pBt!=0 ); assert( pPage->pBt!=0 );
assert( sqlite3PagerIswriteable(pPage->pDbPage) ); assert( sqlite3PagerIswriteable(pPage->pDbPage) );
assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); assert( iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
assert( (iStart + iSize) <= (int)pPage->pBt->usableSize ); assert( iEnd <= pPage->pBt->usableSize );
assert( sqlite3_mutex_held(pPage->pBt->mutex) ); assert( sqlite3_mutex_held(pPage->pBt->mutex) );
assert( iSize>=4 ); /* Minimum cell size is 4 */ assert( iSize>=4 ); /* Minimum cell size is 4 */
assert( iStart<=iLast );
/* Overwrite deleted information with zeros when the secure_delete
** option is enabled */
if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){ if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
/* Overwrite deleted information with zeros when the secure_delete
** option is enabled */
memset(&data[iStart], 0, iSize); memset(&data[iStart], 0, iSize);
} }
/* Add the space back into the linked list of freeblocks. Note that /* The list of freeblocks must be in ascending order. Find the
** even though the freeblock list was checked by btreeInitPage(), ** spot on the list where iStart should be inserted.
** btreeInitPage() did not detect overlapping cells or
** freeblocks that overlapped cells. Nor does it detect when the
** cell content area exceeds the value in the page header. If these
** situations arise, then subsequent insert operations might corrupt
** the freelist. So we do need to check for corruption while scanning
** the freelist.
*/ */
hdr = pPage->hdrOffset; hdr = pPage->hdrOffset;
iPtr = hdr + 1; iPtr = hdr + 1;
iLast = pPage->pBt->usableSize - 4; while( (iFreeBlk = get2byte(&data[iPtr]))>0 && iFreeBlk<iStart ){
assert( iStart<=iLast ); if( iFreeBlk<iPtr+4 ) return SQLITE_CORRUPT_BKPT;
while( (iFreeBlk = get2byte(&data[iPtr]))<iStart && iFreeBlk>0 ){
if( iFreeBlk<iPtr+4 ){
return SQLITE_CORRUPT_BKPT;
}
iPtr = iFreeBlk; iPtr = iFreeBlk;
} }
if( iFreeBlk>iLast ){ if( iFreeBlk>iLast ) return SQLITE_CORRUPT_BKPT;
return SQLITE_CORRUPT_BKPT;
}
assert( iFreeBlk>iPtr || iFreeBlk==0 ); assert( iFreeBlk>iPtr || iFreeBlk==0 );
put2byte(&data[iPtr], iStart);
put2byte(&data[iStart], iFreeBlk);
put2byte(&data[iStart+2], iSize);
pPage->nFree = pPage->nFree + (u16)iSize;
/* Coalesce adjacent free blocks */ /* At this point:
iPtr = hdr + 1; ** iFreeBlk: First freeblock after iStart, or zero if none
while( (iFreeBlk = get2byte(&data[iPtr]))>0 ){ ** iPtr: The address of a pointer iFreeBlk
int iNextBlk; /* Next freeblock after iFreeBlk */ **
int szFreeBlk; /* Size of iFreeBlk */ ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
assert( iFreeBlk>iPtr ); */
assert( iFreeBlk <= (int)pPage->pBt->usableSize-4 ); if( iFreeBlk && iEnd+3>=iFreeBlk ){
iNextBlk = get2byte(&data[iFreeBlk]); nFrag = iFreeBlk - iEnd;
szFreeBlk = get2byte(&data[iFreeBlk+2]); if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_BKPT;
if( iFreeBlk + szFreeBlk + 3 >= iNextBlk && iNextBlk>0 ){ iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
int nFrag; /* Fragment bytes in between iFreeBlk and iNextBlk */ iSize = iEnd - iStart;
int x; /* Temp value */ iFreeBlk = get2byte(&data[iFreeBlk]);
nFrag = iNextBlk - (iFreeBlk+szFreeBlk); }
if( (nFrag<0) || (nFrag>(int)data[hdr+7]) ){
return SQLITE_CORRUPT_BKPT; /* If iPtr is another freeblock (that is, if iPtr is not the freelist pointer
} ** in the page header) then check to see if iStart should be coalesced
data[hdr+7] -= (u8)nFrag; ** onto the end of iPtr.
x = get2byte(&data[iNextBlk]); */
put2byte(&data[iFreeBlk], x); if( iPtr>hdr+1 ){
x = iNextBlk + get2byte(&data[iNextBlk+2]) - iFreeBlk; int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
put2byte(&data[iFreeBlk+2], x); if( iPtrEnd+3>=iStart ){
}else{ if( iPtrEnd>iStart ) return SQLITE_CORRUPT_BKPT;
iPtr = iFreeBlk; nFrag += iStart - iPtrEnd;
iSize = iEnd - iPtr;
iStart = iPtr;
} }
} }
if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_BKPT;
/* If the cell content area begins with a freeblock, remove it. */ data[hdr+7] -= nFrag;
if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ if( iPtr==hdr+1 && iStart==get2byte(&data[hdr+5]) ){
int top; /* The new freeblock is at the beginning of the cell content area,
iFreeBlk = get2byte(&data[hdr+1]); ** so just extend the cell content area rather than create another
memcpy(&data[hdr+1], &data[iFreeBlk], 2); ** freelist entry */
top = get2byte(&data[hdr+5]) + get2byte(&data[iFreeBlk+2]); put2byte(&data[hdr+1], iFreeBlk);
put2byte(&data[hdr+5], top); put2byte(&data[hdr+5], iEnd);
}else{
/* Insert the new freeblock into the freelist */
put2byte(&data[iPtr], iStart);
put2byte(&data[iStart], iFreeBlk);
put2byte(&data[iStart+2], iSize);
} }
assert( sqlite3PagerIswriteable(pPage->pDbPage) ); pPage->nFree += iOrigSize;
return SQLITE_OK; return SQLITE_OK;
} }