D7665: dirstate: when calling rebuild(), avoid some N^2 codepaths

spectral (Kyle Lippincott) phabricator at mercurial-scm.org
Fri Dec 13 23:58:04 UTC 2019


spectral created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  I had a user repo with 200k files in it. Calling `hg debugrebuilddirstate` took
  tens of minutes (I didn't wait for it). In that situation,
  changedfiles==allfiles, and both are lists. This meant that we had to run an
  average of 100k comparisons, for each of 200k files, just to check whether a
  file needed to have normallookup called (it always did), or drop.
  
  While it's probably not a huge issue, in my very awkward synthetic benchmark I
  wrote (not using a benchmark library or anything), I was seeing some slowdowns
  for small-changedfiles and very-large-allfiles invocations, with an inflection
  somewhere around 10 items in changedfiles (regardless of the size of allfiles);
  above 10 items in changedfiles, the new code appears to always be faster. For
  the case of 50k files in changedfiles and the same items in allfiles, I'm seeing
  differences of 15s of just running comparisons vs. 0.003793s. I haven't bothered
  to run a comparison of 200k items in changedfiles and allfiles. :)

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D7665

AFFECTED FILES
  mercurial/dirstate.py

CHANGE DETAILS

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -603,19 +603,34 @@
     def rebuild(self, parent, allfiles, changedfiles=None):
         if changedfiles is None:
             # Rebuild entire dirstate
-            changedfiles = allfiles
+            to_lookup = allfiles
+            to_drop = []
             lastnormaltime = self._lastnormaltime
             self.clear()
             self._lastnormaltime = lastnormaltime
+        elif len(changedfiles) < 10:
+            # Avoid turning allfiles into a set, which can be expensive if it's
+            # large.
+            to_lookup = []
+            to_drop = []
+            for f in changedfiles:
+                if f in allfiles:
+                    to_lookup.append(f)
+                else:
+                    to_drop.append(f)
+        else:
+            changedfilesset = set(changedfiles)
+            to_lookup = changedfilesset.intersection(set(allfiles))
+            to_drop = changedfilesset - to_lookup
 
         if self._origpl is None:
             self._origpl = self._pl
         self._map.setparents(parent, nullid)
-        for f in changedfiles:
-            if f in allfiles:
-                self.normallookup(f)
-            else:
-                self.drop(f)
+
+        for f in to_lookup:
+            self.normallookup(f)
+        for f in to_drop:
+            self.drop(f)
 
         self._dirty = True
 



To: spectral, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list