]> git.draconx.ca Git - dxcommon.git/commitdiff
copysym: Merge tables into a single static allocation.
authorNick Bowler <nbowler@draconx.ca>
Wed, 21 Feb 2024 02:20:37 +0000 (21:20 -0500)
committerNick Bowler <nbowler@draconx.ca>
Fri, 23 Feb 2024 01:31:59 +0000 (20:31 -0500)
On x86_64, gcc overaligns static arrays at least 16 bytes long onto
a 16-byte boundary, and arrays at least 32 bytes long on a 32-byte
boundary, which for these tables seems to be totally unnecessary.

Putting both arrays into one structure means that at least within
this one translation unit, we don't pointlessly waste space padding
between the two arrays to maintain unneeded alignment.

src/copysym.c

index ca23c44cf1734b8dbd12b409f82343ccd668810d..2106965cd8b87615bb3b4bb456e1052585669df5 100644 (file)
@@ -92,16 +92,10 @@ static int compar_prefix(const void *key, const void *elem_)
  */
 const char *copyright_symbol(const char *charset)
 {
-       /* All known encodings of the copyright symbol */
-       static const char codes[] =
-               "(C)"          "\0"
-               "\xc2\xa9"     "\0"
-               "\x97"         "\0"
-               "\xa8"         "\0"
-               "\xb8"         "\0"
-               "\xbf"         "\0"
-               "\x8f\xa2\xed" "\0"
-               "\x81\x30\x84\x38";
+       struct copysym_data {
+               char tab[15][PREFIXLEN+1];
+               char codes[24];
+       };
 
        /*
         * We need the list below to be in lexicographic order in
@@ -111,17 +105,18 @@ const char *copyright_symbol(const char *charset)
 #  error this character encoding is unsupported, please report a bug.
 #endif
 
-       /*
-        * For character sets that include the copyright symbol,
-        * the first 5 characters suffices to distinguish amongst
-        * all the different possible encodings.
-        *
-        * The final byte of each entry indicates the corresponding
-        * offset into the codes array, except for CP112x and ISO-8859-x
-        * which use the special values 0 and 1, respectively (handled
-        * below).
-        */
-       static const char t1[][PREFIXLEN+1] = {
+       static const struct copysym_data data = {
+               /*
+                * For character sets that include the copyright symbol,
+                * the first 5 characters suffices to distinguish amongst
+                * all the different possible encodings.
+                *
+                * The final byte of each entry indicates the corresponding
+                * offset into the codes array, except for CP112x and ISO-8859
+                * which use the values 0 and 1, respectively (handled below).
+                */
+               .tab =
+               {
                "CP112\x00",
                "CP125\x05",
                "CP775\x09",
@@ -137,12 +132,24 @@ const char *copyright_symbol(const char *charset)
                "KOI8-\x0d",
                "PT154\x05",
                "UTF-8\x04"
+               },
+
+               /* All known encodings of the copyright symbol. */
+               .codes =
+               "(C)"          "\0"
+               "\xc2\xa9"     "\0"
+               "\x97"         "\0"
+               "\xa8"         "\0"
+               "\xb8"         "\0"
+               "\xbf"         "\0"
+               "\x8f\xa2\xed" "\0"
+               "\x81\x30\x84\x38"
        };
 
        unsigned cindex = 0;
        const char *m;
 
-       if (!charset || !(m = BSEARCH_ARRAY(charset, t1, compar_prefix)))
+       if (!charset || !(m = BSEARCH_ARRAY(charset, data.tab, compar_prefix)))
                goto no_conv;
 
        cindex = m[PREFIXLEN];
@@ -187,7 +194,7 @@ const char *copyright_symbol(const char *charset)
                cindex = 5 * ((accept >> (collect & 0x1f)) & 1);
        }
 no_conv:
-       return codes+cindex;
+       return (char *)&data + offsetof(struct copysym_data, codes) + cindex;
 }
 
 #endif