Skip to content

Commit 74eaba4

Browse files
author
drh
committed
Merge the Mem.r value into the MemValue union as Mem.u.r. Hence, a Mem can
now store an integer or a real but not both at the same time. Strings are still stored in a separate element Mem.z, for now. FossilOrigin-Name: 4c8c89d7e62aecfe2eb735f7bb114aed6b452847
1 parent 24a0962 commit 74eaba4

File tree

8 files changed

+58
-56
lines changed

8 files changed

+58
-56
lines changed

manifest

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C Performance\simprovement\sfor\saffinity\stransformations\son\scomparison\soperators.
2-
D 2014-09-18T16:28:59.796
1+
C Merge\sthe\sMem.r\svalue\sinto\sthe\sMemValue\sunion\sas\sMem.u.r.\s\sHence,\sa\sMem\scan\nnow\sstore\san\sinteger\sor\sa\sreal\sbut\snot\sboth\sat\sthe\ssame\stime.\s\sStrings\sare\nstill\sstored\sin\sa\sseparate\selement\sMem.z,\sfor\snow.
2+
D 2014-09-18T17:52:15.374
33
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
44
F Makefile.in cf57f673d77606ab0f2d9627ca52a9ba1464146a
55
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -288,15 +288,15 @@ F src/update.c 729f6f18fc27740591d085e1172cebe311144bf0
288288
F src/utf.c 8f634b93d41c089029dd503161a7d3e685d59a9c
289289
F src/util.c 4006c01772bd8d8ac4306d523bbcee41d3e392d8
290290
F src/vacuum.c 59f03f92bcff57faa6a8ca256eb29ccddfb0614a
291-
F src/vdbe.c b00ffadc43a588b02ca2b60b9128338a6f4efcba
291+
F src/vdbe.c 17f285ff89d73b6af5bd1fc90c0943341f4003d5
292292
F src/vdbe.h 09f5b4e3719fa454f252322b1cdab5cf1f361327
293-
F src/vdbeInt.h f90b0de6153f50de630a5a113537efb47083812f
294-
F src/vdbeapi.c c02242df5e9e8d1001e0086f405953833f9c426b
295-
F src/vdbeaux.c 9ac63bc59d2783df77e591e4c4fa8c1153a07eab
293+
F src/vdbeInt.h 45a0b4c5e4b38a2ff8af05102d45e7fa2e6c4439
294+
F src/vdbeapi.c 88929e02676fdbd5f436fcfd63fa0d371756a7ce
295+
F src/vdbeaux.c ac3188f182f25eac58923b1a3c0840c69949ed28
296296
F src/vdbeblob.c 848238dc73e93e48432991bb5651bf87d865eca4
297-
F src/vdbemem.c 8b5e1083fed2da94e315858a7edf5604a5b91804
297+
F src/vdbemem.c 1907e24ab431bd465f5709c30ec28a9119502f9a
298298
F src/vdbesort.c 09efa5e5098d1a159cd21f588eb118e4fe87cfde
299-
F src/vdbetrace.c 16d39c1ef7d1f4a3a7464bea3b7b4bdd7849c415
299+
F src/vdbetrace.c 4f29b04edb0cec3d5fcd9b566d9f0e75c8984362
300300
F src/vtab.c 019dbfd0406a7447c990e1f7bd1dfcdb8895697f
301301
F src/wal.c 10e7de7ce90865a68153f001a61f1d985cd17983
302302
F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
@@ -1198,7 +1198,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
11981198
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
11991199
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
12001200
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
1201-
P 4ef4c9a7c8510203bce0941dda2f76ded8da1de2
1202-
R b808ae1c5ded91d91c2a612e5497b640
1201+
P d7afdcbac24350b73a30c06c45cf0f2122820e4f
1202+
R 320866568edfdafc16dd0b942dd87d16
12031203
U drh
1204-
Z 8ec5fac053656715ec4b7b6a3b299333
1204+
Z 7641aa17b0b2d51a4b828447b0d0dc99

manifest.uuid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d7afdcbac24350b73a30c06c45cf0f2122820e4f
1+
4c8c89d7e62aecfe2eb735f7bb114aed6b452847

src/vdbe.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,13 @@ static void applyNumericAffinity(Mem *pRec, int bTryForInt){
243243
i64 iValue;
244244
u8 enc = pRec->enc;
245245
if( (pRec->flags&MEM_Str)==0 ) return;
246+
if( (pRec->flags&(MEM_Int|MEM_Real))!=0 ) return;
246247
if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
247248
if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
248249
pRec->u.i = iValue;
249250
pRec->flags |= MEM_Int;
250251
}else{
251-
pRec->r = rValue;
252+
pRec->u.r = rValue;
252253
pRec->flags |= MEM_Real;
253254
if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
254255
}
@@ -329,13 +330,13 @@ void sqlite3ValueApplyAffinity(
329330
/*
330331
** pMem currently only holds a string type (or maybe a BLOB that we can
331332
** interpret as a string if we want to). Compute its corresponding
332-
** numeric type, if has one. Set the pMem->r and pMem->u.i fields
333+
** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
333334
** accordingly.
334335
*/
335336
static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
336337
assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
337338
assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
338-
if( sqlite3AtoF(pMem->z, &pMem->r, pMem->n, pMem->enc)==0 ){
339+
if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
339340
return 0;
340341
}
341342
if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
@@ -349,7 +350,7 @@ static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
349350
** none.
350351
**
351352
** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
352-
** But it does set pMem->r and pMem->u.i appropriately.
353+
** But it does set pMem->u.r and pMem->u.i appropriately.
353354
*/
354355
static u16 numericType(Mem *pMem){
355356
if( pMem->flags & (MEM_Int|MEM_Real) ){
@@ -459,7 +460,7 @@ static void memTracePrint(Mem *p){
459460
printf(" i:%lld", p->u.i);
460461
#ifndef SQLITE_OMIT_FLOATING_POINT
461462
}else if( p->flags & MEM_Real ){
462-
printf(" r:%g", p->r);
463+
printf(" r:%g", p->u.r);
463464
#endif
464465
}else if( p->flags & MEM_RowSet ){
465466
printf(" (rowset)");
@@ -1002,7 +1003,7 @@ case OP_Int64: { /* out2-prerelease */
10021003
case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
10031004
pOut->flags = MEM_Real;
10041005
assert( !sqlite3IsNaN(*pOp->p4.pReal) );
1005-
pOut->r = *pOp->p4.pReal;
1006+
pOut->u.r = *pOp->p4.pReal;
10061007
break;
10071008
}
10081009
#endif
@@ -1479,7 +1480,7 @@ case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
14791480
if( sqlite3IsNaN(rB) ){
14801481
goto arithmetic_result_is_null;
14811482
}
1482-
pOut->r = rB;
1483+
pOut->u.r = rB;
14831484
MemSetTypeFlag(pOut, MEM_Real);
14841485
if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
14851486
sqlite3VdbeIntegerAffinity(pOut);
@@ -3572,7 +3573,7 @@ case OP_SeekGT: { /* jump, in3 */
35723573
** (x > 4.9) -> (x >= 5)
35733574
** (x <= 4.9) -> (x < 5)
35743575
*/
3575-
if( pIn3->r<(double)iKey ){
3576+
if( pIn3->u.r<(double)iKey ){
35763577
assert( OP_SeekGE==(OP_SeekGT-1) );
35773578
assert( OP_SeekLT==(OP_SeekLE-1) );
35783579
assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
@@ -3581,7 +3582,7 @@ case OP_SeekGT: { /* jump, in3 */
35813582

35823583
/* If the approximation iKey is smaller than the actual real search
35833584
** term, substitute <= for < and > for >=. */
3584-
else if( pIn3->r>(double)iKey ){
3585+
else if( pIn3->u.r>(double)iKey ){
35853586
assert( OP_SeekLE==(OP_SeekLT+1) );
35863587
assert( OP_SeekGT==(OP_SeekGE+1) );
35873588
assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );

src/vdbeInt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ struct VdbeFrame {
161161
** integer etc.) of the same value.
162162
*/
163163
struct Mem {
164-
union {
164+
union MemValue {
165+
double r; /* Real value used when MEM_Realis set in flags */
165166
i64 i; /* Integer value used when MEM_Int is set in flags */
166167
int nZero; /* Used when bit MEM_Zero is set in flags */
167168
FuncDef *pDef; /* Used only when flags==MEM_Agg */
@@ -171,10 +172,9 @@ struct Mem {
171172
u16 flags; /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
172173
u8 enc; /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
173174
int n; /* Number of characters in string value, excluding '\0' */
174-
double r; /* Real value */
175175
char *z; /* String or BLOB value */
176-
char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */
177176
/* ShallowCopy only needs to copy the information above */
177+
char *zMalloc; /* Dynamic buffer allocated by sqlite3_malloc() */
178178
sqlite3 *db; /* The associated database connection */
179179
void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
180180
#ifdef SQLITE_DEBUG

src/vdbeapi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ static const Mem *columnNullValue(void){
807807
/* .flags = */ MEM_Null,
808808
/* .enc = */ 0,
809809
/* .n = */ 0,
810-
/* .r = */ (double)0,
811810
/* .z = */ 0,
812811
/* .zMalloc = */ 0,
813812
/* .db = */ 0,
@@ -1272,7 +1271,7 @@ int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
12721271
break;
12731272
}
12741273
case SQLITE_FLOAT: {
1275-
rc = sqlite3_bind_double(pStmt, i, pValue->r);
1274+
rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
12761275
break;
12771276
}
12781277
case SQLITE_BLOB: {

src/vdbeaux.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ static char *displayP4(Op *pOp, char *zTemp, int nTemp){
10761076
}else if( pMem->flags & MEM_Int ){
10771077
sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
10781078
}else if( pMem->flags & MEM_Real ){
1079-
sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
1079+
sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
10801080
}else if( pMem->flags & MEM_Null ){
10811081
sqlite3_snprintf(nTemp, zTemp, "NULL");
10821082
}else{
@@ -2949,8 +2949,8 @@ u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
29492949
u64 v;
29502950
u32 i;
29512951
if( serial_type==7 ){
2952-
assert( sizeof(v)==sizeof(pMem->r) );
2953-
memcpy(&v, &pMem->r, sizeof(v));
2952+
assert( sizeof(v)==sizeof(pMem->u.r) );
2953+
memcpy(&v, &pMem->u.r, sizeof(v));
29542954
swapMixedEndianFloat(v);
29552955
}else{
29562956
v = pMem->u.i;
@@ -3020,10 +3020,10 @@ static u32 SQLITE_NOINLINE serialGet(
30203020
swapMixedEndianFloat(t2);
30213021
assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
30223022
#endif
3023-
assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
3023+
assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
30243024
swapMixedEndianFloat(x);
3025-
memcpy(&pMem->r, &x, sizeof(x));
3026-
pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
3025+
memcpy(&pMem->u.r, &x, sizeof(x));
3026+
pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real;
30273027
}
30283028
return 8;
30293029
}
@@ -3368,14 +3368,14 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
33683368
return 0;
33693369
}
33703370
if( (f1&MEM_Real)!=0 ){
3371-
r1 = pMem1->r;
3371+
r1 = pMem1->u.r;
33723372
}else if( (f1&MEM_Int)!=0 ){
33733373
r1 = (double)pMem1->u.i;
33743374
}else{
33753375
return 1;
33763376
}
33773377
if( (f2&MEM_Real)!=0 ){
3378-
r2 = pMem2->r;
3378+
r2 = pMem2->u.r;
33793379
}else if( (f2&MEM_Int)!=0 ){
33803380
r2 = (double)pMem2->u.i;
33813381
}else{
@@ -3536,9 +3536,9 @@ static int vdbeRecordCompareWithSkip(
35363536
}else if( serial_type==7 ){
35373537
double rhs = (double)pRhs->u.i;
35383538
sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
3539-
if( mem1.r<rhs ){
3539+
if( mem1.u.r<rhs ){
35403540
rc = -1;
3541-
}else if( mem1.r>rhs ){
3541+
}else if( mem1.u.r>rhs ){
35423542
rc = +1;
35433543
}
35443544
}else{
@@ -3560,11 +3560,11 @@ static int vdbeRecordCompareWithSkip(
35603560
}else if( serial_type==0 ){
35613561
rc = -1;
35623562
}else{
3563-
double rhs = pRhs->r;
3563+
double rhs = pRhs->u.r;
35643564
double lhs;
35653565
sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
35663566
if( serial_type==7 ){
3567-
lhs = mem1.r;
3567+
lhs = mem1.u.r;
35683568
}else{
35693569
lhs = (double)mem1.u.i;
35703570
}

src/vdbemem.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ int sqlite3VdbeCheckMemInvariants(Mem *p){
3131
*/
3232
assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
3333

34+
/* Cannot be both MEM_Int and MEM_Real at the same time */
35+
assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
36+
3437
/* If p holds a string or blob, the Mem.z must point to exactly
3538
** one of the following:
3639
**
@@ -264,7 +267,7 @@ int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
264267
sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
265268
}else{
266269
assert( fg & MEM_Real );
267-
sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
270+
sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
268271
}
269272
pMem->n = sqlite3Strlen30(pMem->z);
270273
pMem->enc = SQLITE_UTF8;
@@ -421,7 +424,7 @@ i64 sqlite3VdbeIntValue(Mem *pMem){
421424
if( flags & MEM_Int ){
422425
return pMem->u.i;
423426
}else if( flags & MEM_Real ){
424-
return doubleToInt64(pMem->r);
427+
return doubleToInt64(pMem->u.r);
425428
}else if( flags & (MEM_Str|MEM_Blob) ){
426429
i64 value = 0;
427430
assert( pMem->z || pMem->n==0 );
@@ -442,7 +445,7 @@ double sqlite3VdbeRealValue(Mem *pMem){
442445
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
443446
assert( EIGHT_BYTE_ALIGNMENT(pMem) );
444447
if( pMem->flags & MEM_Real ){
445-
return pMem->r;
448+
return pMem->u.r;
446449
}else if( pMem->flags & MEM_Int ){
447450
return (double)pMem->u.i;
448451
}else if( pMem->flags & (MEM_Str|MEM_Blob) ){
@@ -461,12 +464,13 @@ double sqlite3VdbeRealValue(Mem *pMem){
461464
** MEM_Int if we can.
462465
*/
463466
void sqlite3VdbeIntegerAffinity(Mem *pMem){
467+
i64 ix;
464468
assert( pMem->flags & MEM_Real );
465469
assert( (pMem->flags & MEM_RowSet)==0 );
466470
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
467471
assert( EIGHT_BYTE_ALIGNMENT(pMem) );
468472

469-
pMem->u.i = doubleToInt64(pMem->r);
473+
ix = doubleToInt64(pMem->u.r);
470474

471475
/* Only mark the value as an integer if
472476
**
@@ -478,11 +482,9 @@ void sqlite3VdbeIntegerAffinity(Mem *pMem){
478482
** the second condition under the assumption that addition overflow causes
479483
** values to wrap around.
480484
*/
481-
if( pMem->r==(double)pMem->u.i
482-
&& pMem->u.i>SMALLEST_INT64
483-
&& pMem->u.i<LARGEST_INT64
484-
){
485-
pMem->flags |= MEM_Int;
485+
if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
486+
pMem->u.i = ix;
487+
MemSetTypeFlag(pMem, MEM_Int);
486488
}
487489
}
488490

@@ -507,7 +509,7 @@ int sqlite3VdbeMemRealify(Mem *pMem){
507509
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
508510
assert( EIGHT_BYTE_ALIGNMENT(pMem) );
509511

510-
pMem->r = sqlite3VdbeRealValue(pMem);
512+
pMem->u.r = sqlite3VdbeRealValue(pMem);
511513
MemSetTypeFlag(pMem, MEM_Real);
512514
return SQLITE_OK;
513515
}
@@ -527,7 +529,7 @@ int sqlite3VdbeMemNumerify(Mem *pMem){
527529
if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
528530
MemSetTypeFlag(pMem, MEM_Int);
529531
}else{
530-
pMem->r = sqlite3VdbeRealValue(pMem);
532+
pMem->u.r = sqlite3VdbeRealValue(pMem);
531533
MemSetTypeFlag(pMem, MEM_Real);
532534
sqlite3VdbeIntegerAffinity(pMem);
533535
}
@@ -663,7 +665,7 @@ void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
663665
void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
664666
sqlite3VdbeMemSetNull(pMem);
665667
if( !sqlite3IsNaN(val) ){
666-
pMem->r = val;
668+
pMem->u.r = val;
667669
pMem->flags = MEM_Real;
668670
}
669671
}
@@ -1168,14 +1170,14 @@ static int valueFromExpr(
11681170
&& pVal!=0
11691171
){
11701172
sqlite3VdbeMemNumerify(pVal);
1171-
if( pVal->u.i==SMALLEST_INT64 ){
1172-
pVal->flags &= ~MEM_Int;
1173-
pVal->flags |= MEM_Real;
1174-
pVal->r = (double)SMALLEST_INT64;
1173+
if( pVal->flags & MEM_Real ){
1174+
pVal->u.r = -pVal->u.r;
1175+
}else if( pVal->u.i==SMALLEST_INT64 ){
1176+
pVal->u.r = -(double)SMALLEST_INT64;
1177+
MemSetTypeFlag(pVal, MEM_Real);
11751178
}else{
11761179
pVal->u.i = -pVal->u.i;
11771180
}
1178-
pVal->r = -pVal->r;
11791181
sqlite3ValueApplyAffinity(pVal, affinity, enc);
11801182
}
11811183
}else if( op==TK_NULL ){

src/vdbetrace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ char *sqlite3VdbeExpandSql(
127127
}else if( pVar->flags & MEM_Int ){
128128
sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
129129
}else if( pVar->flags & MEM_Real ){
130-
sqlite3XPrintf(&out, 0, "%!.15g", pVar->r);
130+
sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r);
131131
}else if( pVar->flags & MEM_Str ){
132132
int nOut; /* Number of bytes of the string text to include in output */
133133
#ifndef SQLITE_OMIT_UTF16

0 commit comments

Comments
 (0)