revlog: introduce a cache for partial lookups
authorMatt Mackall <mpm@selenic.com>
Tue, 11 Jan 2011 17:12:32 -0600
changeset 13258 c2661863f16f
parent 13257 d1245ce817a8
child 13259 3b616dfa4b17
revlog: introduce a cache for partial lookups Partial lookups are always O(n), and often we look up the same one multiple times.
mercurial/revlog.py
--- a/mercurial/revlog.py	Tue Jan 11 17:09:06 2011 -0600
+++ b/mercurial/revlog.py	Tue Jan 11 17:12:32 2011 -0600
@@ -221,6 +221,7 @@
         self.index = []
         self._shallowroot = shallowroot
         self._parentdelta = 0
+        self._pcache = {}
 
         v = REVLOG_DEFAULT_VERSION
         if hasattr(opener, 'options') and 'defversion' in opener.options:
@@ -703,6 +704,9 @@
                 pass
 
     def _partialmatch(self, id):
+        if id in self._pcache:
+            return self._pcache[id]
+
         if len(id) < 40:
             try:
                 # hex(node)[:...]
@@ -712,6 +716,7 @@
                 nl = [n for n in nl if hex(n).startswith(id)]
                 if len(nl) > 0:
                     if len(nl) == 1:
+                        self._pcache[id] = nl[0]
                         return nl[0]
                     raise LookupError(id, self.indexfile,
                                       _('ambiguous identifier'))