From 8e7f8c02a1c6f1a916376f43cbce1271225c2ec7 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 21 Feb 2012 00:59:16 -0600 Subject: cache return values of XAllocNamedColor round trip to the server on every color lookup is pretty bad --- util.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index 4569aa9..e276aaf 100644 --- a/util.c +++ b/util.c @@ -13,6 +13,11 @@ #include #include +#define COLORCACHE_MAX 256 + +static char *colorcache_names[COLORCACHE_MAX]; +static long colorcache_values[COLORCACHE_MAX]; + #define ONEMASK ((size_t)(-1) / 0xFF) void * @@ -64,3 +69,29 @@ spawn(const char *arg) { wait(0); } +long colorcache_get(const char *name) { + int i; + + for (i = 0; i < COLORCACHE_MAX; ++i) { + if (!colorcache_names[i]) + break; + if (!strcmp(colorcache_names[i], name)) + return colorcache_values[i]; + } + + return -1; +} + +void colorcache_set(const char *name, long value) { + int i; + + for (i = 0; i < COLORCACHE_MAX; ++i) + if (!colorcache_names[i]) + break; + + if (i >= COLORCACHE_MAX) + return; + + colorcache_names[i] = strdup(name); + colorcache_values[i] = value; +} -- cgit v1.2.3-54-g00ecf