Krolyxon
Commit e777adf

Branches

Refs →

nsxive777adf

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.
Author
MahouShoujoMivutilde
Date
2022-01-01 11:55 +0300
Committer
GitHub · 2022-01-01 14:55 +0600
Parent
1a691d4
Commit
e777adf98534394ca005a1ca541696b06c791aa6

+26 −5

2 files changed

  1. M config.def.h +7 −4
  2. M image.c +19 −1
M config.def.h View
@@ -62,11 +62,14 @@ static const bool ANTI_ALIAS = true;
6262 */
6363 static const bool ALPHA_LAYER = false;
6464
65−/* cache size for imlib2, in bytes. For backwards compatibility reasons, the
66− * size is kept at 4MiB. For most users, it is advised to pick a value close to
67− * or above 128MiB for better image (re)loading performance.
65+/* percentage of memory to use for imlib2's cache size.
66+ * 3 means use 3% of total memory which is about 245MiB on 8GiB machine.
67+ * 0 or less means disable cache.
68+ * 100 means use all available memory (but not above CACHE_SIZE_LIMIT).
6869 */
69−static const int CACHE_SIZE = 4 * 1024 * 1024; /* 4MiB */
70+static const int CACHE_SIZE_MEM_PERCENTAGE = 3; /* use 3% of total memory for cache */
71+static const int CACHE_SIZE_LIMIT = 256 * 1024 * 1024; /* but not above 256MiB */
72+static const int CACHE_SIZE_FALLBACK = 32 * 1024 * 1024; /* fallback to 32MiB if we can't determine total memory */
7073
7174 #endif
7275 #ifdef _THUMBS_CONFIG
M image.c View
@@ -46,12 +46,30 @@ enum { DEF_WEBP_DELAY = 75 };
4646 #define ZOOM_MIN (zoom_levels[0] / 100)
4747 #define ZOOM_MAX (zoom_levels[ARRLEN(zoom_levels)-1] / 100)
4848
49+static int calc_cache_size(void)
50+{
51+ int cache;
52+ long pages, page_size;
53+
54+ if (CACHE_SIZE_MEM_PERCENTAGE <= 0)
55+ return 0;
56+
57+ pages = sysconf(_SC_PHYS_PAGES);
58+ page_size = sysconf(_SC_PAGE_SIZE);
59+ if (pages < 0 || page_size < 0)
60+ return CACHE_SIZE_FALLBACK;
61+ cache = (pages/100) * CACHE_SIZE_MEM_PERCENTAGE;
62+ cache *= page_size;
63+
64+ return MIN(cache, CACHE_SIZE_LIMIT);
65+}
66+
4967 void img_init(img_t *img, win_t *win)
5068 {
5169 imlib_context_set_display(win->env.dpy);
5270 imlib_context_set_visual(win->env.vis);
5371 imlib_context_set_colormap(win->env.cmap);
54− imlib_set_cache_size(CACHE_SIZE);
72+ imlib_set_cache_size(calc_cache_size());
5573
5674 img->im = NULL;
5775 img->win = win;