Author: tack
Date: Sun Apr 23 02:02:49 2006
New Revision: 1480

Modified:
   trunk/beacon/src/directory.py

Log:
Refactor _beacon_listdir; improves performance about 40-50% (i.e.
makes it nearly twice as fast).


Modified: trunk/beacon/src/directory.py
==============================================================================
--- trunk/beacon/src/directory.py       (original)
+++ trunk/beacon/src/directory.py       Sun Apr 23 02:02:49 2006
@@ -128,43 +128,51 @@
             return self._beacon_listdir_cache[1]
 
         try:
+            # Try to list the overlay directory
+            overlay_results = os.listdir(self._beacon_ovdir)
+        except OSError:
+            # No overlay
+            overlay_results = []
+
+        try:
             # Try to list the directory. If that fails for some reason,
             # return an empty list
-            listdir = os.listdir(self.filename)
-            result = [ [ x, self.filename + x, False ] for x in listdir
-                       if not x.startswith('.') ]
+            fs_results = os.listdir(self.filename)
         except OSError, e:
             log.warning(e)
             self._beacon_listdir_cache = time.time(), []
             return []
 
-        try:
-            # Try to list the overlay directory
-            result += [ [ x, self._beacon_ovdir + x, True ] \
-                          for x in os.listdir(self._beacon_ovdir) \
-                          if not x.startswith('.') and not x in listdir ]
-        except OSError:
-            # No overlay
-            pass
-        
-        for r in result[:]:
-            try:
-                # append stat information to every result
-                r.append(os.stat(r[1]))
-                if r[2] and stat.S_ISDIR(r[3][stat.ST_MODE]):
-                    # overlay dir, remove
-                    log.warning('skip overlay dir %s' % r[1])
-                    result.remove(r)
-            except (OSError, IOError), e:
-                # unable to stat file, remove it from list
-                log.error(e)
-                result.remove(r)
-        # sort results
-        result.sort(lambda x,y: cmp(x[0], y[0]))
+        results_file_map = {}
+        for is_overlay, prefix, results in ((False, self.filename, 
fs_results), 
+                                            (True, self._beacon_ovdir, 
overlay_results)):
+            for r in results:
+                if (is_overlay and r in results_file_map) or r[0] == ".":
+                    continue
+                fullpath = prefix + r
+                try:
+                    # append stat information to every result
+                    statinfo = os.stat(fullpath)
+                    if is_overlay and stat.S_ISDIR(statinfo[stat.ST_MODE]):
+                        # overlay dir, remove
+                        log.warning('skip overlay dir %s' % r[1])
+                        continue
+                except (OSError, IOError), e:
+                    # unable to stat file, remove it from list
+                    log.error(e)
+                    continue
+
+                results_file_map[r] = (r, fullpath, is_overlay, statinfo)
+
+        # We want to avoid lambda on large data sets, so we sort the keys,
+        # which is just a list of files.  This is the common case that sort()
+        # is optimized for.
+        keys = results_file_map.keys()
+        keys.sort()
+        result = [ results_file_map[x] for x in keys ]
         # store in cache
         self._beacon_listdir_cache = time.time(), result
-        # return results
-        return result[:]
+        return result
 
 
     def __repr__(self):


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to