[meld: 1/12] issue: #dirdiff.fast_insert - make append faster; commit: using unsafe_set_value on set_item_state



commit 943687d6c12dc92cb9fa9d4f141f8eeb95722055
Author: hugosenari <hugosenari gmail com>
Date:   Thu Aug 2 04:21:22 2018 -0300

    issue: #dirdiff.fast_insert - make append faster; commit: using unsafe_set_value on set_item_state

 meld/tree.py        | 26 ++++++++++++++++----------
 meld/treehelpers.py | 30 ++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 10 deletions(-)
---
diff --git a/meld/tree.py b/meld/tree.py
index f75bf3b9..32371645 100644
--- a/meld/tree.py
+++ b/meld/tree.py
@@ -43,6 +43,7 @@ class DiffTreeStore(SearchableTreeStore):
         for col_type in (COL_TYPES + tuple(types)):
             full_types.extend([col_type] * ntree)
         super().__init__(*full_types)
+        self.set_none_of_cols(full_types)
         self.ntree = ntree
         self._setup_default_styles()
 
@@ -116,14 +117,17 @@ class DiffTreeStore(SearchableTreeStore):
     def add_entries(self, parent, names):
         child = self.append(parent)
         for pane, path in enumerate(names):
-            self.set_value(child, self.column_index(COL_PATH, pane), path)
+            column = self.column_index(COL_PATH, pane)
+            self.unsafe_set_value(child, column, path)
         return child
 
     def add_empty(self, parent, text="empty folder"):
         it = self.append(parent)
         for pane in range(self.ntree):
-            self.set_value(it, self.column_index(COL_PATH, pane), None)
+            column = self.column_index(COL_PATH, pane)
+            self.set_value(it, column, None)
             self.set_state(it, pane, STATE_EMPTY, text)
+        return it
 
     def add_error(self, parent, msg, pane):
         it = self.append(parent)
@@ -139,19 +143,21 @@ class DiffTreeStore(SearchableTreeStore):
         self.set_state(it, pane, state, display_text, isdir)
 
     def set_state(self, it, pane, state, label, isdir=0):
+        if not it:
+            return None
         col_idx = self.column_index
         icon = self.icon_details[state][1 if isdir else 0]
         tint = self.icon_details[state][3 if isdir else 2]
-        self.set_value(it, col_idx(COL_STATE, pane), str(state))
-        self.set_value(it, col_idx(COL_TEXT,  pane), label)
-        self.set_value(it, col_idx(COL_ICON,  pane), icon)
-        self.set_value(it, col_idx(COL_TINT, pane), tint)
+        self.unsafe_set_value(it, col_idx(COL_STATE, pane), str(state))
+        self.unsafe_set_value(it, col_idx(COL_TEXT,  pane), label)
+        self.unsafe_set_value(it, col_idx(COL_ICON,  pane), icon)
+        self.unsafe_set_value(it, col_idx(COL_TINT, pane), tint)
 
         fg, style, weight, strike = self.text_attributes[state]
-        self.set_value(it, col_idx(COL_FG, pane), fg)
-        self.set_value(it, col_idx(COL_STYLE, pane), style)
-        self.set_value(it, col_idx(COL_WEIGHT, pane), weight)
-        self.set_value(it, col_idx(COL_STRIKE, pane), strike)
+        self.unsafe_set_value(it, col_idx(COL_FG, pane), fg)
+        self.unsafe_set_value(it, col_idx(COL_STYLE, pane), style)
+        self.unsafe_set_value(it, col_idx(COL_WEIGHT, pane), weight)
+        self.unsafe_set_value(it, col_idx(COL_STRIKE, pane), strike)
 
     def get_state(self, it, pane):
         state_idx = self.column_index(COL_STATE, pane)
diff --git a/meld/treehelpers.py b/meld/treehelpers.py
index abf0787b..10514ee9 100644
--- a/meld/treehelpers.py
+++ b/meld/treehelpers.py
@@ -15,6 +15,11 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from gi.repository import Gtk
+from gi.repository import GObject
+from gi.module import get_introspection_module
+
+
+_GIGtk = get_introspection_module('Gtk')
 
 
 def tree_path_as_tuple(path):
@@ -71,6 +76,12 @@ def refocus_deleted_path(model, path):
 
 class SearchableTreeStore(Gtk.TreeStore):
 
+    def set_none_of_cols(self, types):
+        self._none_of_cols = {
+            col_num: GObject.Value(col_type, None)
+            for col_num, col_type in enumerate(types)
+        }
+
     def inorder_search_down(self, it):
         while it:
             child = self.iter_children(it)
@@ -130,3 +141,22 @@ class SearchableTreeStore(Gtk.TreeStore):
                 break
 
         return prev_path, next_path
+
+    def unsafe_set_value(self, treeiter, column, value):
+        """ This must be fastest than super.set_value,
+        at the cost that may crash the application if you don't
+        know what your're passing here.
+        ie: pass treeiter or column as None crash meld
+
+        treeiter: Gtk.TreeIter
+        column: Int col index
+        value: Str (UTF-8), Int, Float, Double, Boolean or GObject
+
+        return None
+        """
+        if value is None and hasattr(self, '_none_of_cols'):
+            value = self._none_of_cols.get(column)
+        if value is None:
+            self.set_value(treeiter, column, value)
+        else:
+            _GIGtk.TreeStore.set_value(self, treeiter, column, value)


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