Performance enhancement for sqlite3DbFree().

FossilOrigin-Name: c296a9d958ec360fc8d217363348b4918d665bccb3c4f27503a2dcef7db49052
This commit is contained in:
drh 2022-08-22 15:18:37 +00:00
parent 41ce47c4f4
commit 376860ba88
7 changed files with 45 additions and 32 deletions

View File

@ -1,5 +1,5 @@
C Add\sthe\snew\sinternal\sinterface\ssqlite3DbNNFreeNN(db,ptr)\swhere\sboth\sthe\ndb\sand\sptr\sparameters\sare\sguaranteed\sto\sbe\snon-NULL.\s\sUse\sthis\swhere\nappropriate\sto\ssave\smore\sthan\s2\smillion\sCPU\scycles\son\sthe\sstandard\nperformance\stest. C Performance\senhancement\sfor\ssqlite3DbFree().
D 2022-08-22T02:00:26.214 D 2022-08-22T15:18:37.415
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -552,8 +552,8 @@ F src/insert.c aea5361767817f917b0f0f647a1f0b1621bd858938ae6ae545c3b6b9814b798f
F src/json.c 7749b98c62f691697c7ee536b570c744c0583cab4a89200fdd0fc2aa8cc8cbd6 F src/json.c 7749b98c62f691697c7ee536b570c744c0583cab4a89200fdd0fc2aa8cc8cbd6
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 853385cc7a604157e137585097949252d5d0c731768e16b044608e5c95c3614b F src/loadext.c 853385cc7a604157e137585097949252d5d0c731768e16b044608e5c95c3614b
F src/main.c b91c7e71af6f33640c35b8239a285040aad8dfcfdaaf979152e743c0f8017ea8 F src/main.c 211f5fd4110b0565fc2135d60ed12b4ef8a29d634414f471b5f957aa7b7f2afa
F src/malloc.c 24369414a0fa402451cec0f0d1e94b637ce8e93c93cd29038ffe7629a8780ebf F src/malloc.c b7a3430cbe91d3e8e04fc10c2041b3a19794e63556ad2441a13d8dadd9b2bafc
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
F src/mem1.c c12a42539b1ba105e3707d0e628ad70e611040d8f5e38cf942cee30c867083de F src/mem1.c c12a42539b1ba105e3707d0e628ad70e611040d8f5e38cf942cee30c867083de
F src/mem2.c c8bfc9446fd0798bddd495eb5d9dbafa7d4b7287d8c22d50a83ac9daa26d8a75 F src/mem2.c c8bfc9446fd0798bddd495eb5d9dbafa7d4b7287d8c22d50a83ac9daa26d8a75
@ -593,9 +593,9 @@ F src/shell.c.in 269f682249c1bce2962883e5b99c8702b16a488a43b9ae186daa178713a93c5
F src/sqlite.h.in b9b7fd73239d94db20332bb6e504688001e5564b655e1318a4427a1caef4b99e F src/sqlite.h.in b9b7fd73239d94db20332bb6e504688001e5564b655e1318a4427a1caef4b99e
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h a988810c9b21c0dc36dc7a62735012339dc76fc7ab448fb0792721d30eacb69d F src/sqlite3ext.h a988810c9b21c0dc36dc7a62735012339dc76fc7ab448fb0792721d30eacb69d
F src/sqliteInt.h 51ccf8da51d5263a21543631f0c3bdca06e9fbe8238a1b2c8d7ba4f4cd118aea F src/sqliteInt.h ebf18764e404a2cef39ae5bfc8dd4a83bf0d70be1c444a4fbd8539eb35ef6ffd
F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657 F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657
F src/status.c 4a3da6d77eeb3531cb0dbdf7047772a2a1b99f98c69e90ce009c75fe6328b2c0 F src/status.c 1593e408e8e9ac956e8ecdf7db2728d8aa3e9da3405849f08c0f1f7ee7df86b5
F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1 F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
F src/tclsqlite.c 4e64ba300a5a26e0f1170e09032429faeb65e45e8f3d1a7833e8edb69fc2979e F src/tclsqlite.c 4e64ba300a5a26e0f1170e09032429faeb65e45e8f3d1a7833e8edb69fc2979e
F src/test1.c 1356984e97bff07e4a8cc3863e892f05b3348678a74783bb6f350b76316736f1 F src/test1.c 1356984e97bff07e4a8cc3863e892f05b3348678a74783bb6f350b76316736f1
@ -663,7 +663,7 @@ F src/vacuum.c bb346170b0b54c6683bba4a5983aea40485597fdf605c87ec8bc2e199fe88cd8
F src/vdbe.c 4e57ac969bb2252598024ee3ebcb0885cb7976f1606e2bd77975a506da93ed50 F src/vdbe.c 4e57ac969bb2252598024ee3ebcb0885cb7976f1606e2bd77975a506da93ed50
F src/vdbe.h 64619af62603dc3c4f5ff6ff6d2c8f389abd667a29ce6007ed44bd22b3211cd0 F src/vdbe.h 64619af62603dc3c4f5ff6ff6d2c8f389abd667a29ce6007ed44bd22b3211cd0
F src/vdbeInt.h 2cad0aeeb106371ed0e0946bab89f60627087068847afc2451c05056961c18da F src/vdbeInt.h 2cad0aeeb106371ed0e0946bab89f60627087068847afc2451c05056961c18da
F src/vdbeapi.c 4cfbf7ec3ed60366a38655f3f10316c5a3d68f6d4d06e462f88679392611c756 F src/vdbeapi.c 6af7b32ada7ef8facafa91f57aedabd91633f254b34e9c0901ab402d4238e553
F src/vdbeaux.c cd9fc2fb24755366b2718f5e9e02103c39804bdc9e630620aaec08bb3093c46c F src/vdbeaux.c cd9fc2fb24755366b2718f5e9e02103c39804bdc9e630620aaec08bb3093c46c
F src/vdbeblob.c 5e61ce31aca17db8fb60395407457a8c1c7fb471dde405e0cd675974611dcfcd F src/vdbeblob.c 5e61ce31aca17db8fb60395407457a8c1c7fb471dde405e0cd675974611dcfcd
F src/vdbemem.c c3ce80af15e2ff5c2824a8db881681cbf511376f13613da020bac6d320c535b1 F src/vdbemem.c c3ce80af15e2ff5c2824a8db881681cbf511376f13613da020bac6d320c535b1
@ -1999,8 +1999,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 2d13ec086e96a5446462ce0f689c40c8196e740cd693f5967bfe9eb961f03463 P e5eaa80e81fdf86f2875a912b880272b8d099b82b08e945a7988c5dd0fe9d6b5
R bf88e8a14ba34da9f9b58cc1ee676092 R 5d3ac911bfb51b81f7438edbe16f7f7d
U drh U drh
Z 1f007ec3ec70bab2436c890728aeb636 Z 2b49dc0de08069abebcad82ed9e5f9e0
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
e5eaa80e81fdf86f2875a912b880272b8d099b82b08e945a7988c5dd0fe9d6b5 c296a9d958ec360fc8d217363348b4918d665bccb3c4f27503a2dcef7db49052

View File

@ -824,18 +824,19 @@ static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
db->lookaside.bMalloced = pBuf==0 ?1:0; db->lookaside.bMalloced = pBuf==0 ?1:0;
db->lookaside.nSlot = nBig+nSm; db->lookaside.nSlot = nBig+nSm;
}else{ }else{
db->lookaside.pStart = db; db->lookaside.pStart = 0;
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
db->lookaside.pSmallInit = 0; db->lookaside.pSmallInit = 0;
db->lookaside.pSmallFree = 0; db->lookaside.pSmallFree = 0;
db->lookaside.pMiddle = db; db->lookaside.pMiddle = 0;
#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
db->lookaside.pEnd = db; db->lookaside.pEnd = 0;
db->lookaside.bDisable = 1; db->lookaside.bDisable = 1;
db->lookaside.sz = 0; db->lookaside.sz = 0;
db->lookaside.bMalloced = 0; db->lookaside.bMalloced = 0;
db->lookaside.nSlot = 0; db->lookaside.nSlot = 0;
} }
db->lookaside.pTrueEnd = db->lookaside.pEnd;
assert( sqlite3LookasideUsed(db,0)==0 ); assert( sqlite3LookasideUsed(db,0)==0 );
#endif /* SQLITE_OMIT_LOOKASIDE */ #endif /* SQLITE_OMIT_LOOKASIDE */
return SQLITE_OK; return SQLITE_OK;

View File

@ -317,7 +317,7 @@ void *sqlite3_malloc64(sqlite3_uint64 n){
*/ */
#ifndef SQLITE_OMIT_LOOKASIDE #ifndef SQLITE_OMIT_LOOKASIDE
static int isLookaside(sqlite3 *db, const void *p){ static int isLookaside(sqlite3 *db, const void *p){
return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
} }
#else #else
#define isLookaside(A,B) 0 #define isLookaside(A,B) 0
@ -341,18 +341,16 @@ static int lookasideMallocSize(sqlite3 *db, const void *p){
int sqlite3DbMallocSize(sqlite3 *db, const void *p){ int sqlite3DbMallocSize(sqlite3 *db, const void *p){
assert( p!=0 ); assert( p!=0 );
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
if( db==0 || !isLookaside(db,p) ){ if( db==0 ){
if( db==0 ){ assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) );
assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); }else if( !isLookaside(db,p) ){
}else{ assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
}
} }
#endif #endif
if( db ){ if( db ){
if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
assert( sqlite3_mutex_held(db->mutex) ); assert( sqlite3_mutex_held(db->mutex) );
@ -408,14 +406,11 @@ void sqlite3DbFreeNN(sqlite3 *db, void *p){
assert( db==0 || sqlite3_mutex_held(db->mutex) ); assert( db==0 || sqlite3_mutex_held(db->mutex) );
assert( p!=0 ); assert( p!=0 );
if( db ){ if( db ){
if( db->pnBytesFreed ){
measureAllocationSize(db, p);
return;
}
if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
LookasideSlot *pBuf = (LookasideSlot*)p; LookasideSlot *pBuf = (LookasideSlot*)p;
assert( db->pnBytesFreed==0 );
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */ memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
#endif #endif
@ -426,6 +421,7 @@ void sqlite3DbFreeNN(sqlite3 *db, void *p){
#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
LookasideSlot *pBuf = (LookasideSlot*)p; LookasideSlot *pBuf = (LookasideSlot*)p;
assert( db->pnBytesFreed==0 );
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
#endif #endif
@ -434,6 +430,10 @@ void sqlite3DbFreeNN(sqlite3 *db, void *p){
return; return;
} }
} }
if( db->pnBytesFreed ){
measureAllocationSize(db, p);
return;
}
} }
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
@ -445,14 +445,11 @@ void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
assert( db!=0 ); assert( db!=0 );
assert( sqlite3_mutex_held(db->mutex) ); assert( sqlite3_mutex_held(db->mutex) );
assert( p!=0 ); assert( p!=0 );
if( db->pnBytesFreed ){
measureAllocationSize(db, p);
return;
}
if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
LookasideSlot *pBuf = (LookasideSlot*)p; LookasideSlot *pBuf = (LookasideSlot*)p;
assert( db->pnBytesFreed==0 );
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */ memset(p, 0xaa, LOOKASIDE_SMALL); /* Trash freed content */
#endif #endif
@ -463,6 +460,7 @@ void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
LookasideSlot *pBuf = (LookasideSlot*)p; LookasideSlot *pBuf = (LookasideSlot*)p;
assert( db->pnBytesFreed==0 );
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
#endif #endif
@ -471,6 +469,10 @@ void sqlite3DbNNFreeNN(sqlite3 *db, void *p){
return; return;
} }
} }
if( db->pnBytesFreed ){
measureAllocationSize(db, p);
return;
}
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
sqlite3MemdebugSetType(p, MEMTYPE_HEAP); sqlite3MemdebugSetType(p, MEMTYPE_HEAP);

View File

@ -1438,6 +1438,7 @@ struct Lookaside {
#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
void *pStart; /* First byte of available memory space */ void *pStart; /* First byte of available memory space */
void *pEnd; /* First byte past end of available space */ void *pEnd; /* First byte past end of available space */
void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
}; };
struct LookasideSlot { struct LookasideSlot {
LookasideSlot *pNext; /* Next buffer in the list of free buffers */ LookasideSlot *pNext; /* Next buffer in the list of free buffers */

View File

@ -291,6 +291,8 @@ int sqlite3_db_status(
sqlite3BtreeEnterAll(db); sqlite3BtreeEnterAll(db);
db->pnBytesFreed = &nByte; db->pnBytesFreed = &nByte;
assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
db->lookaside.pEnd = db->lookaside.pStart;
for(i=0; i<db->nDb; i++){ for(i=0; i<db->nDb; i++){
Schema *pSchema = db->aDb[i].pSchema; Schema *pSchema = db->aDb[i].pSchema;
if( ALWAYS(pSchema!=0) ){ if( ALWAYS(pSchema!=0) ){
@ -316,6 +318,7 @@ int sqlite3_db_status(
} }
} }
db->pnBytesFreed = 0; db->pnBytesFreed = 0;
db->lookaside.pEnd = db->lookaside.pTrueEnd;
sqlite3BtreeLeaveAll(db); sqlite3BtreeLeaveAll(db);
*pHighwater = 0; *pHighwater = 0;
@ -333,9 +336,12 @@ int sqlite3_db_status(
int nByte = 0; /* Used to accumulate return value */ int nByte = 0; /* Used to accumulate return value */
db->pnBytesFreed = &nByte; db->pnBytesFreed = &nByte;
assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
db->lookaside.pEnd = db->lookaside.pStart;
for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
sqlite3VdbeDelete(pVdbe); sqlite3VdbeDelete(pVdbe);
} }
db->lookaside.pEnd = db->lookaside.pTrueEnd;
db->pnBytesFreed = 0; db->pnBytesFreed = 0;
*pHighwater = 0; /* IMP: R-64479-57858 */ *pHighwater = 0; /* IMP: R-64479-57858 */

View File

@ -1836,8 +1836,11 @@ int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
sqlite3_mutex_enter(db->mutex); sqlite3_mutex_enter(db->mutex);
v = 0; v = 0;
db->pnBytesFreed = (int*)&v; db->pnBytesFreed = (int*)&v;
assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
db->lookaside.pEnd = db->lookaside.pStart;
sqlite3VdbeDelete(pVdbe); sqlite3VdbeDelete(pVdbe);
db->pnBytesFreed = 0; db->pnBytesFreed = 0;
db->lookaside.pEnd = db->lookaside.pTrueEnd;
sqlite3_mutex_leave(db->mutex); sqlite3_mutex_leave(db->mutex);
}else{ }else{
v = pVdbe->aCounter[op]; v = pVdbe->aCounter[op];