From 89a55972fd5d7f0ab815c2a62be69a8f34718353 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 30 Dec 2020 01:04:56 +0100 Subject: libbb/procps: smaller global data for username/groupname cache function old new delta get_cached 101 133 +32 cache_user_group - 4 +4 get_cached_username 17 14 -3 username 8 - -8 groupname 8 - -8 clear_username_cache 65 47 -18 ------------------------------------------------------------------------------ (add/remove: 1/2 grow/shrink: 1/2 up/down: 36/-37) Total: -1 bytes Signed-off-by: Denys Vlasenko --- libbb/procps.c | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/libbb/procps.c b/libbb/procps.c index 7596994..f56b71b 100644 --- a/libbb/procps.c +++ b/libbb/procps.c @@ -21,40 +21,29 @@ typedef struct cache_t { int size; } cache_t; -static cache_t username, groupname; +static cache_t *cache_user_group; -static void clear_cache(cache_t *cp) -{ - free(cp->cache); - cp->cache = NULL; - cp->size = 0; -} void FAST_FUNC clear_username_cache(void) { - clear_cache(&username); - clear_cache(&groupname); -} - -#if 0 /* more generic, but we don't need that yet */ -/* Returns -N-1 if not found. */ -/* cp->cache[N] is allocated and must be filled in this case */ -static int get_cached(cache_t *cp, uid_t id) -{ - int i; - for (i = 0; i < cp->size; i++) - if (cp->cache[i].id == id) - return i; - i = cp->size++; - cp->cache = xrealloc_vector(cp->cache, 2, i); - cp->cache[i++].id = id; - return -i; + if (cache_user_group) { + free(cache_user_group[0].cache); + free(cache_user_group[1].cache); + free(cache_user_group); + cache_user_group = NULL; + } } -#endif -static char* get_cached(cache_t *cp, uid_t id, +static char* get_cached(int user_group, uid_t id, char* FAST_FUNC x2x_utoa(uid_t id)) { + cache_t *cp; int i; + + if (!cache_user_group) + cache_user_group = xzalloc(sizeof(cache_user_group[0]) * 2); + + cp = &cache_user_group[user_group]; + for (i = 0; i < cp->size; i++) if (cp->cache[i].id == id) return cp->cache[i].name; @@ -67,11 +56,11 @@ static char* get_cached(cache_t *cp, uid_t id, } const char* FAST_FUNC get_cached_username(uid_t uid) { - return get_cached(&username, uid, uid2uname_utoa); + return get_cached(0, uid, uid2uname_utoa); } const char* FAST_FUNC get_cached_groupname(gid_t gid) { - return get_cached(&groupname, gid, gid2group_utoa); + return get_cached(1, gid, gid2group_utoa); } -- cgit v1.1