]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Simplifications and optimizations to the Expr object duplication logic.
authordrh <>
Fri, 20 Oct 2023 15:47:30 +0000 (15:47 +0000)
committerdrh <>
Fri, 20 Oct 2023 15:47:30 +0000 (15:47 +0000)
The 5x multiplier crutch from [f371e4c0f8ea73ae] is still present.  More
fixes are still needed.

FossilOrigin-Name: 56142a78163b755f16afc05201f623a7a19d9a4b0620a67f7fa20d2a965a288d

manifest
manifest.uuid
src/expr.c
src/treeview.c

index 982422d0f5af170298d9d4c37f004dbb8c071f8e..84601b58289a137339cbdca01eed881230cb18b5 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Omit\san\sunused\sconstant\sfrom\ssessionfuzz.c\sto\sprevent\sa\scompiler\swarning.
-D 2023-10-20T14:05:26.334
+C Simplifications\sand\soptimizations\sto\sthe\sExpr\sobject\sduplication\slogic.\nThe\s5x\smultiplier\scrutch\sfrom\s[f371e4c0f8ea73ae]\sis\sstill\spresent.\s\sMore\nfixes\sare\sstill\sneeded.
+D 2023-10-20T15:47:30.360
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -668,7 +668,7 @@ F src/date.c eebc54a00e888d3c56147779e9f361b77d62fd69ff2008c5373946aa1ba1d574
 F src/dbpage.c 80e46e1df623ec40486da7a5086cb723b0275a6e2a7b01d9f9b5da0f04ba2782
 F src/dbstat.c 3b677254d512fcafd4d0b341bf267b38b235ccfddbef24f9154e19360fa22e43
 F src/delete.c cb766727c78e715f9fb7ec8a7d03658ed2a3016343ca687acfcec9083cdca500
-F src/expr.c eb0d4b2078d05284e209677183b4bcc20d4545260897215a5ef3fced03b67083
+F src/expr.c 1755a8386bd8bb49b5c21cb33682608be33c07ce5df686eb2aa76983bc3a9520
 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007
 F src/fkey.c a7fcbf7e66d14dbb73cf49f31489ebf66d0e6006c62b95246924a3bae9f37b36
 F src/func.c e8d7b3587a225f4f1116f720b72090511fe9feb936e960bd26a053cea6a17a63
@@ -781,7 +781,7 @@ F src/test_window.c cdae419fdcea5bad6dcd9368c685abdad6deb59e9fc8b84b153de513d394
 F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9
 F src/threads.c 4ae07fa022a3dc7c5beb373cf744a85d3c5c6c3c
 F src/tokenize.c 23d9f4539880b40226254ad9072f4ecf12eb1902e62aea47aac29928afafcfd5
-F src/treeview.c bd8ec60f37e3c5d133e00dbff6ba44bb7e3a102728bab83ec420b3feb7d04440
+F src/treeview.c 62fafcd31eea60b718f8daf448116b7b19f90134ebc6c20777ddbb07f56a3d28
 F src/trigger.c 5286019b152f622a38900284109e4aae1e5f566deb7ad9ba7e931f0660771f32
 F src/update.c 6904814dd62a7a93bbb86d9f1419c7f134a9119582645854ab02b36b676d9f92
 F src/upsert.c fa125a8d3410ce9a97b02cb50f7ae68a2476c405c76aa692d3acf6b8586e9242
@@ -2133,8 +2133,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 95a1dde63117d696323c775580b9c04f044a5b8d609e9174b739ac03ecc1336c
-R 6ad478f91385b35d37da9909f3b6864d
+P a0cf7e24f928183866ac54f0d6cd83c859d487a19dc87572ab0188d90d2ff87d
+R 984c0c092b0039888c79b896b5903542
 U drh
-Z 3d1888cda1fff3d60caf044573bbf655
+Z 2bc926f7992297727e315207ed48e625
 # Remove this line to create a well-formed Fossil manifest.
index 653320efca054f0f53174a1f8619d33076639963..06c3c8c646eef25b0e70a17e25c80c18ee0b46de 100644 (file)
@@ -1 +1 @@
-a0cf7e24f928183866ac54f0d6cd83c859d487a19dc87572ab0188d90d2ff87d
\ No newline at end of file
+56142a78163b755f16afc05201f623a7a19d9a4b0620a67f7fa20d2a965a288d
\ No newline at end of file
index 35c797ac8facc80f521ca3830fdbd26cff333096..aa43c9be7bcc5ed88c70ccbf18cf80e34f28dae7 100644 (file)
@@ -1529,25 +1529,21 @@ static int dupedExprNodeSize(const Expr *p, int flags){
 
 /*
 ** Return the number of bytes required to create a duplicate of the
-** expression passed as the first argument. The second argument is a
-** mask containing EXPRDUP_XXX flags.
+** expression passed as the first argument.
 **
 ** The value returned includes space to create a copy of the Expr struct
 ** itself and the buffer referred to by Expr.u.zToken, if any.
 **
-** If the EXPRDUP_REDUCE flag is set, then the return value includes
-** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
-** and Expr.pRight variables (but not for any structures pointed to or
-** descended from the Expr.x.pList or Expr.x.pSelect variables).
+** The return value includes space to duplicate all Expr nodes in the
+** tree formed by Expr.pLeft and Expr.pRight, but not any other
+** substructure such as Expr.x.pList, Expr.x.pSelect, and Expr.y.pWin.
 */
-static int dupedExprSize(const Expr *p, int flags){
-  int nByte = 0;
-  if( p ){
-    nByte = dupedExprNodeSize(p, flags);
-    if( flags&EXPRDUP_REDUCE ){
-      nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
-    }
-  }
+static int dupedExprSize(const Expr *p){
+  int nByte;
+  assert( p!=0 );
+  nByte = dupedExprNodeSize(p, EXPRDUP_REDUCE);
+  if( p->pLeft ) nByte += dupedExprSize(p->pLeft);
+  if( p->pRight ) nByte += dupedExprSize(p->pRight);
   return nByte;
 }
 
@@ -1564,6 +1560,13 @@ static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
   u8 *zAlloc;           /* Memory space from which to build Expr object */
   u32 staticFlag;       /* EP_Static if space not obtained from malloc */
 
+#ifdef SQLITE_DEBUG
+  /* If zEnd is not NULL, then it is the first byte past the end of the
+  ** zAlloc buffer allocated by this routine.  Used inside assert()
+  ** to ensure that sufficient space was allocated for zAlloc */
+  u8 *zEnd = 0;
+#endif
+
   assert( db!=0 );
   assert( p );
   assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
@@ -1574,8 +1577,13 @@ static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
     zAlloc = *pzBuffer;
     staticFlag = EP_Static;
     assert( zAlloc!=0 );
+    assert( dupFlags==EXPRDUP_REDUCE );
   }else{
-    zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags)*5);
+    int nAlloc = dupFlags ? dupedExprSize(p) : dupedExprNodeSize(p, 0);
+    zAlloc = sqlite3DbMallocRawNN(db, nAlloc*5);
+#ifdef SQLITE_DEBUG
+    zEnd = zAlloc ? zAlloc+nAlloc : 0;
+#endif
     staticFlag = 0;
   }
   pNew = (Expr *)zAlloc;
@@ -1597,12 +1605,14 @@ static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
     if( dupFlags ){
       assert( ExprHasProperty(p, EP_Reduced)==0 );
       memcpy(zAlloc, p, nNewSize);
+      zAlloc += nNewSize;
     }else{
       u32 nSize = (u32)exprStructSize(p);
       memcpy(zAlloc, p, nSize);
       if( nSize<EXPR_FULLSIZE ){
         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
       }
+      zAlloc += EXPR_FULLSIZE;
     }
 
     /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
@@ -1616,8 +1626,9 @@ static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
 
     /* Copy the p->u.zToken string, if any. */
     if( nToken ){
-      char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
+      char *zToken = pNew->u.zToken = (char*)zAlloc;
       memcpy(zToken, p->u.zToken, nToken);
+      zAlloc += nToken;
     }
 
     if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
@@ -1632,7 +1643,6 @@ static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
 
     /* Fill in pNew->pLeft and pNew->pRight. */
     if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
-      zAlloc += dupedExprNodeSize(p, dupFlags);
       if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
         pNew->pLeft = p->pLeft ?
                       exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
@@ -1661,6 +1671,7 @@ static Expr *exprDup(sqlite3 *db, const Expr *p, int dupFlags, u8 **pzBuffer){
       }
     }
   }
+  assert( zEnd==0 || zAlloc<=zEnd );
   return pNew;
 }
 
index a3f128cab21fb053effa51c8c110ed0e3beeeee6..1fad8673dd363bc08b6d018525dc41f44fe142ca 100644 (file)
@@ -724,6 +724,10 @@ void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
 #endif
       break;
     }
+    case TK_ORDER: {
+      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY");
+      break;
+    }
 #ifndef SQLITE_OMIT_SUBQUERY
     case TK_EXISTS: {
       assert( ExprUseXSelect(pExpr) );