]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
An optimization to initMemArray() saves almost 500K cycles. But it seems a
authordrh <>
Mon, 28 Feb 2022 03:25:13 +0000 (03:25 +0000)
committerdrh <>
Mon, 28 Feb 2022 03:25:13 +0000 (03:25 +0000)
little dodgy.  I want to think about this more before merging to trunk.
Perhaps there is a cleaner way to accomplish the same.

FossilOrigin-Name: 7fefd8676110a53e6c98a697e2dbf820740fe602a1e83b6caa8d099c41a15d80

manifest
manifest.uuid
src/vdbeInt.h
src/vdbeapi.c
src/vdbeaux.c

index f855faf0cffd997082a3812fab57b8ebe52b982c..d712c505fdd7564dc64a0f430c8041994bb83207 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Avoid\sunnecessary\sdeinitialization\sof\sthe\sMem.flags\sfield.
-D 2022-02-28T02:35:48.040
+C An\soptimization\sto\sinitMemArray()\ssaves\salmost\s500K\scycles.\s\sBut\sit\sseems\sa\nlittle\sdodgy.\s\sI\swant\sto\sthink\sabout\sthis\smore\sbefore\smerging\sto\strunk.\nPerhaps\sthere\sis\sa\scleaner\sway\sto\saccomplish\sthe\ssame.
+D 2022-02-28T03:25:13.694
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -626,9 +626,9 @@ F src/util.c 602fe229f32a96ceccae4f40824129669582096f7c355f53dbac156c9fecef23
 F src/vacuum.c 6c38ddc52f0619865c91dae9c441d4d48bf3040d7dc1bc5b22da1e45547ed0b3
 F src/vdbe.c 4b969ebe6b61f87a90aebf817bc6ebda5075fe56987591091a9bf22556262484
 F src/vdbe.h a1d0e3b934e835e73edd146f2e7c4eadb711b5c9875c18159a57483fd78e550e
-F src/vdbeInt.h de2348c1643c1ac5bf0932452cbb708f52f52d8b4e29b667abdcfd4bacbf6aa6
-F src/vdbeapi.c 8863ffb5a7bac42fe9a68aaa3526ee29fc18fb02a9b27188b756de41e33856e9
-F src/vdbeaux.c 342a59d99d57185b972fe40b8a9e4bb7d8d52ee8ce327159992ffb3efe55e1ab
+F src/vdbeInt.h 1336e8e23cec21df0dd83d61d0981e3ce72dd19923e9792182066affa2adbf73
+F src/vdbeapi.c 9daec6f3382a150f1da47fc74f2b3aa5b1790bd9dff322539887d25f18054ca9
+F src/vdbeaux.c e5dfd98fe562475d6519af81667b99d237f4999c4709be219d1084ea1fb0af1f
 F src/vdbeblob.c 5e61ce31aca17db8fb60395407457a8c1c7fb471dde405e0cd675974611dcfcd
 F src/vdbemem.c 7737f0b1c480a32b057849c804d2f21d5389649bb8be80f77ad75df700adc9a1
 F src/vdbesort.c 43756031ca7430f7aec3ef904824a7883c4ede783e51f280d99b9b65c0796e35
@@ -1944,8 +1944,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 7cf2d1f0396362aae7b93da75c2036d52ba86acba1cc90abca560bcf0314a22f
-R 4d4dfe83297801d5193fce44bd4e8a2e
+P bb520293d8c11518ba153b986662f081ebfd781d38eb624c509605fa9148f6e9
+R f523ab929bd4578ff1ab123801eb9b63
+T *branch * optimize-init-mem
+T *sym-optimize-init-mem *
+T -sym-trunk *
 U drh
-Z 9485415c0ffb00de37ffcb9d694e10b9
+Z 9ff8d5adc5911dd21d68c30eb1e14ee4
 # Remove this line to create a well-formed Fossil manifest.
index 0689e20b8f9270a72e2345a78ba83350f67ea973..d40615c913224c8d6f5c6e288fcb2b51c186a149 100644 (file)
@@ -1 +1 @@
-bb520293d8c11518ba153b986662f081ebfd781d38eb624c509605fa9148f6e9
\ No newline at end of file
+7fefd8676110a53e6c98a697e2dbf820740fe602a1e83b6caa8d099c41a15d80
\ No newline at end of file
index cc5fd604d5ee8a897b746ce9cb76fe5e0707c8ed..dc284fb0bbb8640149297071937e560c1c5ae110 100644 (file)
@@ -199,6 +199,10 @@ struct VdbeFrame {
 ** Internally, the vdbe manipulates nearly all SQL values as Mem
 ** structures. Each Mem struct may cache multiple representations (string,
 ** integer etc.) of the same value.
+**
+** Code uses offsetof() on this object.  Order of the fields is important.
+** Search for tag-20220228a to find all places that need to change when the
+** field order changes.
 */
 struct sqlite3_value {
   union MemValue {
@@ -208,16 +212,16 @@ struct sqlite3_value {
     const char *zPType; /* Pointer type when MEM_Term|MEM_Subtype|MEM_Null */
     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
   } u;
+  char *z;            /* String or BLOB value */
+  int n;              /* Number of characters in string value, excluding '\0' */
   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
   u8  eSubtype;       /* Subtype for this value */
-  int n;              /* Number of characters in string value, excluding '\0' */
-  char *z;            /* String or BLOB value */
   /* ShallowCopy only needs to copy the information above */
-  char *zMalloc;      /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
+  sqlite3 *db;        /* The associated database connection */
   int szMalloc;       /* Size of the zMalloc allocation */
   u32 uTemp;          /* Transient storage for serial_type in OP_MakeRecord */
-  sqlite3 *db;        /* The associated database connection */
+  char *zMalloc;      /* Space to hold MEM_Str or MEM_Blob if szMalloc>0 */
   void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
 #ifdef SQLITE_DEBUG
   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
@@ -229,7 +233,7 @@ struct sqlite3_value {
 ** Size of struct Mem not including the Mem.zMalloc member or anything that
 ** follows.
 */
-#define MEMCELLSIZE offsetof(Mem,zMalloc)
+#define MEMCELLSIZE offsetof(Mem,db)
 
 /* One or more of the following flags are set to indicate the validOK
 ** representations of the value stored in the Mem struct.
index 6939813a2750786c70f1484ff125806925d748da..69f3d7b120e20d1b00424f3222c64a666a8f4c4c 100644 (file)
@@ -1087,6 +1087,8 @@ int sqlite3_data_count(sqlite3_stmt *pStmt){
 
 /*
 ** Return a pointer to static memory containing an SQL NULL value.
+**
+** Must be revised if column order for Mem changes.  tag-20220228a.
 */
 static const Mem *columnNullValue(void){
   /* Even though the Mem structure contains an element
@@ -1104,15 +1106,15 @@ static const Mem *columnNullValue(void){
 #endif
     = {
         /* .u          = */ {0},
+        /* .z          = */ (char*)0,
+        /* .n          = */ (int)0,
         /* .flags      = */ (u16)MEM_Null,
         /* .enc        = */ (u8)0,
         /* .eSubtype   = */ (u8)0,
-        /* .n          = */ (int)0,
-        /* .z          = */ (char*)0,
-        /* .zMalloc    = */ (char*)0,
+        /* .db         = */ (sqlite3*)0,
         /* .szMalloc   = */ (int)0,
         /* .uTemp      = */ (u32)0,
-        /* .db         = */ (sqlite3*)0,
+        /* .zMalloc    = */ (char*)0,
         /* .xDel       = */ (void(*)(void*))0,
 #ifdef SQLITE_DEBUG
         /* .pScopyFrom = */ (Mem*)0,
index dea71a3c9650af6ebf36dd643986c49267c7a188..0d91124c9dd01678e2d412021c3931aa65d407a3 100644 (file)
@@ -1838,16 +1838,34 @@ void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){
 
 /*
 ** Initialize an array of N Mem element.
+**
+** This is a high-runner, so it is optimized by taking advantage of the
+** order of the fields in a Mem object and using memcpy() rather than
+** individually setting each field.  For each Mem, we need to set:
+**
+**    Mem.flags = flags
+**    Mem.db = db
+**    Mem.szMalloc = 0
+**
+** All other fields of Mem can safely remain uninitialized for now.  They
+** will be initialized before use.  The fields that are initialized by this
+** routine are grouped together so that they can be set using memcpy().
+**
+** tag-20220228a
 */
 static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){
-  while( (N--)>0 ){
-    p->db = db;
-    p->flags = flags;
-    p->szMalloc = 0;
+  if( N>0 ){
+    Mem x;
+    x.flags = flags;
+    x.db = db;
+    x.szMalloc = 0;
+    do{
+      memcpy(&p->flags, &x.flags, offsetof(Mem,uTemp)-offsetof(Mem,flags));
 #ifdef SQLITE_DEBUG
-    p->pScopyFrom = 0;
+      p->pScopyFrom = 0;
 #endif
-    p++;
+      p++;
+    }while( (--N)>0 );
   }
 }