[pango/speed-up-format-filtering: 4/8] Move expensive fontconfig calls to a thread
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pango/speed-up-format-filtering: 4/8] Move expensive fontconfig calls to a thread
- Date: Sat, 22 Aug 2020 01:51:35 +0000 (UTC)
commit b2f23e5de0a7860cec51481209634c02767731cb
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Aug 18 21:55:58 2020 -0400
Move expensive fontconfig calls to a thread
fontconfig is thread-safe, so we can do calls that
can take several milliseconds to complete to a thread,
such as FcFontSetMatch and FcFontSetSort. This patch
is a win, at least for FcFontSetSort, since it reduces
the time that the main thread spends in
pango_fc_patterns_get_font_pattern for i > 0 from
around 10 to 6 milliseconds.
For bigger wins from this, we would have to restructure
things to allow itemization to proceed without blocking
on fonts being available.
pango/pangofc-fontmap.c | 207 +++++++++++++++++++++++++++++++++++++++---------
1 file changed, 168 insertions(+), 39 deletions(-)
---
diff --git a/pango/pangofc-fontmap.c b/pango/pangofc-fontmap.c
index ca46f184..95ef03f2 100644
--- a/pango/pangofc-fontmap.c
+++ b/pango/pangofc-fontmap.c
@@ -743,15 +743,129 @@ struct _PangoFcPatterns {
PangoFcFontMap *fontmap;
+ /* match and fontset are initialized in a thread,
+ * and are protected by a mutex. The thread signals
+ * the cond when match or fonset become available.
+ */
+ GMutex mutex;
+ GCond cond;
+
FcPattern *pattern;
FcPattern *match;
FcFontSet *fontset;
};
+static FcFontSet *
+font_set_copy (FcFontSet *fontset)
+{
+ FcFontSet *copy;
+ int i;
+
+ if (!fontset)
+ return NULL;
+
+ copy = malloc (sizeof (FcFontSet));
+ copy->sfont = copy->nfont = fontset->nfont;
+ copy->fonts = malloc (sizeof (FcPattern *) * copy->nfont);
+ memcpy (copy->fonts, fontset->fonts, sizeof (FcPattern *) * copy->nfont);
+ for (i = 0; i < copy->nfont; i++)
+ FcPatternReference (copy->fonts[i]);
+
+ return copy;
+}
+
+typedef struct {
+ FcConfig *config;
+ FcFontSet *sets[2];
+ int nsets;
+ FcPattern *pattern;
+ PangoFcPatterns *patterns;
+} ThreadData;
+
+static FcFontSet **pango_fc_font_map_get_config_fonts (PangoFcFontMap *fcfontmap,
+ int *nsets);
+
+static ThreadData *
+thread_data_new (PangoFcPatterns *patterns)
+{
+ ThreadData *td;
+ FcFontSet **sets;
+ int i;
+
+ td = g_new (ThreadData, 1);
+ td->patterns = pango_fc_patterns_ref (patterns);
+ td->pattern = FcPatternDuplicate (patterns->pattern);
+ td->config = FcConfigReference (pango_fc_font_map_get_config (patterns->fontmap));
+ sets = pango_fc_font_map_get_config_fonts (patterns->fontmap, &td->nsets);
+ for (i = 0; i < td->nsets; i++)
+ td->sets[i] = font_set_copy (sets[i]);
+
+ return td;
+}
+
+static void
+thread_data_free (gpointer data)
+{
+ ThreadData *td = data;
+ int i;
+
+ for (i = 0; i < td->nsets; i++)
+ FcFontSetDestroy (td->sets[i]);
+ FcPatternDestroy (td->pattern);
+ FcConfigDestroy (td->config);
+ pango_fc_patterns_unref (td->patterns);
+ g_free (td);
+}
+
+static void
+match_in_thread (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ ThreadData *td = task_data;
+ FcResult result;
+ FcPattern *match;
+
+ match = FcFontSetMatch (td->config,
+ td->sets, td->nsets,
+ td->pattern,
+ &result);
+
+ g_mutex_lock (&td->patterns->mutex);
+ td->patterns->match = match;
+ g_cond_signal (&td->patterns->cond);
+ g_mutex_unlock (&td->patterns->mutex);
+}
+
+static void
+sort_in_thread (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ ThreadData *td = task_data;
+ FcResult result;
+ FcFontSet *fontset;
+
+ fontset = FcFontSetSort (td->config,
+ td->sets, td->nsets,
+ td->pattern,
+ FcTrue,
+ NULL,
+ &result);
+
+ g_mutex_lock (&td->patterns->mutex);
+ td->patterns->fontset = fontset;
+ g_cond_signal (&td->patterns->cond);
+ g_mutex_unlock (&td->patterns->mutex);
+}
+
static PangoFcPatterns *
pango_fc_patterns_new (FcPattern *pat, PangoFcFontMap *fontmap)
{
PangoFcPatterns *pats;
+ GTask *task;
pat = uniquify_pattern (fontmap, pat);
pats = g_hash_table_lookup (fontmap->priv->patterns_hash, pat);
@@ -766,8 +880,23 @@ pango_fc_patterns_new (FcPattern *pat, PangoFcFontMap *fontmap)
FcPatternReference (pat);
pats->pattern = pat;
+ g_mutex_init (&pats->mutex);
+ g_cond_init (&pats->cond);
+
+ task = g_task_new (NULL, NULL, NULL, NULL);
+ g_task_set_name (task, "[pango] FcFontSetMatch");
+ g_task_set_task_data (task, thread_data_new (pats), thread_data_free);
+ g_task_run_in_thread (task, match_in_thread);
+ g_object_unref (task);
+
+ task = g_task_new (NULL, NULL, NULL, NULL);
+ g_task_set_name (task, "[pango] FcFontSetSort");
+ g_task_set_task_data (task, thread_data_new (pats), thread_data_free);
+ g_task_run_in_thread (task, sort_in_thread);
+ g_object_unref (task);
+
g_hash_table_insert (fontmap->priv->patterns_hash,
- pats->pattern, pats);
+ pats->pattern, pats);
return pats;
}
@@ -808,6 +937,9 @@ pango_fc_patterns_unref (PangoFcPatterns *pats)
if (pats->fontset)
FcFontSetDestroy (pats->fontset);
+ g_cond_clear (&pats->cond);
+ g_mutex_clear (&pats->mutex);
+
g_slice_free (PangoFcPatterns, pats);
}
@@ -838,9 +970,6 @@ pango_fc_is_supported_font_format (FcPattern* pattern)
return FALSE;
}
-static FcFontSet **pango_fc_font_map_get_config_fonts (PangoFcFontMap *fcfontmap,
- int *nsets);
-
static FcFontSet *
filter_fontset_by_format (FcFontSet *fontset)
{
@@ -869,54 +998,54 @@ pango_fc_patterns_get_font_pattern (PangoFcPatterns *pats, int i, gboolean *prep
{
if (i == 0)
{
- FcResult result;
+ FcPattern *match;
+ FcFontSet *fontset;
- if (!pats->match && !pats->fontset)
- {
- FcFontSet **sets;
- int nsets;
+ g_mutex_lock (&pats->mutex);
- sets = pango_fc_font_map_get_config_fonts (pats->fontmap, &nsets);
+ while (!pats->match && !pats->fontset)
+ g_cond_wait (&pats->cond, &pats->mutex);
- pats->match = FcFontSetMatch (pats->fontmap->priv->config,
- sets, nsets,
- pats->pattern,
- &result);
- }
+ match = pats->match;
+ fontset = pats->fontset;
+
+ g_mutex_unlock (&pats->mutex);
- if (pats->match)
+ if (match)
{
*prepare = FALSE;
- return pats->match;
+ return match;
}
- }
- if (!pats->fontset)
+ if (fontset && i < fontset->nfont)
+ {
+ *prepare = TRUE;
+ return fontset->fonts[i];
+ }
+ else
+ return NULL;
+ }
+ else
{
- FcResult result;
- FcFontSet **sets;
- int nsets;
+ FcFontSet *fontset;
+
+ g_mutex_lock (&pats->mutex);
+
+ while (!pats->fontset)
+ g_cond_wait (&pats->cond, &pats->mutex);
- sets = pango_fc_font_map_get_config_fonts (pats->fontmap, &nsets);
+ fontset = pats->fontset;
- pats->fontset = FcFontSetSort (pats->fontmap->priv->config,
- sets, nsets,
- pats->pattern,
- FcTrue, NULL,
- &result);
+ g_mutex_unlock (&pats->mutex);
- if (pats->match)
+ if (fontset && i < fontset->nfont)
{
- FcPatternDestroy (pats->match);
- pats->match = NULL;
+ *prepare = TRUE;
+ return fontset->fonts[i];
}
+ else
+ return NULL;
}
-
- *prepare = TRUE;
- if (pats->fontset && i < pats->fontset->nfont)
- return pats->fontset->fonts[i];
- else
- return NULL;
}
@@ -2011,7 +2140,7 @@ pango_fc_font_map_cache_clear (PangoFcFontMap *fcfontmap)
pango_fc_font_map_fini (fcfontmap);
pango_fc_font_map_init (fcfontmap);
-
+
ensure_families (fcfontmap);
added = fcfontmap->priv->n_families;
@@ -2068,7 +2197,7 @@ pango_fc_font_map_config_changed (PangoFcFontMap *fcfontmap)
**/
void
pango_fc_font_map_set_config (PangoFcFontMap *fcfontmap,
- FcConfig *fcconfig)
+ FcConfig *fcconfig)
{
FcConfig *oldconfig;
int i;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]