format: Reduce size of struct fmt_spec from 12 bytes to 6.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 8 Mar 2021 05:39:19 +0000 (21:39 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 8 Mar 2021 05:54:16 +0000 (21:54 -0800)
src/data/format.c
src/data/format.h
src/language/data-io/get-data.c
src/language/lexer/format-parser.c
src/language/lexer/format-parser.h
src/language/stats/autorecode.c
src/language/utilities/output.c
src/output/pivot-table.c
src/output/spv/spv-legacy-decoder.c
src/output/spv/spv-writer.c

index a4c52b788ebd811541b9e3ac7d0e25beab016d81..f658a94fd20b8b51b0f41e95f9d63c3d4eff5eb3 100644 (file)
@@ -184,10 +184,7 @@ fmt_settings_set_cc (struct fmt_settings *settings, enum fmt_type type,
 struct fmt_spec
 fmt_for_input (enum fmt_type type, int w, int d)
 {
-  struct fmt_spec f;
-  f.type = type;
-  f.w = w;
-  f.d = d;
+  struct fmt_spec f = { .type = type, .w = w, .d = d };
   assert (fmt_check_input (&f));
   return f;
 }
@@ -197,10 +194,7 @@ fmt_for_input (enum fmt_type type, int w, int d)
 struct fmt_spec
 fmt_for_output (enum fmt_type type, int w, int d)
 {
-  struct fmt_spec f;
-  f.type = type;
-  f.w = w;
-  f.d = d;
+  struct fmt_spec f = { .type = type, .w = w, .d = d };
   assert (fmt_check_output (&f));
   return f;
 }
@@ -1144,12 +1138,8 @@ fmt_clamp_width (struct fmt_spec *fmt, enum fmt_use use)
 static void
 fmt_clamp_decimals (struct fmt_spec *fmt, enum fmt_use use)
 {
-  int max_d;
-
-  max_d = fmt_max_decimals (fmt->type, fmt->w, use);
-  if (fmt->d < 0)
-    fmt->d = 0;
-  else if (fmt->d > max_d)
+  int max_d = fmt_max_decimals (fmt->type, fmt->w, use);
+  if (fmt->d > max_d)
     fmt->d = max_d;
 }
 \f
index 21b75a14f534d7196f9df4ddacb12a8cc58b67c9..1cb7bc6b7feb027573c82c5468b951942d01b6b0 100644 (file)
@@ -56,7 +56,7 @@ enum fmt_category
   };
 
 /* Format type. */
-enum fmt_type
+enum ATTRIBUTE ((packed)) fmt_type
   {
 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) FMT_##NAME,
 #include "format.def"
@@ -75,8 +75,8 @@ enum fmt_type
 struct fmt_spec
   {
     enum fmt_type type;                /* One of FMT_*. */
-    int w;                     /* Width. */
-    int d;                     /* Number of decimal places. */
+    uint16_t w;                        /* Width. */
+    uint8_t d;                 /* Number of decimal places. */
   };
 
 /* Maximum width of any numeric format. */
index cdc867400579bf2d5d4a5e940251dc34409fb824..af44f03a2a8253ed53b4462143f6439cc12501ea 100644 (file)
@@ -626,7 +626,8 @@ parse_get_txt (struct lexer *lexer, struct dataset *ds)
         {
           char fmt_type_name[FMT_TYPE_LEN_MAX + 1];
           enum fmt_type fmt_type;
-          int w, d;
+          uint16_t w;
+          uint8_t d;
 
           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
             goto error;
index 2c05335ff984fa711bacba8920f682cb01c358ee..2ecd539b8fc1660e75f2a8cda703eb7910becb4b 100644 (file)
@@ -34,7 +34,7 @@
 static bool
 parse_abstract_format_specifier__ (struct lexer *lexer,
                                    char type[FMT_TYPE_LEN_MAX + 1],
-                                   int *width, int *decimals)
+                                   uint16_t *width, uint8_t *decimals)
 {
   struct substring s;
   struct substring type_ss, width_ss, decimals_ss;
@@ -90,7 +90,7 @@ error:
 bool
 parse_abstract_format_specifier (struct lexer *lexer,
                                  char type[FMT_TYPE_LEN_MAX + 1],
-                                 int *width, int *decimals)
+                                 uint16_t *width, uint8_t *decimals)
 {
   bool ok = parse_abstract_format_specifier__ (lexer, type, width, decimals);
   if (ok)
index ae1f545dbf73e5f38cc56dbb6e487a16d3440a30..8704e93c477226ffc365c0559d0899ec7e029943 100644 (file)
@@ -22,7 +22,7 @@
 struct lexer;
 
 bool parse_abstract_format_specifier (struct lexer *, char *type,
-                                      int *width, int *decimals);
+                                      uint16_t *width, uint8_t *decimals);
 
 enum fmt_type ;
 struct fmt_spec;
index cd1a220affcc0d9a3a6a243350b73ee2f9c9c087..005ba7622a818602c477b90682c9245d545bf56e 100644 (file)
@@ -387,7 +387,7 @@ cmd_autorecode (struct lexer *lexer, struct dataset *ds)
                 old_values->root, pivot_value_new_value (
                   &item->from, item->width,
                   (item->width
-                   ? &(struct fmt_spec) { FMT_F, item->width, 0 }
+                   ? &(struct fmt_spec) { .type = FMT_F, .w = item->width }
                    : &spec->format),
                   dict_get_encoding (dict)));
               pivot_table_put2 (table, 0, old_value_idx,
index 567314eb6c7fe278610d8335d31cf8c698019cc3..2d7e811885916b0dfd0b397eafd2907ba150819e 100644 (file)
@@ -86,8 +86,8 @@ cmd_output (struct lexer *lexer, struct dataset *ds UNUSED)
              else if (lex_match_id (lexer, "FORMAT"))
                {
                  char type[FMT_TYPE_LEN_MAX + 1];
-                 int width = -1;
-                 int decimals = -1;
+                 uint16_t width;
+                 uint8_t decimals;
 
                  if (! lex_force_match (lexer, T_EQUALS))
                    goto error;
index 55559773c87217cbdec3dbb2060e732f10175e4b..af9a2e9479d1750053744a141328266e74a98fd9 100644 (file)
@@ -869,7 +869,7 @@ pivot_table_create__ (struct pivot_value *title, const char *subtype)
   table->ref_cnt = 1;
   table->show_title = true;
   table->show_caption = true;
-  table->weight_format = (struct fmt_spec) { FMT_F, 40, 0 };
+  table->weight_format = (struct fmt_spec) { .type = FMT_F, .w = 40 };
   table->title = title;
   table->subtype = subtype ? pivot_value_new_text (subtype) : NULL;
   table->command_c = xstrdup_if_nonempty (output_get_command_name ());
@@ -2795,7 +2795,7 @@ struct pivot_value *
 pivot_value_new_integer (double x)
 {
   struct pivot_value *value = pivot_value_new_number (x);
-  value->numeric.format = (struct fmt_spec) { FMT_F, 40, 0 };
+  value->numeric.format = (struct fmt_spec) { .type = FMT_F, .w = 40 };
   return value;
 }
 
index 7641d78ed05c1ec334f3c1c580321aef22942658..d8ddb82e31340c2fa0a8b31f31feedccc9aa669d 100644 (file)
@@ -277,7 +277,7 @@ spv_series_parse_value_map_entry (struct hmap *map,
                           vme->from);
 
       char *error = spv_map_insert (map, from, vme->to, true,
-                                    &(struct fmt_spec) { FMT_A, 40, 0 });
+                                    &(struct fmt_spec) { .type = FMT_A, .w = 40 });
       if (error)
         return error;
 
@@ -367,7 +367,7 @@ decode_number_format (const struct spvdx_number_format *nf)
   if (d < 0 || d > 15)
     d = 2;
 
-  struct fmt_spec f = (struct fmt_spec) { type, 40, d };
+  struct fmt_spec f = (struct fmt_spec) { .type = type, .w = 40, .d = d };
   fmt_fix_output (&f);
   return f;
 }
index 549a0d8f849839b01d4fd3907fd8214048ff2746..2e24a33e9b0dee848feccda520f5bbd6478abe43 100644 (file)
@@ -644,9 +644,10 @@ put_value (struct buf *buf, const struct pivot_value *value)
       put_value_mod (buf, value, NULL);
       size_t len = strlen (value->string.s);
       if (value->string.hex)
-        put_format (buf, &(struct fmt_spec) { FMT_AHEX, len * 2, 0 }, false);
+        put_format (buf, &(struct fmt_spec) { .type = FMT_AHEX, .w = len * 2 },
+                    false);
       else
-        put_format (buf, &(struct fmt_spec) { FMT_A, len, 0 }, false);
+        put_format (buf, &(struct fmt_spec) { .type = FMT_A, .w = len }, false);
       put_string (buf, value->string.value_label);
       put_string (buf, value->string.var_name);
       put_show_values (buf, value->string.show);