[PATCH 2 of 5 V2] dirstate: avoid unnecessary load+dump during backup

Jun Wu quark at fb.com
Fri Mar 3 01:13:28 EST 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1488421266 28800
#      Wed Mar 01 18:21:06 2017 -0800
# Node ID 406f94842e3372f241a151a3ee6ea5f39c04758d
# Parent  44cbc7533adfb66e687e4b28312b166f32b01ce0
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 406f94842e33
dirstate: avoid unnecessary load+dump during backup

Previously, dirstate.savebackup unconditionally dumps the dirstate map to
disk. It may require loading dirstate first to be able to dump it. Those
operations could be expensive if the dirstate is big, and could be avoided
if we know the dirstate file is up-to-date.

This patch avoids the read and write if the dirstate is clean. In that case,
we just do a plain copy without any serialization.

This should make commands which use transactions but do not touch dirstate
faster. For example, "hg bookmark -r REV NAME".

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -1210,6 +1210,7 @@ class dirstate(object):
         # because the latter omits writing out if transaction is running.
         # output file will be used to create backup of dirstate at this point.
-        self._writedirstate(self._opener(filename, "w", atomictemp=True,
-                                         checkambig=True))
+        if self._dirty or not self._opener.exists(filename):
+            self._writedirstate(self._opener(filename, "w", atomictemp=True,
+                                             checkambig=True))
 
         if tr:


More information about the Mercurial-devel mailing list