summaryrefslogtreecommitdiffstats
path: root/image.c
diff options
context:
space:
mode:
authorMahouShoujoMivutilde <14999778+MahouShoujoMivutilde@users.noreply.github.com>2022-01-01 09:55:59 +0100
committerGitHub <noreply@github.com>2022-01-01 09:55:59 +0100
commite777adf98534394ca005a1ca541696b06c791aa6 (patch)
treeca62045ac14db93ff173c2b31a47edc34cba568d /image.c
parent1a691d42f6893e7bd6d18d541a9a46d87223c9cc (diff)
downloadnsxiv-e777adf98534394ca005a1ca541696b06c791aa6.tar.zst
Allow setting cache size based on memory percentage (#184)
The problem: 1. For the most people imlib2's default 4MiB is unreasonably low; 2. Hardcoding cache size to ~256MiB has performance benefits and doesn't increase RAM usage too much on relatively modern systems; 3. But we can't do that, because that would be detrimental to low spec systems that (apparently) not (?) building nsxiv from source, as been discussed #171 Solution: Calculate cache size based on total memory. Default is set as 3%, which means: * ~245MiB for 8GiB * ~30MiB for 1GiB * and so on CACHE_SIZE_LIMIT (256MiB by default) sets the highest possible value. And in case we cannot determine the total memory (e.g since _SC_PHYS_PAGES isn't POSIX) use CACHE_SIZE_FALLBACK (32MiB by default) instead. Co-authored-by: NRK <nrk@disroot.org>
Diffstat (limited to 'image.c')
-rw-r--r--image.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/image.c b/image.c
index 2cec163..800321a 100644
--- a/image.c
+++ b/image.c
@@ -46,12 +46,30 @@ enum { DEF_WEBP_DELAY = 75 };
#define ZOOM_MIN (zoom_levels[0] / 100)
#define ZOOM_MAX (zoom_levels[ARRLEN(zoom_levels)-1] / 100)
+static int calc_cache_size(void)
+{
+ int cache;
+ long pages, page_size;
+
+ if (CACHE_SIZE_MEM_PERCENTAGE <= 0)
+ return 0;
+
+ pages = sysconf(_SC_PHYS_PAGES);
+ page_size = sysconf(_SC_PAGE_SIZE);
+ if (pages < 0 || page_size < 0)
+ return CACHE_SIZE_FALLBACK;
+ cache = (pages/100) * CACHE_SIZE_MEM_PERCENTAGE;
+ cache *= page_size;
+
+ return MIN(cache, CACHE_SIZE_LIMIT);
+}
+
void img_init(img_t *img, win_t *win)
{
imlib_context_set_display(win->env.dpy);
imlib_context_set_visual(win->env.vis);
imlib_context_set_colormap(win->env.cmap);
- imlib_set_cache_size(CACHE_SIZE);
+ imlib_set_cache_size(calc_cache_size());
img->im = NULL;
img->win = win;