From 57f184e21889e7ec72d0eb39dae6972773b706c5 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 30 May 2012 15:55:21 +0300 Subject: [PATCH] darray: tweak parameters a bit for better memory usage 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 --- src/atom.c | 2 ++ src/darray.h | 2 +- src/xkbcomp/rules.c | 3 +++ src/xkbcomp/symbols.c | 4 +--- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/atom.c b/src/atom.c index bdcd205..4f8b089 100644 --- a/src/atom.c +++ b/src/atom.c @@ -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; diff --git a/src/darray.h b/src/darray.h index 6c21bbf..2547b47 100644 --- a/src/darray.h +++ b/src/darray.h @@ -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; diff --git a/src/xkbcomp/rules.c b/src/xkbcomp/rules.c index 14c3c68..244701d 100644 --- a/src/xkbcomp/rules.c +++ b/src/xkbcomp/rules.c @@ -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)) { diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c index a61a52b..823223d 100644 --- a/src/xkbcomp/symbols.c +++ b/src/xkbcomp/symbols.c @@ -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; -- 2.7.4