merge: add mergeresult.mapaction to improve speed
authorArseniy Alekseyev <aalekseyev@janestreet.com>
Thu, 12 Jan 2023 13:14:00 +0000
changeset 49911 c7a04bfabd4d
parent 49910 7b474609f199
child 49912 bc83ebe07bf0
merge: add mergeresult.mapaction to improve speed As a part of [hg update] we convert all [ACTION_CREATED] merge results into [ACTION_GET] actions, and that's slightly inefficient because every insertion pays the full cost of maintaining the [mergeresult] data structure up to date. This commit adds a function [mapaction], which is faster. (saves around 0.3s on a large update involving ~400k files)
mercurial/merge.py
--- a/mercurial/merge.py	Wed Jan 04 19:30:47 2023 +0000
+++ b/mercurial/merge.py	Thu Jan 12 13:14:00 2023 +0000
@@ -246,9 +246,7 @@
         else:
             repo.ui.warn(_(b"%s: replacing untracked files in directory\n") % f)
 
-    for f, args, msg in list(
-        mresult.getactions([mergestatemod.ACTION_CREATED])
-    ):
+    def transformargs(f, args):
         backup = (
             f in fileconflicts
             or pathconflicts
@@ -258,7 +256,11 @@
             )
         )
         (flags,) = args
-        mresult.addfile(f, mergestatemod.ACTION_GET, (flags, backup), msg)
+        return (flags, backup)
+
+    mresult.mapaction(
+        mergestatemod.ACTION_CREATED, mergestatemod.ACTION_GET, transformargs
+    )
 
 
 def _forgetremoved(wctx, mctx, branchmerge, mresult):
@@ -590,6 +592,18 @@
         self._filemapping[filename] = (action, data, message)
         self._actionmapping[action][filename] = (data, message)
 
+    def mapaction(self, actionfrom, actionto, transform):
+        """changes all occurrences of action `actionfrom` into `actionto`,
+        transforming its args with the function `transform`.
+        """
+        orig = self._actionmapping[actionfrom]
+        del self._actionmapping[actionfrom]
+        dest = self._actionmapping[actionto]
+        for f, (data, msg) in orig.items():
+            data = transform(f, data)
+            self._filemapping[f] = (actionto, data, msg)
+            dest[f] = (data, msg)
+
     def getfile(self, filename, default_return=None):
         """returns (action, args, msg) about this file