Patchwork [STABLE,V2] annotate: pre-calculate the "needed" dictionary (issue5360)

login
register
mail settings
Submitter Jun Wu
Date Sept. 4, 2016, 9:42 p.m.
Message ID <00579a5483badf475762.1473025343@x1c>
Download mbox | patch
Permalink /patch/16540/
State Accepted
Headers show

Comments

Jun Wu - Sept. 4, 2016, 9:42 p.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1472826059 -3600
#      Fri Sep 02 15:20:59 2016 +0100
# Node ID 00579a5483badf47576253fad3a472090dc3ed24
# Parent  d130a38ef41f3c9e2d2f26df3535d89a93f87301
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 00579a5483ba
annotate: pre-calculate the "needed" dictionary (issue5360)

The "needed" dict is used as a reference counter to free items in the giant
"hist" dict. However, currently it is not very accurate and can lead to
dropping "hist" items unnecessarily, for example, with the following DAG,

       -3-
      /   \
  0--1--2--4--

The current algorithm will visit and calculate rev 1 twice, undesired. And
it tries to be smart by clearing rev 1's parents: "pcache[1] = []" at the
time hist[1] being accessed (note: hist[1] needs to be used twice, by rev 2
and rev 3). It can result in incorrect results if p1 of rev 4 deletes chunks
belonging to rev 0.

However, simply removing "needed" is not okay, because it will consume 10x
memory:

  # without any change
  % HGRCPATH= lrun ./hg annotate mercurial/commands.py -r d130a38 3>&2 [1]
  MEMORY   49074176
  CPUTIME  9.213
  REALTIME 9.270

  # with "needed" removed
  MEMORY   637673472
  CPUTIME  8.164
  REALTIME 8.249

This patch moves "needed" (and "pcache") calculation to a separate DFS to
address the issue. It improves perf and fixes issue5360 by correctly reusing
hist, while maintaining low memory usage. Some additional attempt has been
made to further reduce memory usage, like changing "pcache[f] = []" to "del
pcache[f]". Therefore the result can be both faster and lower memory usage:

  # with this patch applied
  MEMORY   47575040
  CPUTIME  7.870
  REALTIME 7.926

[1]: lrun is a lightweight sandbox built on Linux cgroup and namespace. It's
     used to measure CPU and memory usage here. Source code is available at
     github.com/quark-zju/lrun.
Jun Wu - Sept. 5, 2016, 9:03 a.m.
Excerpts from Jun Wu's message of 2016-09-04 22:42:23 +0100:
> +Issue5360: Deleted chunk in p1 of a merge changeset
> +
> +  $ cd ..

This "cd .." should be removed. I will wait for other comments (hopefully
none) before V3.

> +  $ hg init repo-5360
> +  $ cd repo-5360
Yuya Nishihara - Sept. 5, 2016, 1:59 p.m.
On Sun, 4 Sep 2016 22:42:23 +0100, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1472826059 -3600
> #      Fri Sep 02 15:20:59 2016 +0100
> # Node ID 00579a5483badf47576253fad3a472090dc3ed24
> # Parent  d130a38ef41f3c9e2d2f26df3535d89a93f87301
> # Available At https://bitbucket.org/quark-zju/hg-draft
> #              hg pull https://bitbucket.org/quark-zju/hg-draft -r 00579a5483ba
> annotate: pre-calculate the "needed" dictionary (issue5360)

Nice. I couldn't find any errors, so queued for stable, thanks.

> +        # 2nd DFS does the actual annotate
> +        visit[:] = [base]
> +        hist = {}
> +        while visit:
>              f = visit[-1]
> -            pcached = f in pcache
> -            if not pcached:
> -                pcache[f] = parents(f)
> +            if f in hist:
> +                visit.pop()
> +                continue
>  
>              ready = True
> @@ -1007,16 +1021,9 @@ class basefilectx(object):
>                      ready = False
>                      visit.append(p)
> -                if not pcached:
> -                    needed[p] = needed.get(p, 0) + 1
>              if ready:
>                  visit.pop()
> -                reusable = f in hist
> -                if reusable:
> -                    curr = hist[f]
> -                else:
> -                    curr = decorate(f.data(), f)
> +                curr = decorate(f.data(), f)
>                  for p in pl:
> -                    if not reusable:
> -                        curr = pair(hist[p], curr)
> +                    curr = pair(hist[p], curr)
>                      if needed[p] == 1:
>                          del hist[p]

I believe the "reusable" case mentioned in a54ddfae8907 is handled well in
new code. Foozy, can you revalidate that?

https://selenic.com/repo/hg/rev/a54ddfae8907

> +Issue5360: Deleted chunk in p1 of a merge changeset
> +
> +  $ cd ..

Moved "cd .." to the bottom of new test.

> +  $ hg init repo-5360
> +  $ cd repo-5360
> +  $ echo 1 > a
> +  $ hg commit -A a -m 1
> +  $ echo 2 >> a
> +  $ hg commit -m 2
> +  $ echo a > a
> +  $ hg commit -m a
> +  $ hg update '.^' -q
> +  $ echo 3 >> a
> +  $ hg commit -m 3 -q
> +  $ hg merge 2 -q
> +  $ cat > a << EOF
> +  > b
> +  > 1
> +  > 2
> +  > 3
> +  > a
> +  > EOF
> +  $ hg resolve --mark -q
> +  $ hg commit -m m
> +  $ hg annotate a
> +  4: b
> +  0: 1
> +  1: 2
> +  3: 3
> +  2: a

Patch

diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -991,13 +991,27 @@  class basefilectx(object):
         # depth-first search.
 
+        # 1st DFS pre-calculates pcache and needed
         visit = [base]
-        hist = {}
         pcache = {}
         needed = {base: 1}
         while visit:
+            f = visit.pop()
+            if f in pcache:
+                continue
+            pl = parents(f)
+            pcache[f] = pl
+            for p in pl:
+                needed[p] = needed.get(p, 0) + 1
+                if p not in pcache:
+                    visit.append(p)
+
+        # 2nd DFS does the actual annotate
+        visit[:] = [base]
+        hist = {}
+        while visit:
             f = visit[-1]
-            pcached = f in pcache
-            if not pcached:
-                pcache[f] = parents(f)
+            if f in hist:
+                visit.pop()
+                continue
 
             ready = True
@@ -1007,16 +1021,9 @@  class basefilectx(object):
                     ready = False
                     visit.append(p)
-                if not pcached:
-                    needed[p] = needed.get(p, 0) + 1
             if ready:
                 visit.pop()
-                reusable = f in hist
-                if reusable:
-                    curr = hist[f]
-                else:
-                    curr = decorate(f.data(), f)
+                curr = decorate(f.data(), f)
                 for p in pl:
-                    if not reusable:
-                        curr = pair(hist[p], curr)
+                    curr = pair(hist[p], curr)
                     if needed[p] == 1:
                         del hist[p]
@@ -1026,5 +1033,5 @@  class basefilectx(object):
 
                 hist[f] = curr
-                pcache[f] = []
+                del pcache[f]
 
         return zip(hist[base][0], hist[base][1].splitlines(True))
diff --git a/tests/test-annotate.t b/tests/test-annotate.t
--- a/tests/test-annotate.t
+++ b/tests/test-annotate.t
@@ -607,2 +607,33 @@  Even when the starting revision is the l
 
   $ cd ..
+
+Issue5360: Deleted chunk in p1 of a merge changeset
+
+  $ cd ..
+  $ hg init repo-5360
+  $ cd repo-5360
+  $ echo 1 > a
+  $ hg commit -A a -m 1
+  $ echo 2 >> a
+  $ hg commit -m 2
+  $ echo a > a
+  $ hg commit -m a
+  $ hg update '.^' -q
+  $ echo 3 >> a
+  $ hg commit -m 3 -q
+  $ hg merge 2 -q
+  $ cat > a << EOF
+  > b
+  > 1
+  > 2
+  > 3
+  > a
+  > EOF
+  $ hg resolve --mark -q
+  $ hg commit -m m
+  $ hg annotate a
+  4: b
+  0: 1
+  1: 2
+  3: 3
+  2: a