cellrenderertext: hash the layout cache

This commit is contained in:
Daemonratte 2026-10-16 19:49:19 +02:00
commit bb97d1cfd6

View file

@ -1377,7 +1377,10 @@ typedef struct
PangoLayout *layout;
guint serial; /* of @layout after our last change */
PangoRectangle rect; /* pixel extents at @width */
guint used;
/* in the cache: the next entry with the same bucket, and the
* neighbours in the order of use, -1 for none */
gint16 bucket_next;
gint16 lru_prev, lru_next;
} CellLayout;
#if PANGO_VERSION_CHECK (1, 46, 0)
@ -1391,31 +1394,38 @@ typedef struct
/* Enough for three or four text columns of the rows a list shows; the
* layout of a short text takes about 1.5 KB. The least recently used
* goes first; all go when the widget is unmapped. */
* goes first; all go when the widget is unmapped. The entries are
* found through buckets by hash, so a lookup does not compare with
* every entry. */
#define CELL_LAYOUTS 128
#define CELL_BUCKETS 64
typedef struct
{
PangoContext *context; /* not a reference: the widget holds one */
guint context_serial;
guint clock;
guint n, size; /* the array grows to CELL_LAYOUTS */
CellLayout *entries;
gint16 buckets[CELL_BUCKETS];
gint16 lru_head, lru_tail; /* most and least recently used */
} CellLayoutCache;
static GQuark cell_layout_quark;
#endif
/* Frees what @cl holds; the links of a cache entry stay */
static void
cell_layout_clear (CellLayout *cl)
{
g_free (cl->text);
cl->text = NULL;
if (cl->attrs)
pango_attr_list_unref (cl->attrs);
cl->attrs = NULL;
if (cl->layout)
g_object_unref (cl->layout);
memset (cl, 0, sizeof (CellLayout));
cl->layout = NULL;
}
#ifdef CELL_LAYOUT_CACHE
@ -1446,6 +1456,7 @@ cell_layout_cache_get (GtkWidget *widget)
PangoContext *context = gtk_widget_get_pango_context (widget);
CellLayoutCache *cache;
guint serial = pango_context_get_serial (context);
guint i;
if (!cell_layout_quark)
cell_layout_quark = g_quark_from_static_string ("gtk-cell-renderer-text-layouts");
@ -1471,6 +1482,9 @@ cell_layout_cache_get (GtkWidget *widget)
cache = g_slice_new0 (CellLayoutCache);
cache->context = context;
cache->context_serial = serial;
for (i = 0; i < CELL_BUCKETS; i++)
cache->buckets[i] = -1;
cache->lru_head = cache->lru_tail = -1;
g_object_set_qdata_full (G_OBJECT (widget), cell_layout_quark,
cache, cell_layout_cache_free);
}
@ -1478,6 +1492,58 @@ cell_layout_cache_get (GtkWidget *widget)
return cache;
}
static void
cell_layout_lru_unlink (CellLayoutCache *cache,
gint i)
{
CellLayout *e = &cache->entries[i];
if (e->lru_prev >= 0)
cache->entries[e->lru_prev].lru_next = e->lru_next;
else
cache->lru_head = e->lru_next;
if (e->lru_next >= 0)
cache->entries[e->lru_next].lru_prev = e->lru_prev;
else
cache->lru_tail = e->lru_prev;
}
static void
cell_layout_lru_push (CellLayoutCache *cache,
gint i)
{
CellLayout *e = &cache->entries[i];
e->lru_prev = -1;
e->lru_next = cache->lru_head;
if (cache->lru_head >= 0)
cache->entries[cache->lru_head].lru_prev = i;
else
cache->lru_tail = i;
cache->lru_head = i;
}
static void
cell_layout_lru_touch (CellLayoutCache *cache,
gint i)
{
if (cache->lru_head == i)
return;
cell_layout_lru_unlink (cache, i);
cell_layout_lru_push (cache, i);
}
static void
cell_layout_bucket_remove (CellLayoutCache *cache,
gint i)
{
gint16 *link = &cache->buckets[cache->entries[i].hash % CELL_BUCKETS];
while (*link != i)
link = &cache->entries[*link].bucket_next;
*link = cache->entries[i].bucket_next;
}
#endif
/* The layout of the cell as it is now, from the cache or made in
@ -1588,9 +1654,9 @@ cell_layout_get (GtkCellRendererText *celltext,
#ifdef CELL_LAYOUT_CACHE
{
CellLayoutCache *cache = cell_layout_cache_get (widget);
guint i, victim = 0;
gint i, victim;
for (i = 0; i < cache->n; i++)
for (i = cache->buckets[key.hash % CELL_BUCKETS]; i >= 0; i = cache->entries[i].bucket_next)
{
CellLayout *e = &cache->entries[i];
@ -1603,19 +1669,17 @@ cell_layout_get (GtkCellRendererText *celltext,
strcmp (e->text, text) == 0 &&
pango_attr_list_equal (e->attrs, attr_list))
{
cell_layout_lru_touch (cache, i);
if (layout_serial (e->layout) == e->serial)
{
e->used = ++cache->clock;
pango_attr_list_unref (attr_list);
return e;
}
/* changed behind our back: make it again */
/* changed behind our back: make it again, in its place */
cell_layout_clear (e);
victim = i;
cl = e;
goto make;
}
if (e->used < cache->entries[victim].used)
victim = i;
}
if (cache->n < CELL_LAYOUTS)
{
@ -1626,14 +1690,20 @@ cell_layout_get (GtkCellRendererText *celltext,
}
victim = cache->n++;
memset (&cache->entries[victim], 0, sizeof (CellLayout));
cell_layout_lru_push (cache, victim);
}
else
cell_layout_clear (&cache->entries[victim]);
make:
{
victim = cache->lru_tail;
cell_layout_bucket_remove (cache, victim);
cell_layout_clear (&cache->entries[victim]);
cell_layout_lru_touch (cache, victim);
}
cache->entries[victim].bucket_next = cache->buckets[key.hash % CELL_BUCKETS];
cache->buckets[key.hash % CELL_BUCKETS] = victim;
cl = &cache->entries[victim];
cl->used = ++cache->clock;
}
make:
#endif
cl->hash = key.hash;