Clean up the implementation of constant function factorization.
FossilOrigin-Name: 2ab997e47998d261bd6190bbce5c09f3fbd4cfd3
diff --git a/manifest b/manifest
index 0bd958e..57528b4 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Attempt\sto\sfactor\sout\sconstant\sfunctions\sfrom\sthe\sinterior\sof\stable\sscans,\nsince\sfunctions\scan\soften\sbe\sexpensive\sto\scompute.
-D 2017-01-04T01:07:24.603
+C Clean\sup\sthe\simplementation\sof\sconstant\sfunction\sfactorization.
+D 2017-01-04T04:10:02.788
F Makefile.in 41bd4cad981487345c4a84081074bcdb876e4b2e
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc b8ca53350ae545e3562403d5da2a69cec79308da
@@ -341,7 +341,7 @@
F src/date.c dc3f1391d9297f8c748132813aaffcb117090d6e
F src/dbstat.c 19ee7a4e89979d4df8e44cfac7a8f905ec89b77d
F src/delete.c c8bc10d145c9666a34ae906250326fdaa8d58fa5
-F src/expr.c 8ba6e4ee13cab031bab897d871cf51678cbdd57e
+F src/expr.c 9e60a71c788a00328652ed37b9f6b6b4374592a3
F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
F src/fkey.c 2e9aabe1aee76273aff8a84ee92c464e095400ae
F src/func.c c67273e1ec08abbdcc14c189892a3ff6eeece86b
@@ -393,7 +393,7 @@
F src/sqlite.h.in e8e2d108d82647f0a812fdb74accf91c1ec08ddc
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h 8648034aa702469afb553231677306cc6492a1ae
-F src/sqliteInt.h ba08d9eb983697be3eb7ee8d41d1121177694f59
+F src/sqliteInt.h 9fdfb8789b27a621f3401468bc1705c32308f877
F src/sqliteLimit.h c0373387c287c8d0932510b5547ecde31b5da247
F src/status.c a9e66593dfb28a9e746cba7153f84d49c1ddc4b1
F src/table.c 5226df15ab9179b9ed558d89575ea0ce37b03fc9
@@ -1541,10 +1541,7 @@
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P d3ac32a6e7f1823450feb3d1089802542090d164
-R c8a0edce7fe1361197ea989faff6405e
-T *branch * factor-constant-funcs
-T *sym-factor-constant-funcs *
-T -sym-trunk *
+P 62e9270a8057d758621da33adb27fad14225f95d
+R 1aa2a1487ef2ae3f764297787590ccdb
U drh
-Z 75605860ea44512895bed0398cfff7b7
+Z 020211a07112e857de5c9ad202e08252
diff --git a/manifest.uuid b/manifest.uuid
index f5c8571..aabd0aa 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-62e9270a8057d758621da33adb27fad14225f95d
\ No newline at end of file
+2ab997e47998d261bd6190bbce5c09f3fbd4cfd3
\ No newline at end of file
diff --git a/src/expr.c b/src/expr.c
index 2fb5dff..d6133c0 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -3613,7 +3613,9 @@
CollSeq *pColl = 0; /* A collating sequence */
if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
- return sqlite3ExprCodeAtInit(pParse, pExpr, -1, 1);
+ /* SQL function can be expensive. So try to move constant functions
+ ** out of the inner loop, even if that means an extra OP_Copy. */
+ return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
}
assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
if( ExprHasProperty(pExpr, EP_TokenOnly) ){
@@ -3996,41 +3998,36 @@
/*
** 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.
+** If regDest>=0 then the result is always stored in that register and the
+** result is not reusable. If regDest<0 then this routine is free to
+** store the value whereever it wants. The register where the expression
+** is stored is returned. When regDest<0, two identical expressions will
+** code to the same register.
*/
int sqlite3ExprCodeAtInit(
Parse *pParse, /* Parsing context */
Expr *pExpr, /* The expression to code when the VDBE initializes */
- int regDest, /* Store the value in this register */
- u8 reusable /* True if this expression is reusable */
+ int regDest /* Store the value in this register */
){
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;
- }
+ if( regDest<0 && p ){
+ 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 ){
struct ExprList_item *pItem = &p->a[p->nExpr-1];
+ pItem->reusable = regDest<0;
+ if( regDest<0 ) regDest = ++pParse->nMem;
pItem->u.iConstExprReg = regDest;
- pItem->reusable = reusable;
}
pParse->pConstExpr = p;
return regDest;
@@ -4057,7 +4054,7 @@
&& sqlite3ExprIsConstantNotJoin(pExpr)
){
*pReg = 0;
- r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1, 1);
+ r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
}else{
int r1 = sqlite3GetTempReg(pParse);
r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
@@ -4111,7 +4108,7 @@
*/
void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
- sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
+ sqlite3ExprCodeAtInit(pParse, pExpr, target);
}else{
sqlite3ExprCode(pParse, pExpr, target);
}
@@ -4183,7 +4180,7 @@
sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
}
}else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
- sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
+ sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
}else{
int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
if( inReg!=target+i ){
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 47fb1b5..8cfa0f8 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -3716,7 +3716,7 @@
void sqlite3ExprCode(Parse*, Expr*, int);
void sqlite3ExprCodeCopy(Parse*, Expr*, int);
void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
-int sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
+int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
int sqlite3ExprCodeTarget(Parse*, Expr*, int);
void sqlite3ExprCodeAndCache(Parse*, Expr*, int);