]> arthur.barton.de Git - bup.git/commitdiff
vfs: cache file sizes in the Node object.
authorAvery Pennarun <apenwarr@gmail.com>
Fri, 23 Apr 2010 20:21:35 +0000 (16:21 -0400)
committerAvery Pennarun <apenwarr@gmail.com>
Fri, 23 Apr 2010 20:21:35 +0000 (16:21 -0400)
Since the filesystem is read only, there's no reason to recalculate the file
size every time someone asks :)

lib/bup/vfs.py

index 7964c9825f2bde56de7a64a10a2ea69cdd707eb8..91566da1dcc29a5fad934a055259694094b13c8e 100644 (file)
@@ -142,6 +142,10 @@ class Node:
     
     
 class File(Node):
+    def __init__(self, parent, name, mode, hash):
+        Node.__init__(self, parent, name, mode, hash)
+        self._cached_size = None
+        
     def _content(self):
         return cp().join(self.hash.encode('hex'))
 
@@ -151,7 +155,9 @@ class File(Node):
     def size(self):
         # FIXME inefficient.  If a file is chunked, we could just check
         # the offset + size of the very last chunk.
-        return sum(len(blob) for blob in self._content())
+        if self._cached_size == None:
+            self._cached_size = sum(len(blob) for blob in self._content())
+        return self._cached_size
     
     def readbytes(self, ofs, count):
         # FIXME inefficient.  If a file is chunked, we could choose to