diff --git a/manifest b/manifest index f855faf0cf..d712c505fd 100644 --- 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. diff --git a/manifest.uuid b/manifest.uuid index 0689e20b8f..d40615c913 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -bb520293d8c11518ba153b986662f081ebfd781d38eb624c509605fa9148f6e9 \ No newline at end of file +7fefd8676110a53e6c98a697e2dbf820740fe602a1e83b6caa8d099c41a15d80 \ No newline at end of file diff --git a/src/vdbeInt.h b/src/vdbeInt.h index cc5fd604d5..dc284fb0bb 100644 --- a/src/vdbeInt.h +++ b/src/vdbeInt.h @@ -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. diff --git a/src/vdbeapi.c b/src/vdbeapi.c index 6939813a27..69f3d7b120 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -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, diff --git a/src/vdbeaux.c b/src/vdbeaux.c index dea71a3c96..0d91124c9d 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -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 ); } }