Performance optimization: reducing over a string allocates a bunch of 1-character strings and is vast overkill. Use find instead.
authorBenjamin Smedberg <benjamin@smedbergs.us>
Wed, 25 Feb 2009 16:20:43 -0500
changeset 179 56f8cbd6b7b7
parent 178 e373af4da9fc
child 180 79707812e432
push id103
push userbsmedberg@mozilla.com
push dateWed, 25 Feb 2009 21:20:57 +0000
Performance optimization: reducing over a string allocates a bunch of 1-character strings and is vast overkill. Use find instead.
pymake/parserdata.py
--- a/pymake/parserdata.py
+++ b/pymake/parserdata.py
@@ -1,26 +1,16 @@
 import logging, re
 import data, functions, util, parser
 from cStringIO import StringIO
 from pymake.globrelative import hasglob, glob
 
 _log = logging.getLogger('pymake.data')
 _tabwidth = 4
 
-def _charlocation(start, char):
-    """
-    Return the column position after processing a perhaps-tab character.
-    This function is meant to be used with reduce().
-    """
-    if char != '\t':
-        return start + 1
-
-    return start + _tabwidth - start % _tabwidth
-
 class Location(object):
     """
     A location within a makefile.
 
     For the moment, locations are just path/line/column, but in the future
     they may reference parent locations for more accurate "included from"
     or "evaled at" error reporting.
     """
@@ -31,20 +21,32 @@ class Location(object):
         self.line = line
         self.column = column
 
     def __add__(self, data):
         """
         Returns a new location on the same line offset by
         the specified string.
         """
-        newcol = reduce(_charlocation, data, self.column)
-        if newcol == self.column:
+        column = self.column
+        i = 0
+        while True:
+            j = data.find('\t', i)
+            if j == -1:
+                column += len(data) - i
+                break
+
+            column += j - i
+            column += _tabwidth
+            column -= column % _tabwidth
+            i = j + 1
+
+        if column == self.column:
             return self
-        return Location(self.path, self.line, newcol)
+        return Location(self.path, self.line, column)
 
     def __str__(self):
         return "%s:%s:%s" % (self.path, self.line, self.column)
 
 def _expandwildcards(makefile, tlist):
     for t in tlist:
         if not hasglob(t):
             yield t