[gtk/text-layout-speedup] textlayout: Avoid some iter comparisons




commit 72b909ea3556a70970c6650661150cb170e28a8f
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Sep 11 15:35:44 2020 -0400

    textlayout: Avoid some iter comparisons
    
    We were doing more iter comparisons than necessary
    in the inner loop of gtk_text_layout_snapshot(), in
    the presence of a selection.

 gtk/gtktextlayout.c | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)
---
diff --git a/gtk/gtktextlayout.c b/gtk/gtktextlayout.c
index 3e77b52f6c..4eb434de34 100644
--- a/gtk/gtktextlayout.c
+++ b/gtk/gtktextlayout.c
@@ -964,7 +964,7 @@ gtk_text_layout_validate_yrange (GtkTextLayout *layout,
     y0 = 0;
   if (y1 < 0)
     y1 = 0;
-  
+
   /* Validate backwards from the anchor line to y0
    */
   line = _gtk_text_iter_get_text_line (anchor);
@@ -4138,6 +4138,7 @@ gtk_text_layout_snapshot (GtkTextLayout      *layout,
   GtkStyleContext *context;
   int offset_y;
   GtkTextIter selection_start, selection_end;
+  gboolean seen_selection_start, seen_selection_end;
   gboolean have_selection;
   GSList *line_list;
   GSList *tmp_list;
@@ -4173,6 +4174,7 @@ gtk_text_layout_snapshot (GtkTextLayout      *layout,
   have_selection = gtk_text_buffer_get_selection_bounds (layout->buffer,
                                                          &selection_start,
                                                          &selection_end);
+  seen_selection_start = seen_selection_end = FALSE;
 
   tmp_list = line_list;
   while (tmp_list != NULL)
@@ -4188,7 +4190,7 @@ gtk_text_layout_snapshot (GtkTextLayout      *layout,
         {
           g_assert (line_display->layout != NULL);
 
-          if (have_selection)
+          if (have_selection && !seen_selection_end)
             {
               GtkTextIter line_start, line_end;
               int byte_count;
@@ -4199,19 +4201,26 @@ gtk_text_layout_snapshot (GtkTextLayout      *layout,
                 gtk_text_iter_forward_to_line_end (&line_end);
               byte_count = gtk_text_iter_get_visible_line_index (&line_end);
 
-              if (gtk_text_iter_compare (&selection_start, &line_end) <= 0 &&
-                  gtk_text_iter_compare (&selection_end, &line_start) >= 0)
+              if (seen_selection_start)
+                selection_start_index = 0;
+              else if (gtk_text_iter_compare (&selection_start, &line_end) <= 0)
                 {
-                  if (gtk_text_iter_compare (&selection_start, &line_start) >= 0)
-                    selection_start_index = gtk_text_iter_get_visible_line_index (&selection_start);
-                  else
-                    selection_start_index = -1;
-
-                  if (gtk_text_iter_compare (&selection_end, &line_end) <= 0)
-                    selection_end_index = gtk_text_iter_get_visible_line_index (&selection_end);
-                  else
-                    selection_end_index = byte_count + 1; /* + 1 to flag past-the-end */
+                  selection_start_index = gtk_text_iter_get_visible_line_index (&selection_start);
+                  seen_selection_start = TRUE;
                 }
+              else
+                selection_start_index = -1;
+
+              if (seen_selection_start &&
+                  gtk_text_iter_compare (&selection_end, &line_end) <= 0)
+                {
+                  selection_end_index = gtk_text_iter_get_visible_line_index (&selection_end);
+                  seen_selection_end = TRUE;
+                }
+              else if (seen_selection_start)
+                selection_end_index = byte_count + 1; /* + 1 to flag past-the-end */
+              else
+                selection_end_index = -1;
             }
 
           if (line_display->node == NULL)


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