[gtksourceview] view: short-circuit updating visible highlights



commit f932480f0e4bcfa4981bf7f403e61fb6711aa6ee
Author: Christian Hergert <chergert redhat com>
Date:   Sat Feb 27 13:51:02 2021 -0800

    view: short-circuit updating visible highlights
    
    If we can avoid the lookups by Y coordinates then we should as they can
    be quite expensive.

 gtksourceview/gtksourcebuffer-private.h |  2 ++
 gtksourceview/gtksourcebuffer.c         | 21 +++++++++++++++++++--
 gtksourceview/gtksourceview.c           | 11 +++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer-private.h b/gtksourceview/gtksourcebuffer-private.h
index 4a0765f0..15073c08 100644
--- a/gtksourceview/gtksourcebuffer-private.h
+++ b/gtksourceview/gtksourcebuffer-private.h
@@ -33,6 +33,8 @@ void                      _gtk_source_buffer_update_syntax_highlight     (GtkSou
                                                                           const GtkTextIter      *end,
                                                                           gboolean                
synchronous);
 GTK_SOURCE_INTERNAL
+gboolean                  _gtk_source_buffer_has_search_highlights       (GtkSourceBuffer        *buffer);
+GTK_SOURCE_INTERNAL
 void                      _gtk_source_buffer_update_search_highlight     (GtkSourceBuffer        *buffer,
                                                                           const GtkTextIter      *start,
                                                                           const GtkTextIter      *end,
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index b935a684..d5751870 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -1563,11 +1563,10 @@ _gtk_source_buffer_update_search_highlight (GtkSourceBuffer   *buffer,
                                            gboolean           synchronous)
 {
        GtkSourceBufferPrivate *priv = gtk_source_buffer_get_instance_private (buffer);
-       GList *l;
 
        g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
 
-       for (l = priv->search_contexts; l != NULL; l = l->next)
+       for (GList *l = priv->search_contexts; l != NULL; l = l->next)
        {
                GtkSourceSearchContext *search_context = l->data;
 
@@ -1578,6 +1577,24 @@ _gtk_source_buffer_update_search_highlight (GtkSourceBuffer   *buffer,
        }
 }
 
+gboolean
+_gtk_source_buffer_has_search_highlights (GtkSourceBuffer *buffer)
+{
+       GtkSourceBufferPrivate *priv = gtk_source_buffer_get_instance_private (buffer);
+
+       g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
+
+       for (GList *l = priv->search_contexts; l != NULL; l = l->next)
+       {
+               GtkSourceSearchContext *search_context = l->data;
+
+               if (gtk_source_search_context_get_highlight (search_context))
+                       return TRUE;
+       }
+
+       return FALSE;
+}
+
 /**
  * gtk_source_buffer_ensure_highlight:
  * @buffer: a #GtkSourceBuffer.
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index 19c91cc3..e4292dd2 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -2252,6 +2252,17 @@ gtk_source_view_ensure_redrawn_rect_is_highlighted (GtkSourceView *view,
 
        GTK_SOURCE_PROFILER_BEGIN_MARK;
 
+       /* If there is nothing to update here in terms of highlighting, then we can
+        * avoid some expensive operations such as looking up iters by location.
+        * Inside of test-widget, this function can easily take .5msec according to
+        * profiling data.
+        */
+       if (!gtk_source_buffer_get_highlight_syntax (priv->source_buffer) &&
+           !_gtk_source_buffer_has_search_highlights (priv->source_buffer))
+       {
+               return;
+       }
+
        gtk_text_view_get_line_at_y (GTK_TEXT_VIEW (view), &iter1, clip->y, NULL);
        gtk_text_iter_backward_line (&iter1);
        gtk_text_view_get_line_at_y (GTK_TEXT_VIEW (view), &iter2, clip->y + clip->height, NULL);


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