SQLite Forum

Minor struct layout optimization in date.c
Login

Minor struct layout optimization in date.c

(1) By anonymous on 2021-12-22 23:04:10 [source]

File date.c lines 619-632
-------------------------------------------------------------------------------
static const struct {
  u8 eType;           /* Transformation type code */
  u8 nName;           /* Length of th name */
  char *zName;        /* Name of the transformation */
  double rLimit;      /* Maximum NNN value for this transform */
  double rXform;      /* Constant used for this transform */
} aXformType[] = {
  { 0, 6, "second", 464269060800.0, 1000.0         },
  { 0, 6, "minute", 7737817680.0,   60000.0        },
  { 0, 4, "hour",   128963628.0,    3600000.0      },
  { 0, 3, "day",    5373485.0,      86400000.0     },
  { 1, 5, "month",  176546.0,       2592000000.0   },
  { 2, 4, "year",   14713.0,        31536000000.0  },
};
-------------------------------------------------------------------------------
Layout of a struct (a single element of the aXformType array):
on 32-bit machines: 1B eType, 1B nName, 2B padding, 4B zName, 8B rLimit, 8B rXform - total 24 bytes
on 64-bit machines: 1B eType, 1B nName, 6B padding, 8B zName, 8B rLimit, 8B rXform - total 32 bytes

The size of the zName field and peceding padding is 6 bytes (32-bit) or 14 bytes (64-bit), length of the longest string pointed to by zName is 6 bytes (7 bytes including terminating NUL character).

The type of the zName field can be changed to an array of 6 chars
-------------------------------------------------------------------------------
static const struct {
  u8 eType;           /* Transformation type code */
  u8 nName;           /* Length of th name */
  char zName[6];      /* Name of the transformation */
  double rLimit;      /* Maximum NNN value for this transform */
  double rXform;      /* Constant used for this transform */
} aXformType[] = {
  { 0, 6, {'s','e','c','o','n','d'},
                    464269060800.0, 1000.0         },
  { 0, 6, {'m','i','n','u','t','e'},
                    7737817680.0,   60000.0        },
  { 0, 4, "hour",   128963628.0,    3600000.0      },
  { 0, 3, "day",    5373485.0,      86400000.0     },
  { 1, 5, "month",  176546.0,       2592000000.0   },
  { 2, 4, "year",   14713.0,        31536000000.0  },
};
-------------------------------------------------------------------------------
New layout on both 32-bit and 64-bit machines: 1B eType, 1B nName, 6B zName, 8B rLimit, 8B rXform - total 24 bytes, no padding
In two cases ("second" and "minute") terminating NUL characters don't fit but the length of the string is explicit anyway (the nName field).

The only use of the zName (lines 864-865):
-------------------------------------------------------------------------------
        if( aXformType[i].nName==n
         && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
-------------------------------------------------------------------------------

(2) By Richard Hipp (drh) on 2021-12-23 00:18:41 in reply to 1 [link] [source]

See check-in c75ba4fa644d338d for a slightly different transformation that reduces the size per row to 16 bytes on both 64-bit and 32-bit.