[latexila] Structure: model doesn't emit signals when not connected to the view



commit 3e3d47a0e7ef89db715c22593adda6d1fa62e226
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Sat Jul 23 03:13:22 2011 +0200

    Structure: model doesn't emit signals when not connected to the view
    
    And it improves a lot the performances! For normal documents, however,
    the difference is not big. But with some stressful files, now the
    performances increases almost linearly as the number of lines increases
    (before, with the same files, it was exponential).

 src/structure.vala       |    3 ++
 src/structure_model.vala |   48 +++++++++++++++++++++++++++++++--------------
 2 files changed, 36 insertions(+), 15 deletions(-)
---
diff --git a/src/structure.vala b/src/structure.vala
index d983385..04096c6 100644
--- a/src/structure.vala
+++ b/src/structure.vala
@@ -510,6 +510,9 @@ public class Structure : VBox
 
     private void set_model (StructureModel? model)
     {
+        if (model != null)
+            model.emit_signals = true;
+
         _model = model;
         _tree_view.set_model (model);
 
diff --git a/src/structure_model.vala b/src/structure_model.vala
index 6ff5a81..86ffcf6 100644
--- a/src/structure_model.vala
+++ b/src/structure_model.vala
@@ -53,6 +53,11 @@ public errordomain StructError {
 
 public class StructureModel : TreeModel, GLib.Object
 {
+    // This model is connected to the view only when the parsing is done. So it is useless
+    // to emit signals during the initial parsing. Emitting a signal can be slow, for
+    // example to get the TreePath (a signal parameter), a O(N) GNode function is used...
+    public bool emit_signals { get; set; default = false; }
+
     private Type[] _column_types;
     private Node<StructData?> _tree;
     private int _stamp;
@@ -437,7 +442,9 @@ public class StructureModel : TreeModel, GLib.Object
         new_stamp ();
         node.data.text = text;
         node.data.end_mark = end_mark;
-        row_changed (path, iter);
+
+        if (emit_signals)
+            row_changed (path, iter);
 
         make_children_between_marks (node);
     }
@@ -560,6 +567,10 @@ public class StructureModel : TreeModel, GLib.Object
     private void insert_node (Node<StructData?> node, bool force_first_child = false)
     {
         new_stamp ();
+        _nb_nodes++;
+
+        if (! emit_signals)
+            return;
 
         TreeIter item_iter = create_iter_at_node (node);
         TreePath item_path = get_path (item_iter);
@@ -568,37 +579,44 @@ public class StructureModel : TreeModel, GLib.Object
         // Attention, the row-has-child-toggled signal must be emitted _after_,
         // else there are strange errors.
         unowned Node<StructData?> parent = node.parent;
-        bool first_child = parent != _tree && parent.n_children () == 1;
+        bool first_child = parent != _tree && parent.children == node;
         if (force_first_child || first_child)
         {
             TreeIter parent_iter = create_iter_at_node (parent);
             TreePath parent_path = get_path (parent_iter);
             row_has_child_toggled (parent_path, parent_iter);
         }
-
-        _nb_nodes++;
     }
 
     private Node<StructData?>? delete_node (Node<StructData?> node)
     {
         new_stamp ();
 
-        TreeIter? iter = create_iter_at_node (node);
-        return_val_if_fail (iter != null, null);
+        TreePath path = null;
+        unowned Node<StructData?> parent = null;
+        if (emit_signals)
+        {
+            TreeIter? iter = create_iter_at_node (node);
+            return_val_if_fail (iter != null, null);
+
+            path = get_path (iter);
+            parent = node.parent;
+        }
 
-        TreePath path = get_path (iter);
-        unowned Node<StructData?> parent = node.parent;
         Node<StructData?> node_unlinked = node.unlink ();
-        row_deleted (path);
+        _nb_nodes -= node_unlinked.n_nodes (TraverseFlags.ALL);
 
-        if (parent != _tree && parent.n_children () == 0)
+        if (emit_signals)
         {
-            TreeIter parent_iter = create_iter_at_node (parent);
-            TreePath parent_path = get_path (parent_iter);
-            row_has_child_toggled (parent_path, parent_iter);
-        }
+            row_deleted (path);
 
-        _nb_nodes -= node_unlinked.n_nodes (TraverseFlags.ALL);
+            if (parent != _tree && parent.children == null)
+            {
+                TreeIter parent_iter = create_iter_at_node (parent);
+                TreePath parent_path = get_path (parent_iter);
+                row_has_child_toggled (parent_path, parent_iter);
+            }
+        }
 
         return node_unlinked;
     }



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