aboutsummaryrefslogtreecommitdiff
path: root/src/expr.c
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2017-01-04 01:07:24 +0000
committerdrh <drh@noemail.net>2017-01-04 01:07:24 +0000
commit1e9b53f9bcd39a4e365f105d880dd89d0e637673 (patch)
tree864c304385273e753ef9683ac8d471bf1067398f /src/expr.c
parenta3a40211bd9067ea6b1faf9166c45a864c08080b (diff)
downloadsqlite-1e9b53f9bcd39a4e365f105d880dd89d0e637673.tar.gz
sqlite-1e9b53f9bcd39a4e365f105d880dd89d0e637673.zip
Attempt to factor out constant functions from the interior of table scans,
since functions can often be expensive to compute. FossilOrigin-Name: 62e9270a8057d758621da33adb27fad14225f95d
Diffstat (limited to 'src/expr.c')
-rw-r--r--src/expr.c39
1 files changed, 26 insertions, 13 deletions
diff --git a/src/expr.c b/src/expr.c
index 3bb0ca03f..2fb5dff8c 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -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);