1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-12 13:01:09 +03:00

Attempt to factor out constant functions from the interior of table scans,

since functions can often be expensive to compute.

FossilOrigin-Name: 62e9270a8057d758621da33adb27fad14225f95d
This commit is contained in:
drh
2017-01-04 01:07:24 +00:00
parent a3a40211bd
commit 1e9b53f9bc
4 changed files with 38 additions and 23 deletions

View File

@@ -3612,6 +3612,9 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
u8 enc = ENC(db); /* The text encoding used by this database */
CollSeq *pColl = 0; /* A collating sequence */
if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
return sqlite3ExprCodeAtInit(pParse, pExpr, -1, 1);
}
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
if( ExprHasProperty(pExpr, EP_TokenOnly) ){
pFarg = 0;
@@ -3992,8 +3995,16 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
/*
** Factor out the code of the given expression to initialization time.
**
** If regDest>=0 then the result is always stored in that register.
** If regDest<0 then this routine is free to store the value whereever
** it wants. The register where the expression is stored is returned.
**
** If regDest<0 and an equivalent expression already exists elsewhere
** in the initialization expressions, then routine just returns the
** register of the prior occurrance.
*/
void sqlite3ExprCodeAtInit(
int sqlite3ExprCodeAtInit(
Parse *pParse, /* Parsing context */
Expr *pExpr, /* The expression to code when the VDBE initializes */
int regDest, /* Store the value in this register */
@@ -4002,6 +4013,18 @@ void sqlite3ExprCodeAtInit(
ExprList *p;
assert( ConstFactorOk(pParse) );
p = pParse->pConstExpr;
if( regDest<0 ){
if( p && reusable ){
struct ExprList_item *pItem;
int i;
for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
return pItem->u.iConstExprReg;
}
}
}
regDest = ++pParse->nMem;
}
pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
p = sqlite3ExprListAppend(pParse, p, pExpr);
if( p ){
@@ -4010,6 +4033,7 @@ void sqlite3ExprCodeAtInit(
pItem->reusable = reusable;
}
pParse->pConstExpr = p;
return regDest;
}
/*
@@ -4032,19 +4056,8 @@ int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
&& pExpr->op!=TK_REGISTER
&& sqlite3ExprIsConstantNotJoin(pExpr)
){
ExprList *p = pParse->pConstExpr;
int i;
*pReg = 0;
if( p ){
struct ExprList_item *pItem;
for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
return pItem->u.iConstExprReg;
}
}
}
r2 = ++pParse->nMem;
sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1, 1);
}else{
int r1 = sqlite3GetTempReg(pParse);
r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);