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

Reduce the overhead of SQLITE_ENABLE_STMT_SCANSTATUS some.

FossilOrigin-Name: 212927e97e7be7d237de08359dce0dfb9211ac406b32009a6e15afd79c006475
This commit is contained in:
dan
2022-12-07 20:09:54 +00:00
parent 209dbab997
commit 7f4b066eb2
7 changed files with 51 additions and 77 deletions

View File

@@ -1,5 +1,5 @@
C Fix\sharmless\scompiler\swarning\sin\sthe\sdynamic\scontinuation\sprompt\sof\sthe\sCLI. C Reduce\sthe\soverhead\sof\sSQLITE_ENABLE_STMT_SCANSTATUS\ssome.
D 2022-12-07T19:51:48.626 D 2022-12-07T20:09:54.641
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
@@ -717,11 +717,11 @@ F src/upsert.c 5303dc6c518fa7d4b280ec65170f465c7a70b7ac2b22491598f6d0b4875b3145
F src/utf.c ee39565f0843775cc2c81135751ddd93eceb91a673ea2c57f61c76f288b041a0 F src/utf.c ee39565f0843775cc2c81135751ddd93eceb91a673ea2c57f61c76f288b041a0
F src/util.c 313f3154e2b85a447326f5dd15de8d31a4df6ab0c3579bd58f426ff634ec9050 F src/util.c 313f3154e2b85a447326f5dd15de8d31a4df6ab0c3579bd58f426ff634ec9050
F src/vacuum.c 84ce7f01f8a7a08748e107a441db83bcec13970190ddcb0c9ff522adbc1c23fd F src/vacuum.c 84ce7f01f8a7a08748e107a441db83bcec13970190ddcb0c9ff522adbc1c23fd
F src/vdbe.c 8fdef91d3c9bfa363e8eb381d362e2f0cadec456975ec5595c0bfdf65da84e53 F src/vdbe.c df9ff3fcaa3c60fa9b0bca340c3f3cfc63a51361901ca06fe18bdeadeb2a1a24
F src/vdbe.h 6d921884cf8ec6a53efba99f8b68e32e955367631743e29039840e781aaf547c F src/vdbe.h 73b904a6b3bb27f308c6cc287a5751ebc7f1f89456be0ed068a12b92844c6e8c
F src/vdbeInt.h e83be1eea422758d42302941e96e59d93193c373da655a87befdc03a851e0f95 F src/vdbeInt.h 8651e4c4e04d1860d0bdcf330cb8294e3778a9d4222be30ce4c490d9220af783
F src/vdbeapi.c 5e265ab5165b0dfeac55e6a2ad3929d64584c4e7ca106b438137d7596da91348 F src/vdbeapi.c df3f73a4d0a487f2068e3c84776cd6e3fba5ae80ff612659dcfda4307686420b
F src/vdbeaux.c 0b81f317c86ed9f4ba822af66a52777fed6e8180edc79d4fc62ffe75c8e52d80 F src/vdbeaux.c 25691b395bf57ae0a754570fc3ba2d6f8bd2f0a8944f1d2589b9e68114e01c2b
F src/vdbeblob.c 5e61ce31aca17db8fb60395407457a8c1c7fb471dde405e0cd675974611dcfcd F src/vdbeblob.c 5e61ce31aca17db8fb60395407457a8c1c7fb471dde405e0cd675974611dcfcd
F src/vdbemem.c 6cfed43758d57b6e3b99d9cdedfeccd86e45a07e427b22d8487cbdbebb6c522a F src/vdbemem.c 6cfed43758d57b6e3b99d9cdedfeccd86e45a07e427b22d8487cbdbebb6c522a
F src/vdbesort.c 43756031ca7430f7aec3ef904824a7883c4ede783e51f280d99b9b65c0796e35 F src/vdbesort.c 43756031ca7430f7aec3ef904824a7883c4ede783e51f280d99b9b65c0796e35
@@ -2067,8 +2067,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 976c23520fa64b39aa54047f2fd12445e65940206768ce51c0d690d0d0d570ab P 68947b0a1147365a29e335d5e4dc55fe5d9afe3562a0709b111067d6017c5a42
R b08d7359f3a0adfc8758cdabe6250a50 R 4a013de93da29e8129599899cb5c1b54
U drh U dan
Z 38d73aa067c1a85ec056e9ed86a84d61 Z 4aecefd86ed6f42fbe2469d178e09d33
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
68947b0a1147365a29e335d5e4dc55fe5d9afe3562a0709b111067d6017c5a42 212927e97e7be7d237de08359dce0dfb9211ac406b32009a6e15afd79c006475

View File

@@ -790,15 +790,12 @@ int sqlite3VdbeExec(
assert( pOp>=aOp && pOp<&aOp[p->nOp]); assert( pOp>=aOp && pOp<&aOp[p->nOp]);
nVmStep++; nVmStep++;
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE) #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
if( p->anExec ){ pOp->nExec++;
assert( p->anExec && p->anCycle ); pnCycle = &pOp->nCycle;
p->anExec[(int)(pOp-aOp)]++;
pnCycle = &p->anCycle[pOp-aOp];
# ifdef VDBE_PROFILE # ifdef VDBE_PROFILE
if( sqlite3NProfileCnt==0 ) if( sqlite3NProfileCnt==0 )
# endif # endif
*pnCycle -= sqlite3Hwtime(); *pnCycle -= sqlite3Hwtime();
}
#endif #endif
/* Only allow tracing if SQLITE_DEBUG is defined. /* Only allow tracing if SQLITE_DEBUG is defined.
@@ -7152,10 +7149,6 @@ case OP_Program: { /* jump */
pFrame->aOp = p->aOp; pFrame->aOp = p->aOp;
pFrame->nOp = p->nOp; pFrame->nOp = p->nOp;
pFrame->token = pProgram->token; pFrame->token = pProgram->token;
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
pFrame->anExec = p->anExec;
pFrame->anCycle = p->anCycle;
#endif
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
pFrame->iFrameMagic = SQLITE_FRAME_MAGIC; pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
#endif #endif
@@ -7192,10 +7185,6 @@ case OP_Program: { /* jump */
memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8); memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
p->aOp = aOp = pProgram->aOp; p->aOp = aOp = pProgram->aOp;
p->nOp = pProgram->nOp; p->nOp = pProgram->nOp;
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
p->anExec = 0;
p->anCycle = 0;
#endif
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
/* Verify that second and subsequent executions of the same trigger do not /* Verify that second and subsequent executions of the same trigger do not
** try to reuse register values from the first use. */ ** try to reuse register values from the first use. */
@@ -8732,17 +8721,11 @@ default: { /* This is really OP_Noop, OP_Explain */
} }
#if defined(VDBE_PROFILE) #if defined(VDBE_PROFILE)
assert( pnCycle ); *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
if( pnCycle ){ pnCycle = 0;
*pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
assert( *pnCycle < (((u64)1) << 48) );
pnCycle = 0;
}
#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
if( pnCycle ){ *pnCycle += sqlite3Hwtime();
*pnCycle += sqlite3Hwtime(); pnCycle = 0;
pnCycle = 0;
}
#endif #endif
/* The following code adds nothing to the actual functionality /* The following code adds nothing to the actual functionality
@@ -8819,11 +8802,10 @@ abort_due_to_error:
** top. */ ** top. */
vdbe_return: vdbe_return:
#if defined(VDBE_PROFILE) #if defined(VDBE_PROFILE)
if( pnCycle ){ if( pnCycle ){
*pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime(); *pnCycle += sqlite3NProfileCnt ? sqlite3NProfileCnt : sqlite3Hwtime();
assert( *pnCycle < (((u64)1) << 48) ); pnCycle = 0;
pnCycle = 0; }
}
#elif defined(SQLITE_ENABLE_STMT_SCANSTATUS) #elif defined(SQLITE_ENABLE_STMT_SCANSTATUS)
if( pnCycle ){ if( pnCycle ){
*pnCycle += sqlite3Hwtime(); *pnCycle += sqlite3Hwtime();

View File

@@ -71,6 +71,10 @@ struct VdbeOp {
u32 iSrcLine; /* Source-code line that generated this opcode u32 iSrcLine; /* Source-code line that generated this opcode
** with flags in the upper 8 bits */ ** with flags in the upper 8 bits */
#endif #endif
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
u64 nExec;
u64 nCycle;
#endif
}; };
typedef struct VdbeOp VdbeOp; typedef struct VdbeOp VdbeOp;

View File

@@ -171,8 +171,6 @@ struct VdbeFrame {
Vdbe *v; /* VM this frame belongs to */ Vdbe *v; /* VM this frame belongs to */
VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */ VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
Op *aOp; /* Program instructions for parent frame */ Op *aOp; /* Program instructions for parent frame */
i64 *anExec; /* Event counters from parent frame */
u64 *anCycle; /* Cycle counters from parent frame */
Mem *aMem; /* Array of memory cells for parent frame */ Mem *aMem; /* Array of memory cells for parent frame */
VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */ VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
u8 *aOnce; /* Bitmask used by OP_Once */ u8 *aOnce; /* Bitmask used by OP_Once */
@@ -492,10 +490,6 @@ struct Vdbe {
u32 expmask; /* Binding to these vars invalidates VM */ u32 expmask; /* Binding to these vars invalidates VM */
SubProgram *pProgram; /* Linked list of all sub-programs used by VM */ SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
AuxData *pAuxData; /* Linked list of auxdata allocations */ AuxData *pAuxData; /* Linked list of auxdata allocations */
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
i64 *anExec; /* Number of times each op has been executed */
u64 *anCycle; /* Sum of sqlite3HwTime() spent in each opcode */
#endif
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
int nScan; /* Entries in aScan[] */ int nScan; /* Entries in aScan[] */
ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */ ScanStatus *aScan; /* Scan definitions for sqlite3_stmt_scanstatus() */

View File

@@ -2123,14 +2123,12 @@ int sqlite3_stmt_scanstatus_v2(
ScanStatus *pScan; ScanStatus *pScan;
int idx; int idx;
/* If the v2 flag is clear, then this function must ignore any ScanStatus
** structures with ScanStatus.addrLoop set to 0. */
if( iScan<0 ){ if( iScan<0 ){
int ii; int ii;
if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){ if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){
i64 res = 0; i64 res = 0;
for(ii=0; ii<p->nOp; ii++){ for(ii=0; ii<p->nOp; ii++){
res += p->anCycle[ii]; res += p->aOp[ii].nCycle;
} }
*(i64*)pOut = res; *(i64*)pOut = res;
return 0; return 0;
@@ -2141,6 +2139,8 @@ int sqlite3_stmt_scanstatus_v2(
idx = iScan; idx = iScan;
pScan = &p->aScan[idx]; pScan = &p->aScan[idx];
}else{ }else{
/* If the COMPLEX flag is clear, then this function must ignore any
** ScanStatus structures with ScanStatus.addrLoop set to 0. */
for(idx=0; idx<p->nScan; idx++){ for(idx=0; idx<p->nScan; idx++){
pScan = &p->aScan[idx]; pScan = &p->aScan[idx];
if( pScan->zName ){ if( pScan->zName ){
@@ -2154,7 +2154,7 @@ int sqlite3_stmt_scanstatus_v2(
switch( iScanStatusOp ){ switch( iScanStatusOp ){
case SQLITE_SCANSTAT_NLOOP: { case SQLITE_SCANSTAT_NLOOP: {
if( pScan->addrLoop>0 ){ if( pScan->addrLoop>0 ){
*(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; *(sqlite3_int64*)pOut = p->aOp[pScan->addrLoop].nExec;
}else{ }else{
*(sqlite3_int64*)pOut = -1; *(sqlite3_int64*)pOut = -1;
} }
@@ -2162,7 +2162,7 @@ int sqlite3_stmt_scanstatus_v2(
} }
case SQLITE_SCANSTAT_NVISIT: { case SQLITE_SCANSTAT_NVISIT: {
if( pScan->addrVisit>0 ){ if( pScan->addrVisit>0 ){
*(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; *(sqlite3_int64*)pOut = p->aOp[pScan->addrVisit].nExec;
}else{ }else{
*(sqlite3_int64*)pOut = -1; *(sqlite3_int64*)pOut = -1;
} }
@@ -2218,7 +2218,7 @@ int sqlite3_stmt_scanstatus_v2(
if( iIns==0 ) break; if( iIns==0 ) break;
if( iIns>0 ){ if( iIns>0 ){
while( iIns<=iEnd ){ while( iIns<=iEnd ){
res += p->anCycle[iIns]; res += p->aOp[iIns].nCycle;
iIns++; iIns++;
} }
}else{ }else{
@@ -2229,7 +2229,7 @@ int sqlite3_stmt_scanstatus_v2(
if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){ if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){
continue; continue;
} }
res += p->anCycle[iOp]; res += p->aOp[iOp].nCycle;
} }
} }
} }
@@ -2261,7 +2261,11 @@ int sqlite3_stmt_scanstatus(
*/ */
void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){
Vdbe *p = (Vdbe*)pStmt; Vdbe *p = (Vdbe*)pStmt;
memset(p->anExec, 0, p->nOp * sizeof(i64)); int ii;
memset(p->anCycle, 0, p->nOp * sizeof(u64)); for(ii=0; ii<p->nOp; ii++){
Op *pOp = &p->aOp[ii];
pOp->nExec = 0;
pOp->nCycle = 0;
}
} }
#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */

View File

@@ -262,6 +262,10 @@ int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
pOp->zComment = 0; pOp->zComment = 0;
#endif #endif
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
pOp->nExec = 0;
pOp->nCycle = 0;
#endif
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
if( p->db->flags & SQLITE_VdbeAddopTrace ){ if( p->db->flags & SQLITE_VdbeAddopTrace ){
sqlite3VdbePrintOp(0, i, &p->aOp[i]); sqlite3VdbePrintOp(0, i, &p->aOp[i]);
@@ -2508,8 +2512,10 @@ void sqlite3VdbeRewind(Vdbe *p){
p->iStatement = 0; p->iStatement = 0;
p->nFkConstraint = 0; p->nFkConstraint = 0;
#ifdef VDBE_PROFILE #ifdef VDBE_PROFILE
memset(p->anExec, 0, sizeof(i64)*p->nOp); for(i=0; i<p->nOp; i++){
memset(p->anCycle, 0, sizeof(u64)*p->nOp); p->aOp[i].nExec = 0;
p->aOp[i].nCycle = 0;
}
#endif #endif
} }
@@ -2617,10 +2623,6 @@ void sqlite3VdbeMakeReady(
p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem)); p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*)); p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*)); p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
p->anCycle = allocSpace(&x, 0, p->nOp*sizeof(u64));
#endif
if( x.nNeeded ){ if( x.nNeeded ){
x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded); x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
x.nFree = x.nNeeded; x.nFree = x.nNeeded;
@@ -2629,10 +2631,6 @@ void sqlite3VdbeMakeReady(
p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem)); p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*)); p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*)); p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
p->anCycle = allocSpace(&x, p->anCycle, p->nOp*sizeof(u64));
#endif
} }
} }
@@ -2647,10 +2645,6 @@ void sqlite3VdbeMakeReady(
p->nMem = nMem; p->nMem = nMem;
initMemArray(p->aMem, nMem, db, MEM_Undefined); initMemArray(p->aMem, nMem, db, MEM_Undefined);
memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*)); memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
memset(p->anExec, 0, p->nOp*sizeof(i64));
memset(p->anCycle, 0, p->nOp*sizeof(u64));
#endif
} }
sqlite3VdbeRewind(p); sqlite3VdbeRewind(p);
} }
@@ -2708,10 +2702,6 @@ static void closeCursorsInFrame(Vdbe *p){
int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
Vdbe *v = pFrame->v; Vdbe *v = pFrame->v;
closeCursorsInFrame(v); closeCursorsInFrame(v);
#if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || defined(VDBE_PROFILE)
v->anExec = pFrame->anExec;
v->anCycle = pFrame->anCycle;
#endif
v->aOp = pFrame->aOp; v->aOp = pFrame->aOp;
v->nOp = pFrame->nOp; v->nOp = pFrame->nOp;
v->aMem = pFrame->aMem; v->aMem = pFrame->aMem;
@@ -3543,8 +3533,8 @@ int sqlite3VdbeReset(Vdbe *p){
} }
for(i=0; i<p->nOp; i++){ for(i=0; i<p->nOp; i++){
char zHdr[100]; char zHdr[100];
i64 cnt = p->anExec[i]; i64 cnt = p->aOp[i].nExec;
i64 cycles = p->anCycle[i]; i64 cycles = p->aOp[i].nCycle;
sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ", sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
cnt, cnt,
cycles, cycles,