mirror of
https://github.com/sqlite/sqlite.git
synced 2025-05-28 12:41:31 +03:00
Optimizations to btree.c save about 4.5 million CPU cycles:
(1) Clone insertCell() into a separate insertCellFast() routine for use by sqlite3BtreeInsert(). (2) Mark allocateSpace() as always-inline. (3) Improved coalesence of adjacent free blocks in pageFreeArray(). FossilOrigin-Name: 5c12c400fe8eb4e86e14c69a6c34d0d78d9861e5d40a36c6a596a81c6dd65977
This commit is contained in:
commit
2da07d9025
17
manifest
17
manifest
@ -1,5 +1,5 @@
|
||||
C Fix\sa\sproblem\scausing\sthe\srbu_exclusive_checkpoint=1\sto\sbe\signored\swith\szipvfs\sdatabases.
|
||||
D 2023-04-07T14:03:30.087
|
||||
C Optimizations\sto\sbtree.c\ssave\sabout\s4.5\smillion\sCPU\scycles:\n(1)\sClone\sinsertCell()\sinto\sa\sseparate\sinsertCellFast()\sroutine\sfor\nuse\sby\ssqlite3BtreeInsert().\s\s(2)\sMark\sallocateSpace()\sas\salways-inline.\n(3)\sImproved\scoalesence\sof\sadjacent\sfree\sblocks\sin\spageFreeArray().
|
||||
D 2023-04-08T13:01:34.177
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
||||
@ -564,7 +564,7 @@ F src/auth.c f4fa91b6a90bbc8e0d0f738aa284551739c9543a367071f55574681e0f24f8cf
|
||||
F src/backup.c a2891172438e385fdbe97c11c9745676bec54f518d4447090af97189fd8e52d7
|
||||
F src/bitvec.c 7c849aac407230278445cb069bebc5f89bf2ddd87c5ed9459b070a9175707b3d
|
||||
F src/btmutex.c 6ffb0a22c19e2f9110be0964d0731d2ef1c67b5f7fabfbaeb7b9dabc4b7740ca
|
||||
F src/btree.c 023b41e0a4563880812288e32682538013287b7b82e0c24a57b0e26b6f0ef168
|
||||
F src/btree.c 895a4ab3a5aeb7db9ed894bb3fd1e2eee4422e95fa6ca3d3d0e803bc0080ec55
|
||||
F src/btree.h aa354b9bad4120af71e214666b35132712b8f2ec11869cb2315c52c81fad45cc
|
||||
F src/btreeInt.h a3268a60cbc91f578001f44ba40aae9c1b8aecbb0d2c095dd7fc54b0872ea4b8
|
||||
F src/build.c 8357d6ca9a8c9afc297c431df28bc2af407b47f3ef2311875276c944b30c4d54
|
||||
@ -630,7 +630,7 @@ F src/shell.c.in 2140c98b8185ce5f024d706a552dd0ee861c35621dab6599a9234019348bf9d
|
||||
F src/sqlite.h.in 84f0e61a07292977c31f108776e5148eb1c761e7c276de2290c1511dad7c7d3a
|
||||
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
|
||||
F src/sqlite3ext.h da473ce2b3d0ae407a6300c4a164589b9a6bfdbec9462688a8593ff16f3bb6e4
|
||||
F src/sqliteInt.h 899781baef0d1dd0910524df6350e0ef7e2761131f6e04ec5e34f3b32e262998
|
||||
F src/sqliteInt.h a605bcdaf9083655e848b54d55c053000ed4ab8325cca8696c41585ba8ac05fd
|
||||
F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657
|
||||
F src/status.c 160c445d7d28c984a0eae38c144f6419311ed3eace59b44ac6dafc20db4af749
|
||||
F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
|
||||
@ -2052,8 +2052,9 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||
P c07b62bef96bb69e9b1ce08f0084fdce8de981f4d8ea9689c87a41f1e4451ac7
|
||||
R 8848363a9c3a234e64f59bd74865cd9d
|
||||
U dan
|
||||
Z 1cd4b7dd2aa9fce599ff7d1499f29586
|
||||
P d8f50b31e8d64fce5141da4a016767a15482703364692a55df346f059fc9d30c 2dcdbb50356edbd3a79e53fa0bee4e700c2bdea78e27173b62ddabe44b066726
|
||||
R 88a02467116587aeb37e104f663e3a20
|
||||
T +closed 2dcdbb50356edbd3a79e53fa0bee4e700c2bdea78e27173b62ddabe44b066726
|
||||
U drh
|
||||
Z f3818585026019f77b55a7228538100f
|
||||
# Remove this line to create a well-formed Fossil manifest.
|
||||
|
@ -1 +1 @@
|
||||
d8f50b31e8d64fce5141da4a016767a15482703364692a55df346f059fc9d30c
|
||||
5c12c400fe8eb4e86e14c69a6c34d0d78d9861e5d40a36c6a596a81c6dd65977
|
181
src/btree.c
181
src/btree.c
@ -1746,7 +1746,7 @@ static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
|
||||
** allocation is being made in order to insert a new cell, so we will
|
||||
** also end up needing a new cell pointer.
|
||||
*/
|
||||
static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
|
||||
static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
|
||||
const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
|
||||
u8 * const data = pPage->aData; /* Local cache of pPage->aData */
|
||||
int top; /* First byte of cell content area */
|
||||
@ -1862,7 +1862,7 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
|
||||
assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
|
||||
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
|
||||
assert( iSize>=4 ); /* Minimum cell size is 4 */
|
||||
assert( iStart<=pPage->pBt->usableSize-4 );
|
||||
assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
|
||||
|
||||
/* The list of freeblocks must be in ascending order. Find the
|
||||
** spot on the list where iStart should be inserted.
|
||||
@ -1919,6 +1919,11 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
|
||||
}
|
||||
pTmp = &data[hdr+5];
|
||||
x = get2byte(pTmp);
|
||||
if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
|
||||
/* Overwrite deleted information with zeros when the secure_delete
|
||||
** option is enabled */
|
||||
memset(&data[iStart], 0, iSize);
|
||||
}
|
||||
if( iStart<=x ){
|
||||
/* The new freeblock is at the beginning of the cell content area,
|
||||
** so just extend the cell content area rather than create another
|
||||
@ -1930,14 +1935,9 @@ static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
|
||||
}else{
|
||||
/* Insert the new freeblock into the freelist */
|
||||
put2byte(&data[iPtr], iStart);
|
||||
put2byte(&data[iStart], iFreeBlk);
|
||||
put2byte(&data[iStart+2], iSize);
|
||||
}
|
||||
if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
|
||||
/* Overwrite deleted information with zeros when the secure_delete
|
||||
** option is enabled */
|
||||
memset(&data[iStart], 0, iSize);
|
||||
}
|
||||
put2byte(&data[iStart], iFreeBlk);
|
||||
put2byte(&data[iStart+2], iSize);
|
||||
pPage->nFree += iOrigSize;
|
||||
return SQLITE_OK;
|
||||
}
|
||||
@ -7096,6 +7096,14 @@ static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
|
||||
** in pTemp or the original pCell) and also record its index.
|
||||
** Allocating a new entry in pPage->aCell[] implies that
|
||||
** pPage->nOverflow is incremented.
|
||||
**
|
||||
** The insertCellFast() routine below works exactly the same as
|
||||
** insertCell() except that it lacks the pTemp and iChild parameters
|
||||
** which are assumed zero. Other than that, the two routines are the
|
||||
** same.
|
||||
**
|
||||
** Fixes or enhancements to this routine should be reflected in
|
||||
** insertCellFast()!
|
||||
*/
|
||||
static int insertCell(
|
||||
MemPage *pPage, /* Page into which we are copying */
|
||||
@ -7118,14 +7126,103 @@ static int insertCell(
|
||||
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
|
||||
assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
|
||||
assert( pPage->nFree>=0 );
|
||||
assert( iChild>0 );
|
||||
if( pPage->nOverflow || sz+2>pPage->nFree ){
|
||||
if( pTemp ){
|
||||
memcpy(pTemp, pCell, sz);
|
||||
pCell = pTemp;
|
||||
}
|
||||
if( iChild ){
|
||||
put4byte(pCell, iChild);
|
||||
put4byte(pCell, iChild);
|
||||
j = pPage->nOverflow++;
|
||||
/* Comparison against ArraySize-1 since we hold back one extra slot
|
||||
** as a contingency. In other words, never need more than 3 overflow
|
||||
** slots but 4 are allocated, just to be safe. */
|
||||
assert( j < ArraySize(pPage->apOvfl)-1 );
|
||||
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( NEVER(rc!=SQLITE_OK) ){
|
||||
return rc;
|
||||
}
|
||||
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
|
||||
data = pPage->aData;
|
||||
assert( &data[pPage->cellOffset]==pPage->aCellIdx );
|
||||
rc = allocateSpace(pPage, sz, &idx);
|
||||
if( rc ){ return rc; }
|
||||
/* The allocateSpace() routine guarantees the following properties
|
||||
** if it returns successfully */
|
||||
assert( idx >= 0 );
|
||||
assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
|
||||
assert( idx+sz <= (int)pPage->pBt->usableSize );
|
||||
pPage->nFree -= (u16)(2 + sz);
|
||||
/* In a corrupt database where an entry in the cell index section of
|
||||
** a btree page has a value of 3 or less, the pCell value might point
|
||||
** as many as 4 bytes in front of the start of the aData buffer for
|
||||
** the source page. Make sure this does not cause problems by not
|
||||
** reading the first 4 bytes */
|
||||
memcpy(&data[idx+4], pCell+4, sz-4);
|
||||
put4byte(&data[idx], iChild);
|
||||
pIns = pPage->aCellIdx + i*2;
|
||||
memmove(pIns+2, pIns, 2*(pPage->nCell - i));
|
||||
put2byte(pIns, idx);
|
||||
pPage->nCell++;
|
||||
/* increment the cell count */
|
||||
if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
|
||||
assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
|
||||
#ifndef SQLITE_OMIT_AUTOVACUUM
|
||||
if( pPage->pBt->autoVacuum ){
|
||||
int rc2 = SQLITE_OK;
|
||||
/* The cell may contain a pointer to an overflow page. If so, write
|
||||
** the entry for the overflow page into the pointer map.
|
||||
*/
|
||||
ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
|
||||
if( rc2 ) return rc2;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return SQLITE_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
** This variant of insertCell() assumes that the pTemp and iChild
|
||||
** parameters are both zero. Use this variant in sqlite3BtreeInsert()
|
||||
** for performance improvement, and also so that this variant is only
|
||||
** called from that one place, and is thus inlined, and thus runs must
|
||||
** faster.
|
||||
**
|
||||
** Fixes or enhancements to this routine should be reflected into
|
||||
** the insertCell() routine.
|
||||
*/
|
||||
static int insertCellFast(
|
||||
MemPage *pPage, /* Page into which we are copying */
|
||||
int i, /* New cell becomes the i-th cell of the page */
|
||||
u8 *pCell, /* Content of the new cell */
|
||||
int sz /* Bytes of content in pCell */
|
||||
){
|
||||
int idx = 0; /* Where to write new cell content in data[] */
|
||||
int j; /* Loop counter */
|
||||
u8 *data; /* The content of the whole page */
|
||||
u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
|
||||
|
||||
assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
|
||||
assert( MX_CELL(pPage->pBt)<=10921 );
|
||||
assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
|
||||
assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
|
||||
assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
|
||||
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
|
||||
assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
|
||||
assert( pPage->nFree>=0 );
|
||||
assert( pPage->nOverflow==0 );
|
||||
if( sz+2>pPage->nFree ){
|
||||
j = pPage->nOverflow++;
|
||||
/* Comparison against ArraySize-1 since we hold back one extra slot
|
||||
** as a contingency. In other words, never need more than 3 overflow
|
||||
@ -7157,17 +7254,7 @@ static int insertCell(
|
||||
assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
|
||||
assert( idx+sz <= (int)pPage->pBt->usableSize );
|
||||
pPage->nFree -= (u16)(2 + sz);
|
||||
if( iChild ){
|
||||
/* In a corrupt database where an entry in the cell index section of
|
||||
** a btree page has a value of 3 or less, the pCell value might point
|
||||
** as many as 4 bytes in front of the start of the aData buffer for
|
||||
** the source page. Make sure this does not cause problems by not
|
||||
** reading the first 4 bytes */
|
||||
memcpy(&data[idx+4], pCell+4, sz-4);
|
||||
put4byte(&data[idx], iChild);
|
||||
}else{
|
||||
memcpy(&data[idx], pCell, sz);
|
||||
}
|
||||
memcpy(&data[idx], pCell, sz);
|
||||
pIns = pPage->aCellIdx + i*2;
|
||||
memmove(pIns+2, pIns, 2*(pPage->nCell - i));
|
||||
put2byte(pIns, idx);
|
||||
@ -7496,42 +7583,50 @@ static int pageFreeArray(
|
||||
u8 * const pEnd = &aData[pPg->pBt->usableSize];
|
||||
u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
|
||||
int nRet = 0;
|
||||
int i;
|
||||
int i, j;
|
||||
int iEnd = iFirst + nCell;
|
||||
u8 *pFree = 0; /* \__ Parameters for pending call to */
|
||||
int szFree = 0; /* / freeSpace() */
|
||||
int nFree = 0;
|
||||
int aOfst[10];
|
||||
int aAfter[10];
|
||||
|
||||
for(i=iFirst; i<iEnd; i++){
|
||||
u8 *pCell = pCArray->apCell[i];
|
||||
if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
|
||||
int sz;
|
||||
int iAfter;
|
||||
int iOfst;
|
||||
/* No need to use cachedCellSize() here. The sizes of all cells that
|
||||
** are to be freed have already been computing while deciding which
|
||||
** cells need freeing */
|
||||
sz = pCArray->szCell[i]; assert( sz>0 );
|
||||
if( pFree!=(pCell + sz) ){
|
||||
if( pFree ){
|
||||
assert( pFree>aData && (pFree - aData)<65536 );
|
||||
freeSpace(pPg, (u16)(pFree - aData), szFree);
|
||||
iOfst = (u16)(pCell - aData);
|
||||
iAfter = iOfst+sz;
|
||||
for(j=0; j<nFree; j++){
|
||||
if( aOfst[j]==iAfter ){
|
||||
aOfst[j] = iOfst;
|
||||
break;
|
||||
}else if( aAfter[j]==iOfst ){
|
||||
aAfter[j] = iAfter;
|
||||
break;
|
||||
}
|
||||
pFree = pCell;
|
||||
szFree = sz;
|
||||
if( pFree+sz>pEnd ){
|
||||
return 0;
|
||||
}
|
||||
if( j>=nFree ){
|
||||
if( nFree>=sizeof(aOfst)/sizeof(aOfst[0]) ){
|
||||
for(j=0; j<nFree; j++){
|
||||
freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
|
||||
}
|
||||
nFree = 0;
|
||||
}
|
||||
}else{
|
||||
/* The current cell is adjacent to and before the pFree cell.
|
||||
** Combine the two regions into one to reduce the number of calls
|
||||
** to freeSpace(). */
|
||||
pFree = pCell;
|
||||
szFree += sz;
|
||||
aOfst[nFree] = iOfst;
|
||||
aAfter[nFree] = iAfter;
|
||||
if( &aData[iAfter]>pEnd ) return 0;
|
||||
nFree++;
|
||||
}
|
||||
nRet++;
|
||||
}
|
||||
}
|
||||
if( pFree ){
|
||||
assert( pFree>aData && (pFree - aData)<65536 );
|
||||
freeSpace(pPg, (u16)(pFree - aData), szFree);
|
||||
for(j=0; j<nFree; j++){
|
||||
freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
|
||||
}
|
||||
return nRet;
|
||||
}
|
||||
@ -9312,7 +9407,7 @@ int sqlite3BtreeInsert(
|
||||
}else{
|
||||
assert( pPage->leaf );
|
||||
}
|
||||
rc = insertCell(pPage, idx, newCell, szNew, 0, 0);
|
||||
rc = insertCellFast(pPage, idx, newCell, szNew);
|
||||
assert( pPage->nOverflow==0 || rc==SQLITE_OK );
|
||||
assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
|
||||
|
||||
|
@ -281,15 +281,22 @@
|
||||
#endif
|
||||
|
||||
/*
|
||||
** A macro to hint to the compiler that a function should not be
|
||||
** Macros to hint to the compiler that a function should or should not be
|
||||
** inlined.
|
||||
*/
|
||||
#if defined(__GNUC__)
|
||||
# define SQLITE_NOINLINE __attribute__((noinline))
|
||||
# define SQLITE_INLINE __attribute__((always_inline)) inline
|
||||
#elif defined(_MSC_VER) && _MSC_VER>=1310
|
||||
# define SQLITE_NOINLINE __declspec(noinline)
|
||||
# define SQLITE_INLINE __forceinline
|
||||
#else
|
||||
# define SQLITE_NOINLINE
|
||||
# define SQLITE_INLINE
|
||||
#endif
|
||||
#if defined(SQLITE_COVERAGE_TEST)
|
||||
# undef SQLITE_INLINE
|
||||
# define SQLITE_INLINE
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
Loading…
x
Reference in New Issue
Block a user