Optimize out N*M suffix matching in Builder.py.
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Fri, 4 Mar 2005 21:27:30 +0000 (21:27 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Fri, 4 Mar 2005 21:27:30 +0000 (21:27 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@1241 fdb21ef1-2011-0410-befe-b5e4ea1792b1

bin/objcounts.py
src/engine/SCons/Builder.py
src/engine/SCons/Debug.py
src/engine/SCons/Script/Main.py

index 1b72837a10da7831b06ae282033f48e6a8afbb18..db02aab4fc21b192791aa58fa20f9e33e7a0f09d 100644 (file)
@@ -35,7 +35,7 @@ Compare the --debug=object counts from two build logs.
 
 def fetch_counts(fname):
     contents = open(fname).read()
-    m = re.search('\nObject counts:\n(.*)\n[^\s]', contents, re.S)
+    m = re.search('\nObject counts:(\n\s[^\n]*)*', contents, re.S)
     lines = m.group().split('\n')
     list = [l.split() for l in lines if re.match('\s+\d', l)]
     d = {}
index 39466a56feea2835d89070b38e3979e4a1fa716e..8d99a32583041ec717a5df432bf512713ca5b9a0 100644 (file)
@@ -721,23 +721,32 @@ class MultiStepBuilder(BuilderBase):
 
         src_suffixes = self.src_suffixes(env)
 
+        def match_src_suffix(node, src_suffixes=src_suffixes):
+            # This reaches directly into the Node.name attribute (instead
+            # of using an accessor function) for performance reasons.
+            return filter(lambda s, n=node.name:
+                                 n[-len(s):] == s,
+                          src_suffixes)
+
         for snode in slist:
-            for srcsuf in src_suffixes:
-                if str(snode)[-len(srcsuf):] == srcsuf and sdict.has_key(srcsuf):
-                    tgt = sdict[srcsuf]._execute(env, None, [snode], overwarn)
-                    # If the subsidiary Builder returned more than one target,
-                    # then filter out any sources that this Builder isn't
-                    # capable of building.
-                    if len(tgt) > 1:
-                        tgt = filter(lambda x, self=self, suf=src_suffixes, e=env:
-                                     self.splitext(SCons.Util.to_String(x),e)[1] in suf,
-                                     tgt)
-                    final_sources.extend(tgt)
-                    snode = None
-                    break
-            if snode:
+            name = snode.name
+            match = match_src_suffix(snode)
+            if match:
+                try:
+                    bld = sdict[match[0]]
+                except KeyError:
+                    final_sources.append(snode)
+                else:
+                    tlist = bld._execute(env, None, [snode], overwarn)
+                    # If the subsidiary Builder returned more than one
+                    # target, then filter out any sources that this
+                    # Builder isn't capable of building.
+                    if len(tlist) > 1:
+                        tlist = filter(match_src_suffix, tlist)
+                    final_sources.extend(tlist)
+            else:
                 final_sources.append(snode)
-                
+
         return BuilderBase._execute(self, env, target, final_sources, overwarn)
 
     def get_src_builders(self, env):
index 0d2d85dd7cc5c6356eaebc976154da8991823b26..bf7ed438d9054afc62e1ed2674876b8a3b54ee9d 100644 (file)
@@ -104,3 +104,51 @@ else:
         def memory():
             res = resource.getrusage(resource.RUSAGE_SELF)
             return res[4]
+
+
+
+caller_dicts = {}
+
+def caller(back=0):
+    import traceback
+    tb = traceback.extract_stack(limit=3+back)
+    key = tb[1][:3]
+    try:
+        entry = caller_dicts[key]
+    except KeyError:
+        entry = caller_dicts[key] = {}
+    key = tb[0][:3]
+    try:
+        entry[key] = entry[key] + 1
+    except KeyError:
+        entry[key] = 1
+
+def dump_caller_counts(file=sys.stdout):
+    keys = caller_dicts.keys()
+    keys.sort()
+    for k in keys:
+        file.write("Callers of %s:%d(%s):\n" % func_shorten(k))
+        counts = caller_dicts[k]
+        callers = counts.keys()
+        callers.sort()
+        for c in callers:
+            #file.write("    counts[%s] = %s\n" % (c, counts[c]))
+            t = ((counts[c],) + func_shorten(c))
+            file.write("    %6d %s:%d(%s)\n" % t)
+
+shorten_list = [
+    ( '/scons/SCons/',          1),
+    ( '/src/engine/SCons/',     1),
+    ( '/usr/lib/python',        0),
+]
+
+def func_shorten(func_tuple):
+    f = func_tuple[0]
+    for t in shorten_list:
+        i = string.find(f, t[0])
+        if i >= 0:
+            if t[1]:
+                i = i + len(t[0])
+            f = f[i:]
+            break
+    return (f,)+func_tuple[1:]
index f50273ecd7b630e4ce0c578a07140db9756e2e37..f66f214fc0d452695870043549b97d98fd03328b 100644 (file)
@@ -1127,6 +1127,11 @@ def _main(args, parser):
         print "Memoizer (memory cache) hits and misses:"
         SCons.Memoize.Dump()
 
+    # Dump any caller counts.  This is purely for internal debugging
+    # during development, so there's no need to control it with a
+    # --debug= option.
+    SCons.Debug.dump_caller_counts()
+
 def _exec_main():
     all_args = sys.argv[1:]
     try: