darray: tweak parameters a bit for better memory usage
authorRan Benita <ran234@gmail.com>
Wed, 30 May 2012 12:55:21 +0000 (15:55 +0300)
committerRan Benita <ran234@gmail.com>
Sat, 9 Jun 2012 09:34:57 +0000 (12:34 +0300)
Here are some quick numbers from valgrind, running rulescomp only with a
simple, common "us,de" rule set:

before darray: cb047bb
total heap usage: 44,924 allocs, 44,924 frees, 3,162,342 bytes allocated

after darray: c87468e
total heap usage: 52,670 allocs, 52,670 frees, 2,844,517 bytes allocated

tweaking specific inital allocation sizes:
total heap usage: 52,652 allocs, 52,652 frees, 2,841,814 bytes allocated

changing initial alloc = 2 globally
total heap usage: 47,802 allocs, 47,802 frees, 2,833,614 bytes allocated

changing initial alloc = 3 globally
total heap usage: 47,346 allocs, 47,346 frees, 3,307,110 bytes allocated

changing initial alloc = 4 globally
total heap usage: 44,643 allocs, 44,643 frees, 2,853,646 bytes allocated

[ Changing the geometric progression constant from 2 only made things
worse. I tried the golden ratio - not so golden :) ]

The last one is obviously the best, so it was chosen, with the specific
tweaks thrown in as well (these were there before but don't make much
difference). Overall it seems to do better than the previous manual
allocations which is a bit surprising.

Signed-off-by: Ran Benita <ran234@gmail.com>
src/atom.c
src/darray.h
src/xkbcomp/rules.c
src/xkbcomp/symbols.c

index bdcd205..4f8b089 100644 (file)
@@ -93,6 +93,8 @@ atom_table_new(void)
     if (!table)
         return NULL;
 
+    darray_init(table->table);
+    darray_growalloc(table->table, 100);
     darray_append(table->table, NULL);
 
     return table;
index 6c21bbf..2547b47 100644 (file)
@@ -280,7 +280,7 @@ typedef darray(unsigned long)  darray_ulong;
 static inline size_t darray_next_alloc(size_t alloc, size_t need)
 {
        if (alloc == 0)
-               alloc = 1;
+               alloc = 4;
        while (alloc < need)
                alloc *= 2;
        return alloc;
index 14c3c68..244701d 100644 (file)
@@ -992,10 +992,13 @@ load_rules(FILE *file)
     rules = calloc(1, sizeof(*rules));
     if (!rules)
         return NULL;
+    darray_init(rules->rules);
+    darray_growalloc(rules->rules, 16);
 
     memset(&mapping, 0, sizeof(mapping));
     memset(&tgroup, 0, sizeof(tgroup));
     darray_init(line);
+    darray_growalloc(line, 128);
 
     while (input_line_get(file, &line)) {
         if (match_line(&line, &mapping, &trule, &tgroup)) {
index a61a52b..823223d 100644 (file)
@@ -256,8 +256,6 @@ typedef struct _ModMapEntry
     } u;
 } ModMapEntry;
 
-#define SYMBOLS_INIT_SIZE 110
-
 typedef struct _SymbolsInfo
 {
     char *name;         /* e.g. pc+us+inet(evdev) */
@@ -286,7 +284,7 @@ InitSymbolsInfo(SymbolsInfo * info, struct xkb_keymap *keymap)
     info->fileID = 0;
     info->merge = MergeOverride;
     darray_init(info->keys);
-    darray_growalloc(info->keys, SYMBOLS_INIT_SIZE);
+    darray_growalloc(info->keys, 110);
     info->modMap = NULL;
     for (i = 0; i < XkbNumKbdGroups; i++)
         info->groupNames[i] = XKB_ATOM_NONE;