changeset 4373:abeb3edb2b4e

dirstate: make parents() faster. This allows localrepository.status() to avoid reading all but the first 40 bytes of the dirstate in the common case of running "hg status".
author Bryan O'Sullivan <bos@serpentine.com>
date Tue, 24 Apr 2007 11:46:15 -0700
parents 4ddc6d374265
children 9edc2d6f7c10
files mercurial/dirstate.py
diffstat 1 files changed, 24 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dirstate.py	Tue Apr 24 11:05:39 2007 -0700
+++ b/mercurial/dirstate.py	Tue Apr 24 11:46:15 2007 -0700
@@ -20,6 +20,7 @@
         self.dirty = 0
         self.ui = ui
         self.map = None
+        self.fp = None
         self.pl = None
         self.dirs = None
         self.copymap = {}
@@ -133,12 +134,28 @@
             self.lazyread()
             return self[key]
 
+    _unknown = ('?', 0, 0, 0)
+
+    def get(self, key):
+        try:
+            return self[key]
+        except KeyError:
+            return self._unknown
+
     def __contains__(self, key):
         self.lazyread()
         return key in self.map
 
     def parents(self):
-        self.lazyread()
+        if self.pl is None:
+            self.pl = [nullid, nullid]
+            try:
+                self.fp = self.opener('dirstate')
+                st = self.fp.read(40)
+                if len(st) == 40:
+                    self.pl = st[:20], st[20:40]
+            except IOError, err:
+                if err.errno != errno.ENOENT: raise
         return self.pl
 
     def branch(self):
@@ -202,7 +219,12 @@
         self.map = {}
         self.pl = [nullid, nullid]
         try:
-            st = self.opener("dirstate").read()
+            if self.fp:
+                self.fp.seek(0)
+                st = self.fp.read()
+                self.fp = None
+            else:
+                st = self.opener("dirstate").read()
             if st:
                 self.parse(st)
         except IOError, err: