GtkWidget: Precompile template xml on class creation
authorAlexander Larsson <alexl@redhat.com>
Thu, 29 Aug 2019 14:19:33 +0000 (16:19 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 10 Sep 2019 16:08:20 +0000 (12:08 -0400)
Ideally we will precompile during build and store the result in the
resource, but if that doesn't happen at least we will only parse
the xml once.

gtk/gtkwidget.c

index f1111277bac1aac8bd4d9d5fcc91296445600a3f..18dde07f4c26aca34dc286f41efcd3d0f0ad69bd 100644 (file)
@@ -12227,12 +12227,32 @@ void
 gtk_widget_class_set_template (GtkWidgetClass    *widget_class,
                               GBytes            *template_bytes)
 {
+  GBytes *data = NULL;
+
   g_return_if_fail (GTK_IS_WIDGET_CLASS (widget_class));
   g_return_if_fail (widget_class->priv->template == NULL);
   g_return_if_fail (template_bytes != NULL);
 
   widget_class->priv->template = g_slice_new0 (GtkWidgetTemplate);
-  widget_class->priv->template->data = g_bytes_ref (template_bytes);
+
+  if (!_gtk_buildable_parser_is_precompiled (g_bytes_get_data (template_bytes, NULL), g_bytes_get_size (template_bytes)))
+    {
+      GError *error = NULL;
+
+      data = _gtk_buildable_parser_precompile (g_bytes_get_data (template_bytes, NULL),
+                                               g_bytes_get_size (template_bytes),
+                                               &error);
+      if (data == NULL)
+        {
+          g_warning ("Failed to precompile template for class %s: %s", G_OBJECT_CLASS_NAME (widget_class), error->message);
+          g_error_free (error);
+        }
+    }
+
+  if (data)
+    widget_class->priv->template->data = data;
+  else
+    widget_class->priv->template->data = g_bytes_ref (template_bytes);
 }
 
 /**