[gnome-builder] results: avoid vfunc when sorting



commit cd5229030a1e218b4f08028dcc0010dd8f8a155f
Author: Christian Hergert <christian hergert me>
Date:   Thu Oct 1 18:30:39 2015 -0700

    results: avoid vfunc when sorting
    
    We can avoid the vfunc dereference in some cases easily by checking if
    the compare func is null. Next step is to implement the sort ourself
    so we can inline the whole thing.

 libide/ide-completion-results.c |   42 ++++++++++++++++++++++++--------------
 1 files changed, 26 insertions(+), 16 deletions(-)
---
diff --git a/libide/ide-completion-results.c b/libide/ide-completion-results.c
index 3abbcd4..2c7db8e 100644
--- a/libide/ide-completion-results.c
+++ b/libide/ide-completion-results.c
@@ -285,6 +285,19 @@ ide_completion_results_refilter (IdeCompletionResults *self)
 }
 
 static gint
+compare_fast (const IdeCompletionItem *left,
+              const IdeCompletionItem *right)
+{
+  if (left->priority < right->priority)
+    return -1;
+  else if (left->priority > right->priority)
+    return 1;
+  else
+    return 0;
+}
+
+
+static gint
 ide_completion_results_sorter (gconstpointer a,
                                gconstpointer b,
                                gpointer      user_data)
@@ -298,10 +311,22 @@ static void
 ide_completion_results_resort (IdeCompletionResults *self)
 {
   IdeCompletionResultsPrivate *priv = ide_completion_results_get_instance_private (self);
+  IdeCompletionResultsClass *klass;
 
   g_assert (IDE_IS_COMPLETION_RESULTS (self));
 
-  priv->head = g_list_sort_with_data (priv->head, ide_completion_results_sorter, self);
+  klass = IDE_COMPLETION_RESULTS_GET_CLASS (self);
+
+  /*
+   * Instead of invoking the vfunc for every item, save ourself an extra
+   * dereference and call g_list_sort() directly with our compare funcs.
+   *
+   * TODO: Next step would be to use an inline sort instead of g_list_sort.
+   */
+  if (G_LIKELY (klass->compare == NULL))
+    priv->head = g_list_sort (priv->head, (GCompareFunc)compare_fast);
+  else
+    priv->head = g_list_sort_with_data (priv->head, ide_completion_results_sorter, self);
 }
 
 void
@@ -332,19 +357,6 @@ ide_completion_results_present (IdeCompletionResults        *self,
   gtk_source_completion_context_add_proposals (context, provider, priv->head, TRUE);
 }
 
-static gint
-ide_completion_results_compare (IdeCompletionResults *self,
-                                IdeCompletionItem    *left,
-                                IdeCompletionItem    *right)
-{
-  if (left->priority < right->priority)
-    return -1;
-  else if (left->priority > right->priority)
-    return 1;
-  else
-    return 0;
-}
-
 static void
 ide_completion_results_get_property (GObject    *object,
                                      guint       prop_id,
@@ -392,8 +404,6 @@ ide_completion_results_class_init (IdeCompletionResultsClass *klass)
   object_class->get_property = ide_completion_results_get_property;
   object_class->set_property = ide_completion_results_set_property;
 
-  klass->compare = ide_completion_results_compare;
-
   gParamSpecs [PROP_QUERY] =
     g_param_spec_string ("query",
                          "Query",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]