Allocate Parse objects off of the stack where appropriate for a substantial

performance increase and a size reduction.

FossilOrigin-Name: ea8affa9e453b201b479162f621b591e7a65a489
This commit is contained in:
drh 2016-10-03 01:21:51 +00:00
parent 1a7df58c1a
commit cb43a937e5
6 changed files with 53 additions and 71 deletions

View File

@ -1,5 +1,5 @@
C Add\sSQLITE_USE_ALLOCA\sto\sthe\s--lean\sconfiguration\sin\sspeed-check.sh.
D 2016-10-01T23:55:23.573
C Allocate\sParse\sobjects\soff\sof\sthe\sstack\swhere\sappropriate\sfor\sa\ssubstantial\nperformance\sincrease\sand\sa\ssize\sreduction.
D 2016-10-03T01:21:51.273
F Makefile.in 6fd48ffcf7c2deea7499062d1f3747f986c19678
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc 5151cc64c4c05f3455f4f692ad11410a810d937f
@ -326,10 +326,10 @@ F src/alter.c 299117695b1f21ac62dfc5b608588810ba22ed0d
F src/analyze.c 8b62b2cf4da85451534ac0af82cafc418d837f68
F src/attach.c 8c19066b4b5357b5d66154e856c61df01e71203a
F src/auth.c 930b376a9c56998557367e6f7f8aaeac82a2a792
F src/backup.c 92c2e3b5fcb47626413717138617f4d32f08aea4
F src/backup.c faf17e60b43233c214aae6a8179d24503a61e83b
F src/bitvec.c 3ee4c8b2c94ed3a7377256e18199e6ff5cf33f63
F src/btmutex.c bc87dd3b062cc26edfe79918de2200ccb8d41e73
F src/btree.c 56e1732ecfb3731efcb216266ec26b1b96e5e8c9
F src/btree.c a0a80fbbb34a35ca02cd829bad693b7854bee313
F src/btree.h d05b2fcc290991a8a3d9ea1816ddd55a4359dcde
F src/btreeInt.h c18b7d2a3494695133e4e60ee36061d37f45d9a5
F src/build.c 59dcfdc1ee55439d069af301ef7f2e84421b5102
@ -381,7 +381,7 @@ F src/pcache.h 2cedcd8407eb23017d92790b112186886e179490
F src/pcache1.c 4bb7a6a5300c67d0b033d25adb509c120c03e812
F src/pragma.c d932ba278654617cdd281f88a790a3185fca7c44
F src/pragma.h 64c78a648751b9f4f297276c4eb7507b14b4628c
F src/prepare.c 3c8b1bc8799a794aa2e825db1a977e93487b01d4
F src/prepare.c b1140c3d0cf59bc85ace00ce363153041b424b7a
F src/printf.c a5f0ca08ddede803c241266abb46356ec748ded1
F src/random.c ba2679f80ec82c4190062d756f22d0c358180696
F src/resolve.c 3c3cf0dc719cd2a32ab5c1e10c26481dd565492e
@ -469,7 +469,7 @@ F src/wal.h 6dd221ed384afdc204bc61e25c23ef7fd5a511f2
F src/walker.c 91a6df7435827e41cff6bb7df50ea00934ee78b0
F src/where.c 5f91be9fe122e847c4e72d54d3989eb32a927981
F src/whereInt.h 14dd243e13b81cbb0a66063d38b70f93a7d6e613
F src/wherecode.c e412e09abad1eea213d85594cf46db9f877db56d
F src/wherecode.c 81da7e9b1bf1a882f3ee60b96b750322b83f17fd
F src/whereexpr.c 3d8813ef320f10bd27e6c0d784abe1669cfb514e
F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd
@ -1525,7 +1525,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 361940b44dd17bf2b39fc0e0716c0de6b2b7f4f7
R 4f860bb3856935aeca97748386fce380
P fe07609efc31c4639c40fbda501c55f443756ad2
R 23db76cc6247870f7cdbd64de85d5b65
U drh
Z ab4e3dcba96bb867e3f78f3c2db73961
Z 76db15deb9298331c9d3d6c6e8ebd767

View File

@ -1 +1 @@
fe07609efc31c4639c40fbda501c55f443756ad2
ea8affa9e453b201b479162f621b591e7a65a489

View File

@ -83,22 +83,16 @@ static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
int i = sqlite3FindDbName(pDb, zDb);
if( i==1 ){
Parse *pParse;
Parse sParse;
int rc = 0;
pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
if( pParse==0 ){
sqlite3ErrorWithMsg(pErrorDb, SQLITE_NOMEM, "out of memory");
rc = SQLITE_NOMEM_BKPT;
}else{
pParse->db = pDb;
if( sqlite3OpenTempDatabase(pParse) ){
sqlite3ErrorWithMsg(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
memset(&sParse, 0, sizeof(sParse));
sParse.db = pDb;
if( sqlite3OpenTempDatabase(&sParse) ){
sqlite3ErrorWithMsg(pErrorDb, sParse.rc, "%s", sParse.zErrMsg);
rc = SQLITE_ERROR;
}
sqlite3DbFree(pErrorDb, pParse->zErrMsg);
sqlite3ParserReset(pParse);
sqlite3StackFree(pErrorDb, pParse);
}
sqlite3DbFree(pErrorDb, sParse.zErrMsg);
sqlite3ParserReset(&sParse);
if( rc ){
return 0;
}

View File

@ -763,7 +763,7 @@ static int btreeMoveto(
){
int rc; /* Status code */
UnpackedRecord *pIdxKey; /* Unpacked index key */
char aSpace[200]; /* Temp space for pIdxKey - to avoid a malloc */
char aSpace[384]; /* Temp space for pIdxKey - to avoid a malloc */
char *pFree = 0;
if( pKey ){

View File

@ -518,20 +518,14 @@ static int sqlite3Prepare(
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
const char **pzTail /* OUT: End of parsed string */
){
Parse *pParse; /* Parsing context */
char *zErrMsg = 0; /* Error message */
int rc = SQLITE_OK; /* Result code */
int i; /* Loop counter */
Parse sParse; /* Parsing context */
/* Allocate the parsing context */
pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
if( pParse==0 ){
rc = SQLITE_NOMEM_BKPT;
goto end_prepare;
}
memset(pParse, 0, PARSE_HDR_SZ);
memset(PARSE_TAIL(pParse), 0, PARSE_TAIL_SZ);
pParse->pReprepare = pReprepare;
memset(&sParse, 0, PARSE_HDR_SZ);
memset(PARSE_TAIL(&sParse), 0, PARSE_TAIL_SZ);
sParse.pReprepare = pReprepare;
assert( ppStmt && *ppStmt==0 );
/* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
assert( sqlite3_mutex_held(db->mutex) );
@ -575,8 +569,7 @@ static int sqlite3Prepare(
sqlite3VtabUnlockList(db);
pParse->db = db;
pParse->nQueryLoop = 0; /* Logarithmic, so 0 really means 1 */
sParse.db = db;
if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
char *zSqlCopy;
int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
@ -589,61 +582,61 @@ static int sqlite3Prepare(
}
zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
if( zSqlCopy ){
sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
sqlite3RunParser(&sParse, zSqlCopy, &zErrMsg);
sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
sqlite3DbFree(db, zSqlCopy);
}else{
pParse->zTail = &zSql[nBytes];
sParse.zTail = &zSql[nBytes];
}
}else{
sqlite3RunParser(pParse, zSql, &zErrMsg);
sqlite3RunParser(&sParse, zSql, &zErrMsg);
}
assert( 0==pParse->nQueryLoop );
assert( 0==sParse.nQueryLoop );
if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
if( pParse->checkSchema ){
schemaIsValid(pParse);
if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
if( sParse.checkSchema ){
schemaIsValid(&sParse);
}
if( db->mallocFailed ){
pParse->rc = SQLITE_NOMEM_BKPT;
sParse.rc = SQLITE_NOMEM_BKPT;
}
if( pzTail ){
*pzTail = pParse->zTail;
*pzTail = sParse.zTail;
}
rc = pParse->rc;
rc = sParse.rc;
#ifndef SQLITE_OMIT_EXPLAIN
if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
static const char * const azColName[] = {
"addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
"selectid", "order", "from", "detail"
};
int iFirst, mx;
if( pParse->explain==2 ){
sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
if( sParse.explain==2 ){
sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
iFirst = 8;
mx = 12;
}else{
sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
sqlite3VdbeSetNumCols(sParse.pVdbe, 8);
iFirst = 0;
mx = 8;
}
for(i=iFirst; i<mx; i++){
sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
sqlite3VdbeSetColName(sParse.pVdbe, i-iFirst, COLNAME_NAME,
azColName[i], SQLITE_STATIC);
}
}
#endif
if( db->init.busy==0 ){
Vdbe *pVdbe = pParse->pVdbe;
sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
Vdbe *pVdbe = sParse.pVdbe;
sqlite3VdbeSetSql(pVdbe, zSql, (int)(sParse.zTail-zSql), saveSqlFlag);
}
if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
sqlite3VdbeFinalize(pParse->pVdbe);
if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
sqlite3VdbeFinalize(sParse.pVdbe);
assert(!(*ppStmt));
}else{
*ppStmt = (sqlite3_stmt*)pParse->pVdbe;
*ppStmt = (sqlite3_stmt*)sParse.pVdbe;
}
if( zErrMsg ){
@ -654,16 +647,15 @@ static int sqlite3Prepare(
}
/* Delete any TriggerPrg structures allocated while parsing this statement. */
while( pParse->pTriggerPrg ){
TriggerPrg *pT = pParse->pTriggerPrg;
pParse->pTriggerPrg = pT->pNext;
while( sParse.pTriggerPrg ){
TriggerPrg *pT = sParse.pTriggerPrg;
sParse.pTriggerPrg = pT->pNext;
sqlite3DbFree(db, pT);
}
end_prepare:
sqlite3ParserReset(pParse);
sqlite3StackFree(db, pParse);
sqlite3ParserReset(&sParse);
rc = sqlite3ApiExit(db, rc);
assert( (rc&db->errMask)==rc );
return rc;

View File

@ -1987,7 +1987,7 @@ Bitmask sqlite3WhereCodeOneLoopStart(
** the implied "t1.a=123" constraint.
*/
for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
Expr *pE, *pEAlt;
Expr *pE, sEAlt;
WhereTerm *pAlt;
if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
@ -2005,13 +2005,9 @@ Bitmask sqlite3WhereCodeOneLoopStart(
testcase( pAlt->eOperator & WO_IS );
testcase( pAlt->eOperator & WO_IN );
VdbeModuleComment((v, "begin transitive constraint"));
pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
if( pEAlt ){
*pEAlt = *pAlt->pExpr;
pEAlt->pLeft = pE->pLeft;
sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
sqlite3StackFree(db, pEAlt);
}
sEAlt = *pAlt->pExpr;
sEAlt.pLeft = pE->pLeft;
sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
}
/* For a LEFT OUTER JOIN, generate code that will record the fact that