Pack struct ParsedWord more tightly.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 25 Sep 2023 16:07:32 +0000 (12:07 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 25 Sep 2023 16:07:32 +0000 (12:07 -0400)
In a 64-bit build there's an awful lot of useless pad space in
ParsedWords.  Since we may allocate large arrays of these,
it's worth some effort to reduce their size.

Here we reduce the alen field from uint32 to uint16, and then re-order
the fields to avoid unnecessary padding.  alen is only used to
remember the allocated size of the apos[] array, which is not allowed
to exceed MAXNUMPOS (256) elements, so uint16 is plenty of space for
it.  That gets us from 40 bytes to 24 on 64-bit builds, and from 20
bytes to 16 on 32-bit builds.

Per discussion of bug #18080.  Unfortunately this is an ABI break
so we can't back-patch.

Discussion: https://postgr.es/m/1146921.1695411070@sss.pgh.pa.us

src/include/tsearch/ts_utils.h

index d3dc8bae47585a507396a3ce88f442b92924cb6c..d2aae0c3372e5090e79530d0a4a83f4e73c265ea 100644 (file)
@@ -81,8 +81,10 @@ extern void pushOperator(TSQueryParserState state, int8 oper, int16 distance);
  */
 typedef struct
 {
+       uint16          flags;                  /* currently, only TSL_PREFIX */
        uint16          len;
        uint16          nvariant;
+       uint16          alen;
        union
        {
                uint16          pos;
@@ -90,13 +92,11 @@ typedef struct
                /*
                 * When apos array is used, apos[0] is the number of elements in the
                 * array (excluding apos[0]), and alen is the allocated size of the
-                * array.
+                * array.  We do not allow more than MAXNUMPOS array elements.
                 */
                uint16     *apos;
        }                       pos;
-       uint16          flags;                  /* currently, only TSL_PREFIX */
        char       *word;
-       uint32          alen;
 } ParsedWord;
 
 typedef struct