Commit d327d3e27be2f5d4119fe491a4d23f8bb2f5759b

Ran Benita 2012-08-07T11:40:07

types: store atoms instead of strings for level and type names We don't use these strings much, so storing them in the manner they were compiled saves some copying and space. Signed-off-by: Ran Benita <ran234@gmail.com>

diff --git a/src/keymap-dump.c b/src/keymap-dump.c
index b2a08b6..4f8d6e5 100644
--- a/src/keymap-dump.c
+++ b/src/keymap-dump.c
@@ -361,7 +361,7 @@ write_types(struct xkb_keymap *keymap, struct buf *buf)
         type = &keymap->types[i];
 
         write_buf(buf, "\t\ttype \"%s\" {\n",
-                  type->name);
+                  xkb_atom_text(keymap->ctx, type->name));
         write_buf(buf, "\t\t\tmodifiers= %s;\n",
                   get_mod_mask_text(keymap, type->mods.real_mods,
                                     type->mods.vmods));
@@ -396,7 +396,7 @@ write_types(struct xkb_keymap *keymap, struct buf *buf)
                 if (!type->level_names[n])
                     continue;
                 write_buf(buf, "\t\t\tlevel_name[Level%d]= \"%s\";\n", n + 1,
-                          type->level_names[n]);
+                          xkb_atom_text(keymap->ctx, type->level_names[n]));
             }
         }
         write_buf(buf, "\t\t};\n");
@@ -753,12 +753,15 @@ write_symbols(struct xkb_keymap *keymap, struct buf *buf)
                         continue;
                     type = XkbKeyTypeIndex(key, group);
                     write_buf(buf, "\n\t\t\ttype[group%u]= \"%s\",",
-                              group + 1, keymap->types[type].name);
+                              group + 1,
+                              xkb_atom_text(keymap->ctx,
+                                            keymap->types[type].name));
                 }
             }
             else {
                 write_buf(buf, "\n\t\t\ttype= \"%s\",",
-                          keymap->types[type].name);
+                          xkb_atom_text(keymap->ctx,
+                                        keymap->types[type].name));
             }
         }
 
diff --git a/src/xkb-priv.h b/src/xkb-priv.h
index 95b243e..86d5324 100644
--- a/src/xkb-priv.h
+++ b/src/xkb-priv.h
@@ -91,6 +91,9 @@
 #include "list.h"
 
 typedef uint16_t xkb_level_index_t;
+typedef uint32_t xkb_atom_t;
+
+#define XKB_ATOM_NONE 0
 
 enum xkb_file_type {
     /* The top level file which includes the other component files. */
@@ -252,8 +255,8 @@ struct xkb_key_type {
     xkb_level_index_t num_levels;
     struct xkb_kt_map_entry *map;
     unsigned int num_entries;
-    const char *name;
-    const char **level_names;
+    xkb_atom_t name;
+    xkb_atom_t *level_names;
 };
 
 struct xkb_sym_interpret {
@@ -465,10 +468,6 @@ XkbKeycodeInRange(struct xkb_keymap *keymap, xkb_keycode_t kc)
     return kc >= keymap->min_key_code && kc <= keymap->max_key_code;
 }
 
-typedef uint32_t xkb_atom_t;
-
-#define XKB_ATOM_NONE 0
-
 xkb_atom_t
 xkb_atom_intern(struct xkb_context *ctx, const char *string);
 
diff --git a/src/xkbcomp/keytypes.c b/src/xkbcomp/keytypes.c
index 3f6a290..76204ae 100644
--- a/src/xkbcomp/keytypes.c
+++ b/src/xkbcomp/keytypes.c
@@ -1022,7 +1022,6 @@ static bool
 CopyDefToKeyType(KeyTypesInfo *info, KeyTypeInfo *def,
                  struct xkb_key_type *type)
 {
-    unsigned int i;
     PreserveInfo *pre;
     struct xkb_keymap *keymap = info->keymap;
 
@@ -1053,29 +1052,9 @@ CopyDefToKeyType(KeyTypesInfo *info, KeyTypeInfo *def,
     type->map = darray_mem(def->entries, 0);
     type->num_entries = darray_size(def->entries);
     darray_init(def->entries);
-
-    type->name = xkb_atom_text(keymap->ctx, def->name);
-
-    if (!darray_empty(def->level_names)) {
-        type->level_names = calloc(darray_size(def->level_names),
-                                   sizeof(*type->level_names));
-        if (!type->level_names) {
-            log_warn(info->keymap->ctx,
-                     "Couldn't allocate level names array; "
-                     "Level name settings for type %s lost\n",
-                     xkb_atom_text(keymap->ctx, def->name));
-        } else {
-            xkb_atom_t *name;
-
-            /* assert def->szNames<=def->num_levels */
-            i = 0;
-            darray_foreach(name, def->level_names)
-                type->level_names[i++] = xkb_atom_text(keymap->ctx, *name);
-        }
-    }
-    else {
-        type->level_names = NULL;
-    }
+    type->name = def->name;
+    type->level_names = darray_mem(def->level_names, 0);
+    darray_init(def->level_names);
 
     return ComputeEffectiveMap(keymap, type);
 }
diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c
index 7067cc5..1ed5d11 100644
--- a/src/xkbcomp/symbols.c
+++ b/src/xkbcomp/symbols.c
@@ -1476,20 +1476,19 @@ FindKeyForSymbol(struct xkb_keymap *keymap, xkb_keysym_t sym)
 /**
  * Find the given name in the keymap->map->types and return its index.
  *
- * @param atom The atom to search for.
+ * @param name The name to search for.
  * @param type_rtrn Set to the index of the name if found.
  *
  * @return true if found, false otherwise.
  */
 static bool
-FindNamedType(struct xkb_keymap *keymap, xkb_atom_t atom, unsigned *type_rtrn)
+FindNamedType(struct xkb_keymap *keymap, xkb_atom_t name, unsigned *type_rtrn)
 {
-    unsigned n = 0;
-    const char *name = xkb_atom_text(keymap->ctx, atom);
+    unsigned int i;
 
-    for (n = 0; n < keymap->num_types; n++) {
-        if (streq(keymap->types[n].name, name)) {
-            *type_rtrn = n;
+    for (i = 0; i < keymap->num_types; i++) {
+        if (keymap->types[i].name == name) {
+            *type_rtrn = i;
             return true;
         }
     }
@@ -1756,7 +1755,8 @@ CopySymbolsDef(SymbolsInfo *info, KeyInfo *keyi,
             log_lvl(info->keymap->ctx, 1,
                     "Type \"%s\" has %d levels, but %s has %d symbols; "
                     "Ignoring extra symbols\n",
-                    type->name, type->num_levels,
+                    xkb_atom_text(keymap->ctx, type->name),
+                    type->num_levels,
                     LongKeyNameText(keyi->name),
                     keyi->numLevels[i]);
             keyi->numLevels[i] = type->num_levels;