pivot-table: Reduce size of struct pivot_value from 96 bytes to 80.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 8 Mar 2021 06:52:43 +0000 (22:52 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 9 Mar 2021 02:15:48 +0000 (18:15 -0800)
src/data/settings.h
src/output/ascii.c
src/output/pivot-table.c
src/output/pivot-table.h
src/output/spv/spv.c
src/output/table.c
tests/output/pivot-table-test.c

index 39bac54430f7764a26dafeb9d7bdb57f8e7692d9..1361bef3747cd7afe781d76fab9e5ceaf9c4732c 100644 (file)
@@ -21,6 +21,7 @@
 #include <stddef.h>
 
 #include "data/format.h"
+#include "libpspp/compiler.h"
 #include "libpspp/float-format.h"
 #include "libpspp/integer-format.h"
 #include "libpspp/message.h"
@@ -106,7 +107,7 @@ void settings_set_fuzzbits (int);
 
 /* Whether to show variable or value labels or the underlying value or variable
    name. */
-enum settings_value_show
+enum ATTRIBUTE ((packed)) settings_value_show
   {
     /* Use higher-level default.
        In a pivot_value, the default is taken from the pivot_table.
index cd5fcec64b06ab8c704f4b2fa9b3157089e59450..cdd93aa8e61d1e6d27315b208d6d35e9a813d0b6 100644 (file)
@@ -1033,8 +1033,8 @@ ascii_test_write (struct output_driver *driver,
     .underline = underline,
   };
   const struct pivot_value value = {
-    .type = PIVOT_VALUE_TEXT,
     .text = {
+      .type = PIVOT_VALUE_TEXT,
       .local = CONST_CAST (char *, s),
       .c = CONST_CAST (char *, s),
       .id = CONST_CAST (char *, s),
index f9082eb24e953a405f1b88e78a878e73d5a5e781..ad146a58ba1bdf4baccb03b79b5dd3f5a9a7f795 100644 (file)
@@ -2692,8 +2692,8 @@ pivot_value_new_user_text_nocopy (char *text)
 {
   struct pivot_value *value = xmalloc (sizeof *value);
   *value = (struct pivot_value) {
-    .type = PIVOT_VALUE_TEXT,
     .text = {
+      .type = PIVOT_VALUE_TEXT,
       .local = text,
       .c = text,
       .id = text,
@@ -2735,8 +2735,8 @@ pivot_value_new_text (const char *text)
 
   struct pivot_value *value = xmalloc (sizeof *value);
   *value = (struct pivot_value) {
-    .type = PIVOT_VALUE_TEXT,
     .text = {
+      .type = PIVOT_VALUE_TEXT,
       .local = local,
       .c = c,
       .id = c,
@@ -2762,8 +2762,8 @@ pivot_value_new_text_format (const char *format, ...)
 
   struct pivot_value *value = xmalloc (sizeof *value);
   *value = (struct pivot_value) {
-    .type = PIVOT_VALUE_TEXT,
     .text = {
+      .type = PIVOT_VALUE_TEXT,
       .local = local,
       .c = c,
       .id = xstrdup (c),
@@ -2784,8 +2784,10 @@ pivot_value_new_number (double x)
 {
   struct pivot_value *value = xmalloc (sizeof *value);
   *value = (struct pivot_value) {
-    .type = PIVOT_VALUE_NUMERIC,
-    .numeric = { .x = x, },
+    .numeric = {
+      .type = PIVOT_VALUE_NUMERIC,
+      .x = x
+    },
   };
   return value;
 }
@@ -2863,8 +2865,8 @@ pivot_value_new_variable (const struct variable *variable)
 {
   struct pivot_value *value = xmalloc (sizeof *value);
   *value = (struct pivot_value) {
-    .type = PIVOT_VALUE_VARIABLE,
     .variable = {
+      .type = PIVOT_VALUE_VARIABLE,
       .var_name = xstrdup (var_get_name (variable)),
       .var_label = xstrdup_if_nonempty (var_get_label (variable)),
     },
index 2a5d8467f81037e152505df4a8e7ae1049e24faa..0d6e333b06a82861350d56537c17f4dff2c9e266 100644 (file)
@@ -579,7 +579,7 @@ void pivot_table_dump (const struct pivot_table *, int indentation);
 \f
 /* pivot_value. */
 
-enum pivot_value_type
+enum ATTRIBUTE ((packed)) pivot_value_type
   {
     PIVOT_VALUE_NUMERIC,          /* A value of a numeric variable. */
     PIVOT_VALUE_STRING,           /* A value of a string variable. */
@@ -656,62 +656,75 @@ struct pivot_value
     size_t *footnote_indexes;
     size_t n_footnotes;
 
-    enum pivot_value_type type;
     union
       {
+        enum pivot_value_type type;
+
         /* PIVOT_VALUE_NUMERIC. */
         struct
           {
-            double x;                 /* The numeric value. */
+            enum pivot_value_type type;
+            enum settings_value_show show; /* Show value or label or both? */
             struct fmt_spec format;   /* Format to display 'x'. */
+            bool honor_small;         /* Honor value of pivot table 'small'? */
+            double x;                 /* The numeric value. */
             char *var_name;           /* May be NULL. */
             char *value_label;        /* May be NULL. */
-            enum settings_value_show show; /* Show value or label or both? */
-            bool honor_small;         /* Honor value of pivot table 'small'? */
           }
         numeric;
 
         /* PIVOT_VALUE_STRING. */
         struct
           {
-            char *s;                  /* The string value. */
+            enum pivot_value_type type;
+            enum settings_value_show show; /* Show value or label or both? */
             bool hex;                 /* Display in hex? */
+            char *s;                  /* The string value. */
             char *var_name;           /* May be NULL. */
             char *value_label;        /* May be NULL. */
-            enum settings_value_show show; /* Show value or label or both? */
           }
         string;
 
         /* PIVOT_VALUE_VARIABLE. */
         struct
           {
+            enum pivot_value_type type;
+            enum settings_value_show show; /* Show name or label or both? */
             char *var_name;
             char *var_label;          /* May be NULL. */
-            enum settings_value_show show; /* Show name or label or both? */
           }
         variable;
 
         /* PIVOT_VALUE_TEXT. */
         struct
           {
+            enum pivot_value_type type;
+
             /* 'local', 'c', and 'id' must all be nonnull, but they are allowed
                to be the same pointer. */
+            bool user_provided;
             char *local;              /* Localized. */
             char *c;                  /* English. */
             char *id;                 /* Identifier. */
-            bool user_provided;
           }
         text;
 
         /* PIVOT_VALUE_TEMPLATE. */
         struct
           {
+            enum pivot_value_type type;
+
+            /* Arguments.
+
+               The odd ordering in this struct reduces the overall size
+               of struct pivot_value. */
+            unsigned int n_args;
+            struct pivot_argument *args;
+
             /* Both 'local' and 'id' must be nonnull, but they are allowed to
                be the same pointer. */
             char *local;              /* Localized. */
             char *id;                 /* Identifier. */
-            struct pivot_argument *args;
-            size_t n_args;
           }
         template;
       };
index 93f61b20bb12cae6ed9f6a118d2214dace394902..9b0fe17a21ea4878ccac8e1be290b6003f3d1e35 100644 (file)
@@ -285,8 +285,8 @@ decode_container_text (const struct spvsx_container_text *ct)
   struct pivot_value *value = xmalloc (sizeof *value);
   *value = (struct pivot_value) {
     .font_style = font_style,
-    .type = PIVOT_VALUE_TEXT,
     .text = {
+      .type = PIVOT_VALUE_TEXT,
       .local = text,
       .c = text,
       .id = text,
index 442b765134c9b277161c84adc0647905942be9fa..bf22d2da5af8809de3f2b5d3e7b51e8d6aab70c0 100644 (file)
@@ -504,8 +504,8 @@ table_get_cell (const struct table *t, int x, int y, struct table_cell *cell)
     = t->styles[(opt & TAB_STYLE_MASK) >> TAB_STYLE_SHIFT];
 
   static const struct pivot_value empty_value = {
-    .type = PIVOT_VALUE_TEXT,
     .text = {
+      .type = PIVOT_VALUE_TEXT,
       .local = (char *) "",
       .c = (char *) "",
       .id = (char *) "",
index bf275b20d68e4a7fb1bc85e236bbb8de240920a9..98336b2563f3aefd89d42ee6de5838b0cdd6df51 100644 (file)
@@ -677,8 +677,10 @@ read_value (struct lexer *lexer, const struct pivot_table *pt,
     {
       value = xmalloc (sizeof *value);
       *value = (struct pivot_value) {
-        .type = PIVOT_VALUE_STRING,
-        .string = { .s = xstrdup (lex_tokcstr (lexer)) },
+        .string = {
+          .type = PIVOT_VALUE_STRING,
+          .s = xstrdup (lex_tokcstr (lexer))
+        },
       };
       lex_get (lexer);
     }
@@ -686,8 +688,10 @@ read_value (struct lexer *lexer, const struct pivot_table *pt,
     {
       value = xmalloc (sizeof *value);
       *value = (struct pivot_value) {
-        .type = PIVOT_VALUE_VARIABLE,
-        .variable = { .var_name = xstrdup (lex_tokcstr (lexer)) },
+        .variable = {
+          .type = PIVOT_VALUE_VARIABLE,
+          .var_name = xstrdup (lex_tokcstr (lexer))
+        },
       };
       lex_get (lexer);
     }