From 33aa90b022e7d711a733ca7eb62c0b285f974317 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Fri, 20 May 2016 22:38:55 +0200
Subject: Refreshed the widths summary of lines groups only when necessary.

---
 ChangeLog                   |   5 +++
 src/glibext/gwidthtracker.c | 102 ++++++++++++++++++++++++++++++++------------
 2 files changed, 79 insertions(+), 28 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 95bc008..4e50933 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+16-05-20  Cyrille Bagard <nocbos@gmail.com>
+
+	* src/glibext/gwidthtracker.c:
+	Refresh the widths summary of lines groups only when necessary.
+
 16-05-19  Cyrille Bagard <nocbos@gmail.com>
 
 	* plugins/mobicore/mclf.c:
diff --git a/src/glibext/gwidthtracker.c b/src/glibext/gwidthtracker.c
index 11ea0b1..10d01ab 100644
--- a/src/glibext/gwidthtracker.c
+++ b/src/glibext/gwidthtracker.c
@@ -41,6 +41,7 @@ typedef struct _common_metrics
     size_t last;                            /* Dernier indice de portion   */
 
     line_width_summary summary;             /* Compilation de largeurs     */
+    bool cached;                            /* Mise en cache des calculs   */
 
 } common_metrics;
 
@@ -86,8 +87,11 @@ static size_t g_width_tracker_find_metrics(const GWidthTracker *, size_t);
 /* Prend en compte une évolution du volume de lignes. */
 static void g_width_tracker_update_ranges(GWidthTracker *, size_t, size_t);
 
+/* Réinitialise les largeurs requises par une portion de lignes.* */
+static void g_width_tracker_reset_widths(GWidthTracker *, size_t);
+
 /* Recalcule les largeurs requises par une portion de lignes. */
-static void g_width_tracker_update_widths(GWidthTracker *, size_t);
+static const line_width_summary *g_width_tracker_get_up_to_date_widths(GWidthTracker *, size_t);
 
 /* Calcule les largeurs requises par un ensemble de lignes. */
 static void g_width_tracker_ensure_valid_required_widths(GWidthTracker *);
@@ -259,13 +263,13 @@ GWidthTracker *g_width_tracker_new_restricted(const GWidthTracker *template, siz
     if (result->portions[0].first != first)
     {
         result->portions[0].first = first;
-        g_width_tracker_update_widths(result, 0);
+        g_width_tracker_reset_widths(result, 0);
     }
 
     if (result->portions[result->count - 1].last != last)
     {
         result->portions[result->count - 1].last = last;
-        g_width_tracker_update_widths(result, result->count - 1);
+        g_width_tracker_reset_widths(result, result->count - 1);
     }
 
     return result;
@@ -351,9 +355,9 @@ static void g_width_tracker_update_ranges(GWidthTracker *tracker, size_t start,
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : tracker = gestionnaire de largeurs de lignes à mettre jour.  *
-*                index   = indice de la portion à rafraîchir.                 *
+*                index   = indice de portion à marquer pour réinitialisation. *
 *                                                                             *
-*  Description : Recalcule les largeurs requises par une portion de lignes.   *
+*  Description : Réinitialise les largeurs requises par une portion de lignes.*
 *                                                                             *
 *  Retour      : -                                                            *
 *                                                                             *
@@ -361,12 +365,10 @@ static void g_width_tracker_update_ranges(GWidthTracker *tracker, size_t start,
 *                                                                             *
 ******************************************************************************/
 
-static void g_width_tracker_update_widths(GWidthTracker *tracker, size_t index)
+static void g_width_tracker_reset_widths(GWidthTracker *tracker, size_t index)
 {
     common_metrics *portion;                /* Portion à actualiser        */
-    BufferLineColumn k;                     /* Boucle de parcours #1       */
-    size_t i;                               /* Boucle de parcours #2       */
-    GBufferLine *line;                      /* Ligne à manipuler           */
+    BufferLineColumn k;                     /* Boucle de parcours          */
 
     assert(index < tracker->count);
 
@@ -374,25 +376,69 @@ static void g_width_tracker_update_widths(GWidthTracker *tracker, size_t index)
 
     /* Réinitialisation globale ? */
 
-    for (k = 0; k < BLC_COUNT && tracker->cached; k++)
-        tracker->cached &= (tracker->summary.max_widths[k] != portion->summary.max_widths[k]);
+    if (portion->cached)
+    {
+        for (k = 0; k < BLC_COUNT && tracker->cached; k++)
+            tracker->cached &= (tracker->summary.max_widths[k] != portion->summary.max_widths[k]);
 
-    tracker->cached &= (tracker->summary.merged_width != portion->summary.merged_width);
+        tracker->cached &= (tracker->summary.merged_width != portion->summary.merged_width);
+
+    }
 
     /* Réinitialisation locale */
 
-    memset(&portion->summary, 0, sizeof(line_width_summary));
+    portion->cached = false;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : tracker = gestionnaire de largeurs de lignes à mettre jour.  *
+*                index   = indice de la portion à rafraîchir.                 *
+*                                                                             *
+*  Description : Recalcule les largeurs requises par une portion de lignes.   *
+*                                                                             *
+*  Retour      : Accès en lecture seule au résumé à jour.                     *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static const line_width_summary *g_width_tracker_get_up_to_date_widths(GWidthTracker *tracker, size_t index)
+{
+    common_metrics *portion;                /* Portion à actualiser        */
+    size_t i;                               /* Boucle de parcours          */
+    GBufferLine *line;                      /* Ligne à manipuler           */
 
-    /* Collecte */
+    assert(index < tracker->count);
 
-    for (i = portion->first; i <= portion->last; i++)
+    portion = &tracker->portions[index];
+
+    if (!portion->cached)
     {
-        line = g_code_buffer_find_line_by_index(tracker->buffer, i);
+        /* Réinitialisation locale */
 
-        g_buffer_line_collect_widths(line, &portion->summary);
+        memset(&portion->summary, 0, sizeof(line_width_summary));
+
+        /* Collecte */
+
+        for (i = portion->first; i <= portion->last; i++)
+        {
+            line = g_code_buffer_find_line_by_index(tracker->buffer, i);
+
+            g_buffer_line_collect_widths(line, &portion->summary);
+
+        }
+
+        /* Marquage pour mémoire */
+
+        portion->cached = true;
 
     }
 
+    return &portion->summary;
+
 }
 
 
@@ -431,7 +477,7 @@ void g_width_tracker_update_added(GWidthTracker *tracker, size_t index, size_t c
         tracker->portions[0].first = 0;
         tracker->portions[0].last = count - 1;
 
-        g_width_tracker_update_widths(tracker, 0);
+        g_width_tracker_reset_widths(tracker, 0);
 
         return;
 
@@ -453,7 +499,7 @@ void g_width_tracker_update_added(GWidthTracker *tracker, size_t index, size_t c
 
     portion->last += count;
 
-    g_width_tracker_update_widths(tracker, current);
+    g_width_tracker_reset_widths(tracker, current);
 
     next = current + 1;
 
@@ -506,9 +552,9 @@ void g_width_tracker_update_added(GWidthTracker *tracker, size_t index, size_t c
 
             /* Mise à jour des largeurs */
 
-            g_width_tracker_update_widths(tracker, current);
+            g_width_tracker_reset_widths(tracker, current);
 
-            g_width_tracker_update_widths(tracker, current + 1);
+            g_width_tracker_reset_widths(tracker, current + 1);
 
         }
 
@@ -614,10 +660,10 @@ void g_width_tracker_update_deleted(GWidthTracker *tracker, size_t start, size_t
     /* Mise à jour des largeurs aux extrémités */
 
     if (keep_first)
-        g_width_tracker_update_widths(tracker, first);
+        g_width_tracker_reset_widths(tracker, first);
 
     if (keep_last && last != first)
-        g_width_tracker_update_widths(tracker, dest);
+        g_width_tracker_reset_widths(tracker, dest);
 
 }
 
@@ -653,7 +699,7 @@ static void g_width_tracker_ensure_valid_required_widths(GWidthTracker *tracker)
 
         for (i = 0; i < tracker->count; i++)
         {
-            summary = &tracker->portions[i].summary;
+            summary = g_width_tracker_get_up_to_date_widths(tracker, i);
 
             for (k = 0; k < BLC_COUNT; k++)
                 global->max_widths[k] = MAX(global->max_widths[k], summary->max_widths[k]);
@@ -707,7 +753,7 @@ const line_width_summary *g_width_tracker_get_width_summary(GWidthTracker *track
 void g_width_tracker_get_local_width_summary(GWidthTracker *tracker, size_t index, line_width_summary *summary)
 {
     size_t current;                         /* Indice de portion visée     */
-    common_metrics *portion;                /* Portion locale à consulter  */
+    const line_width_summary *local;        /* Valeurs à intégrer          */
     BufferLineColumn i;                     /* Boucle de parcours          */
 
     g_width_tracker_ensure_valid_required_widths(tracker);
@@ -715,15 +761,15 @@ void g_width_tracker_get_local_width_summary(GWidthTracker *tracker, size_t inde
     current = g_width_tracker_find_metrics(tracker, index);
     assert(current < tracker->count);
 
-    portion = &tracker->portions[current];
+    local = g_width_tracker_get_up_to_date_widths(tracker, current);
 
     for (i = BLC_FIRST; i < BLC_DISPLAY; i++)
         summary->max_widths[i] = tracker->summary.max_widths[i];
 
     for (i = BLC_DISPLAY; i < BLC_COUNT; i++)
-        summary->max_widths[i] = portion->summary.max_widths[i];
+        summary->max_widths[i] = local->max_widths[i];
 
-    summary->merged_width = portion->summary.merged_width;
+    summary->merged_width = local->merged_width;
 
 }
 
-- 
cgit v0.11.2-87-g4458