Merge performance enhancements and other patches.

FossilOrigin-Name: 44e1b55aab331933719b67f7fcf7d335234b8b79599fba6033a0a27094bcb644
diff --git a/manifest b/manifest
index e9337d0..ec69408 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\sperformance\senhancements\sfrom\strunk.
-D 2018-12-28T21:32:35.935
+C Merge\sperformance\senhancements\sand\sother\spatches.
+D 2018-12-28T21:40:46.050
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in d8b254f8bb81bab43c340d70d17dc3babab40fcc8a348c8255881f780a45fee6
@@ -454,7 +454,7 @@
 F src/btree.c 4429a1615440f0253d470b59f955fe84787fd6f709ae114c0a12d132ae725599
 F src/btree.h febb2e817be499570b7a2e32a9bbb4b607a9234f6b84bb9ae84916d4806e96f2
 F src/btreeInt.h 620ab4c7235f43572cf3ac2ac8723cbdf68073be4d29da24897c7b77dda5fd96
-F src/build.c 5ca38181e826bcc35e0176c31a691d897ed0cc708fc935fd51e8023705cdcf1a
+F src/build.c f5968d8081182ddfee1629d7465c232c1d4f5d54687a632b188ae2cef5d32368
 F src/callback.c 25dda5e1c2334a367b94a64077b1d06b2553369f616261ca6783c48bcb6bda73
 F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e
 F src/ctime.c 109e58d00f62e8e71ee1eb5944ac18b90171c928ab2e082e058056e1137cc20b
@@ -462,7 +462,7 @@
 F src/dbpage.c 135eb3b5e74f9ef74bde5cec2571192c90c86984fa534c88bf4a055076fa19b7
 F src/dbstat.c 3c8bd4e77f0244fd2bd7cc90acf116ad2f8e82d70e536637f35ac2bc99b726f9
 F src/delete.c f7938125847e8ef485448db5fbad29acb2991381a02887dd854c1617315ab9fb
-F src/expr.c 7e6f3cd438b5023c82c52f45ef3c73d0db576092d26e5927c787a4610c3dd20a
+F src/expr.c 8f674a8295e7c0d65abf3983c2e8a671141d41274f7a3d843ae09e42c97d8a4c
 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
 F src/fkey.c 012dd7dba1a62fda6b76e633ab303b2232ee2874a685c915065227ab20ad6ae0
 F src/func.c 7c288b4ce309b5a8b8473514b88e1f8e69a80134509a8c0db8e39c858e367e7f
@@ -505,7 +505,7 @@
 F src/pcache1.c ddc9fc7d9861cf3a1f30660264b76b1ae9e1dce5dbba085cf001d5cb6b41cf8c
 F src/pragma.c 96ce7dce4dc9cb2b7aa0e1b2ce7536870bdc00b10becc278245e775489447ea0
 F src/pragma.h fdd03d78a7497f74a3f652909f945328480089189526841ae829ce7313d98d13
-F src/prepare.c d0918fb8d00b1ebf19655e7f3d28464b3fc3c43c16d36dd06092de02244a5180
+F src/prepare.c 4fd8502d4e89ba1219ae0467222e8df4608405cb9799b78cc46c4a4990f1cc37
 F src/printf.c 0f1177cf1dd4d7827bf64d840768514ec76409abecaca9e8b577dbd065150381
 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
 F src/resolve.c 72fe8cae7326b979e7258ab4c531956951e1a5f3fe8644c646abaec1b2eb6d95
@@ -515,7 +515,7 @@
 F src/sqlite.h.in b54cd42d2f3b739a00de540cafe2dcd0de3b8e1748a2db33a68def487e9e602f
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h 960f1b86c3610fa23cb6a267572a97dcf286e77aa0dd3b9b23292ffaa1ea8683
-F src/sqliteInt.h ac354476cedd0a7cf103ec225423bc654bc0df8a7a1019ce298425ebe91062cf
+F src/sqliteInt.h 1da81c69ce881019fe00565c75406265cf40be92a17f323f9045bf034e719ecb
 F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b
 F src/status.c 46e7aec11f79dad50965a5ca5fa9de009f7d6bde08be2156f1538a0a296d4d0e
 F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34
@@ -1795,7 +1795,7 @@
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P da53f3d3ff26cc32d3a5ac788b6785baa3d1688e50d9354025a3130dc7e71a38 891f1f72187f0f9ec0d24fda98cc08be3ae3c3ff8b27c4e409ee7135c3106398
-R c1c92a3b03da98f440390a0e9ced2b83
+P 50e5f390fbab4189c9700a2c62c951ab9acbf0a03c491e88f3e3bcf19a2311cc 0f1b9ff9e1e6f13e03045fcb7d0907227085054f9eb0b0b8471fb26b0094b13a
+R 447360c957ed3833b50528f10575cb30
 U drh
-Z 16f502f29d74bfe53bffbbe615dec4cd
+Z e14093037f7573306bc3a2513169a16b
diff --git a/manifest.uuid b/manifest.uuid
index b04e66b..509dc3d 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-0f1b9ff9e1e6f13e03045fcb7d0907227085054f9eb0b0b8471fb26b0094b13a
\ No newline at end of file
+44e1b55aab331933719b67f7fcf7d335234b8b79599fba6033a0a27094bcb644
\ No newline at end of file
diff --git a/src/build.c b/src/build.c
index ab90e12..23b35d2 100644
--- a/src/build.c
+++ b/src/build.c
@@ -168,7 +168,7 @@
     ** on each used database.
     */
     if( db->mallocFailed==0 
-     && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
+     && (DbMaskNonZero(pParse->cookieMask) || pParse->pAuxExpr)
     ){
       int iDb, i;
       assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
@@ -207,17 +207,25 @@
       */
       sqlite3AutoincrementBegin(pParse);
 
-      /* Code constant expressions that where factored out of inner loops */
-      if( pParse->pConstExpr ){
-        ExprList *pEL = pParse->pConstExpr;
+      if( pParse->pAuxExpr ){
+        ExprList *pEL = pParse->pAuxExpr;
+        struct ExprList_item *pX;
         pParse->okConstFactor = 0;
-        for(i=0; i<pEL->nExpr; i++){
-          sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
+        /* Code constant expressions that where factored out of inner loops */
+        for(pX=pEL->a, i=pEL->nExpr; i>0; i--, pX++){
+          if( pX->bAuxSubrtn ) continue;
+          sqlite3ExprCode(pParse, pX->pExpr, pX->u.iConstExprReg);
         }
+        sqlite3VdbeGoto(v, 1);  /* Jump back to the main code */
+        /* Code subroutines factored out from main */
+        for(pX=pEL->a, i=pEL->nExpr; i>0; i--, pX++){
+          if( !pX->bAuxSubrtn ) continue;
+          /* sqlite3ExprCode(pParse, pX->pExpr, pX->u.iConstExprReg); */
+        }
+      }else{
+        /* Jump back to the beginning of main */
+        sqlite3VdbeGoto(v, 1);
       }
-
-      /* Finally, jump back to the beginning of the executable code. */
-      sqlite3VdbeGoto(v, 1);
     }
   }
 
diff --git a/src/expr.c b/src/expr.c
index f0a3a07..ebb6b20 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -1950,7 +1950,7 @@
 **       operands created by the constant propagation optimization.
 **
 ** When this routine returns true, it indicates that the expression
-** can be added to the pParse->pConstExpr list and evaluated once when
+** can be added to the pParse->pAuxExpr list and evaluated once when
 ** the prepared statement starts up.  See sqlite3ExprCodeAtInit().
 */
 int sqlite3ExprIsConstantNotJoin(Expr *p){
@@ -4138,7 +4138,7 @@
 ){
   ExprList *p;
   assert( ConstFactorOk(pParse) );
-  p = pParse->pConstExpr;
+  p = pParse->pAuxExpr;
   if( regDest<0 && p ){
     struct ExprList_item *pItem;
     int i;
@@ -4156,7 +4156,7 @@
      if( regDest<0 ) regDest = ++pParse->nMem;
      pItem->u.iConstExprReg = regDest;
   }
-  pParse->pConstExpr = p;
+  pParse->pAuxExpr = p;
   return regDest;
 }
 
@@ -4394,7 +4394,7 @@
   }else{
     /* Mark the expression is being from the ON or USING clause of a join
     ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
-    ** it into the Parse.pConstExpr list.  We should use a new bit for this,
+    ** it into the Parse.pAuxExpr list.  We should use a new bit for this,
     ** for clarity, but we are out of bits in the Expr.flags field so we
     ** have to reuse the EP_FromJoin bit.  Bummer. */
     exprX.flags |= EP_FromJoin;
diff --git a/src/prepare.c b/src/prepare.c
index b43a37f..4f0f10a 100644
--- a/src/prepare.c
+++ b/src/prepare.c
@@ -506,7 +506,7 @@
 void sqlite3ParserReset(Parse *pParse){
   sqlite3 *db = pParse->db;
   sqlite3DbFree(db, pParse->aLabel);
-  sqlite3ExprListDelete(db, pParse->pConstExpr);
+  sqlite3ExprListDelete(db, pParse->pAuxExpr);
   if( db ){
     assert( db->lookaside.bDisable >= pParse->disableLookaside );
     db->lookaside.bDisable -= pParse->disableLookaside;
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 2b88d2f..2246f84 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -2587,8 +2587,9 @@
     u8 sortOrder;           /* 1 for DESC or 0 for ASC */
     unsigned done :1;       /* A flag to indicate when processing is finished */
     unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
-    unsigned reusable :1;   /* Constant expression is reusable */
+    unsigned reusable   :1; /* Constant expression is reusable */
     unsigned bSorterRef :1; /* Defer evaluation until after sorting */
+    unsigned bAuxSubrtn :1; /* Member of pAuxExpr to be coded as a subroutine */
     union {
       struct {
         u16 iOrderByCol;      /* For ORDER BY, column number in result set */
@@ -3075,7 +3076,7 @@
                        ** of the base register during check-constraint eval */
   int nLabel;          /* Number of labels used */
   int *aLabel;         /* Space to hold the labels */
-  ExprList *pConstExpr;/* Constant expressions */
+  ExprList *pAuxExpr;  /* Exprs coded at initialization or as subroutines */
   Token constraintName;/* Name of the constraint currently being parsed */
   yDbMask writeMask;   /* Start a write transaction on these databases */
   yDbMask cookieMask;  /* Bitmask of schema verified databases */