gtkfilesystem: bound the shared file icon cache

The per-icon-theme pixbuf cache used for themed file icons was never
evicted, so zooming through the icon view kept a rendered copy of every
icon at every size alive for the lifetime of the process.

Keep it as an LRU instead: at most 64 entries and 4 MiB of pixel data,
least recently used entries are dropped first, and the whole cache is
still cleared when the icon theme changes. 64 entries comfortably hold
the icons of one folder at two zoom levels, so scrolling and zooming
keep hitting the cache while memory stays bounded on small machines.

Valgrind zoom stress (icon view, all zoom levels, folder switches)
reports no errors in the new code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F6aiMGWu4tXHcuSSC9DHi2
This commit is contained in:
Daemonratte 2026-10-13 12:48:10 +02:00
commit d666cf3f03

View file

@ -1639,18 +1639,105 @@ file_icon_cache_key_free (FileIconCacheKey *key)
g_slice_free (FileIconCacheKey, key);
}
static void
file_icon_cache_theme_changed_cb (GtkIconTheme *icon_theme,
GHashTable *cache)
/* The cache is bounded (least recently used entries go first) so zooming
* through many icon sizes does not keep every rendered size alive. */
#define FILE_ICON_CACHE_MAX_ENTRIES 64
#define FILE_ICON_CACHE_MAX_BYTES (4 * 1024 * 1024)
typedef struct
{
g_hash_table_remove_all (cache);
GHashTable *table; /* FileIconCacheKey -> FileIconCacheEntry */
GQueue lru; /* keys, most recently used first */
gsize bytes;
} FileIconCache;
typedef struct
{
GdkPixbuf *pixbuf;
GList *link; /* in FileIconCache.lru */
gsize bytes;
} FileIconCacheEntry;
static void
file_icon_cache_entry_free (FileIconCacheEntry *entry)
{
g_object_unref (entry->pixbuf);
g_slice_free (FileIconCacheEntry, entry);
}
static GHashTable *
static void
file_icon_cache_free (FileIconCache *cache)
{
g_queue_clear (&cache->lru);
g_hash_table_destroy (cache->table);
g_slice_free (FileIconCache, cache);
}
static void
file_icon_cache_theme_changed_cb (GtkIconTheme *icon_theme,
FileIconCache *cache)
{
g_queue_clear (&cache->lru);
g_hash_table_remove_all (cache->table);
cache->bytes = 0;
}
static GdkPixbuf *
file_icon_cache_lookup (FileIconCache *cache,
const FileIconCacheKey *key)
{
FileIconCacheEntry *entry = g_hash_table_lookup (cache->table, key);
if (entry == NULL)
return NULL;
/* Mark as most recently used. */
g_queue_unlink (&cache->lru, entry->link);
g_queue_push_head_link (&cache->lru, entry->link);
return entry->pixbuf;
}
static void
file_icon_cache_insert (FileIconCache *cache,
FileIconCacheKey *key,
GdkPixbuf *pixbuf)
{
FileIconCacheEntry *entry;
/* g_hash_table_insert() would free @key and keep the old one, leaving a
* dangling pointer in the LRU queue; keep the existing entry instead. */
if (g_hash_table_lookup (cache->table, key) != NULL)
{
file_icon_cache_key_free (key);
return;
}
entry = g_slice_new (FileIconCacheEntry);
entry->pixbuf = g_object_ref (pixbuf);
entry->bytes = (gsize) gdk_pixbuf_get_rowstride (pixbuf) * gdk_pixbuf_get_height (pixbuf);
g_queue_push_head (&cache->lru, key);
entry->link = cache->lru.head;
g_hash_table_insert (cache->table, key, entry);
cache->bytes += entry->bytes;
while (cache->lru.length > 1 &&
(cache->lru.length > FILE_ICON_CACHE_MAX_ENTRIES ||
cache->bytes > FILE_ICON_CACHE_MAX_BYTES))
{
FileIconCacheKey *old_key = g_queue_pop_tail (&cache->lru);
FileIconCacheEntry *old = g_hash_table_lookup (cache->table, old_key);
cache->bytes -= old->bytes;
g_hash_table_remove (cache->table, old_key);
}
}
static FileIconCache *
file_icon_cache_get_for_icon_theme (GtkIconTheme *icon_theme)
{
static GQuark quark_file_icon_cache = 0;
GHashTable *cache;
FileIconCache *cache;
if (G_UNLIKELY (quark_file_icon_cache == 0))
quark_file_icon_cache = g_quark_from_static_string ("gtk-file-icon-cache");
@ -1658,14 +1745,16 @@ file_icon_cache_get_for_icon_theme (GtkIconTheme *icon_theme)
cache = g_object_get_qdata (G_OBJECT (icon_theme), quark_file_icon_cache);
if (!cache)
{
cache = g_hash_table_new_full (file_icon_cache_key_hash,
file_icon_cache_key_equal,
(GDestroyNotify) file_icon_cache_key_free,
g_object_unref);
cache = g_slice_new0 (FileIconCache);
cache->table = g_hash_table_new_full (file_icon_cache_key_hash,
file_icon_cache_key_equal,
(GDestroyNotify) file_icon_cache_key_free,
(GDestroyNotify) file_icon_cache_entry_free);
g_queue_init (&cache->lru);
g_object_set_qdata_full (G_OBJECT (icon_theme),
quark_file_icon_cache,
cache,
(GDestroyNotify) g_hash_table_destroy);
(GDestroyNotify) file_icon_cache_free);
g_signal_connect (icon_theme, "changed",
G_CALLBACK (file_icon_cache_theme_changed_cb),
@ -1684,7 +1773,7 @@ get_pixbuf_from_gicon (GIcon *icon,
GdkScreen *screen;
GtkIconTheme *icon_theme;
GtkIconInfo *icon_info;
GHashTable *cache;
FileIconCache *cache;
FileIconCacheKey lookup_key;
FileIconCacheKey *cache_key;
GdkPixbuf *pixbuf;
@ -1708,7 +1797,7 @@ get_pixbuf_from_gicon (GIcon *icon,
lookup_key.icon = icon;
lookup_key.icon_size = icon_size;
pixbuf = g_hash_table_lookup (cache, &lookup_key);
pixbuf = file_icon_cache_lookup (cache, &lookup_key);
if (pixbuf)
return g_object_ref (pixbuf);
}
@ -1734,7 +1823,7 @@ get_pixbuf_from_gicon (GIcon *icon,
if (cache)
{
cache_key = file_icon_cache_key_new (screen, icon, icon_size);
g_hash_table_insert (cache, cache_key, g_object_ref (pixbuf));
file_icon_cache_insert (cache, cache_key, pixbuf);
}
return pixbuf;