Commits

Yuya Nishihara committed 32c7eba

fileview: speed up displayFile() by eliminating extra fm.width() calls

Because QFontMetrics::width() is very expensive, it should search the possible
longest line first and apply fm.width() to it.

Also, Python's splitlines() seems slightly faster than QString's split('\n').

It saves 260msec (400msec - 140msec) for text file of 953kB, 25000 lines.

Comments (0)

Files changed (1)

tortoisehg/hgqt/fileview.py

             font = self.sci.font()
 
         fm = QFontMetrics(font)
-        maxWidth = fm.maxWidth()
-        lines = self.sci.text().split('\n')
-        widths = [fm.width(line) + maxWidth for line in lines]
-        self.maxWidth = max(widths)
+        self.maxWidth = fm.maxWidth()
+        lines = unicode(self.sci.text()).splitlines()
+        if lines:
+            # assume that the longest line has the largest width;
+            # fm.width() is too slow to apply to each line.
+            try:
+                longestline = max(lines, key=len)
+            except TypeError:  # Python<2.5 has no key support
+                longestline = max((len(l), l) for l in lines)[1]
+            self.maxWidth += fm.width(longestline)
         self.updateScrollBar()
 
     #