Mercurial > libjeffpc
diff str.c @ 192:94c9db63f1c3
str: preallocate " " string
Since this is a relatively common string we can preallocate it and return it
instead of going to the heap.
One of the consumers of libjeffpc saw almost a 17% reduction in struct str
allocations during startup:
cache buf buf buf buf memory alloc alloc
name size in use in ptc total in use succeed fail
------------------------- ------ ------- ------- ------- ------- --------- -----
str-cache (before) 24 5347 - 5577 132K 795715 0
str-cache (after) 24 5347 - 5577 132K 663036 0
Note: This consumer is a heavy user of strings.
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Sat, 15 Apr 2017 19:38:57 -0400 |
parents | 626a2d545bbe |
children | f377dd66ddb4 |
line wrap: on
line diff
--- a/str.c Sat Apr 15 19:31:05 2017 -0400 +++ b/str.c Sat Apr 15 19:38:57 2017 -0400 @@ -29,7 +29,9 @@ #include <jeffpc/jeffpc.h> static struct str empty_string = STR_STATIC_INITIALIZER(""); -static struct str one_char[128]; +static struct str one_char[128] = { + [' '] = STR_STATIC_INITIALIZER(" "), +}; static struct mem_cache *str_cache;