[PATCH] strip: make query to get new bookmark target cheaper

Siddharth Agarwal sid0 at fb.com
Wed Dec 5 17:08:35 CST 2012


# HG changeset patch
# User Siddharth Agarwal <sid0 at fb.com>
# Date 1354746795 28800
# Node ID f0da340980832986e8c69551b294f63bb29099e0
# Parent  7baee10423b3c0c1c0a61c6b1ccb3dee4ae83d34
strip: make query to get new bookmark target cheaper

The current query to get the new bookmark target for stripped revisions
involves multiple walks up the DAG, and is really expensive, taking over 2.5
seconds on a repository with over 400,000 changesets even if just one
changeset is being stripped.

A slightly simplified version of the current query is

max(heads(::<tostrip> - <tostrip>))

We make two observations here.

1. For any set s, max(heads(s)) == max(s). That is because revision numbers
   define a topological order, so that the element with the highest revision
   number in s will not have any children in s.

2. For any set s, max(::s - s) == max(parents(s) - s). In other words, the
   ancestor of s with the highest revision number not in s is a parent of one
   of the revs in s. Why? Because if it were an ancestor but not a parent of s,
   it would have a descendant that would be a parent of s. This descendant
   would have a higher revision number, leading to a contradiction.

Combining these two observations, we rewrite the revset query as

max(parents(<tostrip>) - <tostrip>)

The time complexity is now linear in the number of changesets being stripped.
For the above repository, the query now takes 0.1 seconds when one changeset
is stripped. This speeds up operations that use repair.strip, like the rebase
and strip commands.

diff -r 7baee10423b3 -r f0da34098083 mercurial/repair.py
--- a/mercurial/repair.py	Tue Dec 04 21:28:51 2012 -0800
+++ b/mercurial/repair.py	Wed Dec 05 14:33:15 2012 -0800
@@ -112,8 +112,10 @@
         saverevs.difference_update(descendants)
     savebases = [cl.node(r) for r in saverevs]
     stripbases = [cl.node(r) for r in tostrip]
-    newbmtarget = repo.revs('sort(heads((::%ld) - (%ld)), -rev)',
-                            tostrip, tostrip)
+
+    # For a set s, max(parents(s) - s) is the same as max(heads(::s - s)), but
+    # is much faster
+    newbmtarget = repo.revs('max(parents(%ld) - (%ld))', tostrip, tostrip)
     if newbmtarget:
         newbmtarget = repo[newbmtarget[0]].node()
     else:


More information about the Mercurial-devel mailing list