Skip to contentSkip to conversation

Multiparent diff tree-walker + combine-diff speedup

PATCH Superseded Kirill Smelkov 3 participants 10 messages Jun 15, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Multiparent diff tree-walker + combine-diff speedup

Here go combine-diff speedup patches in form of first reworking diff tree-walker to work in general case - when a commit have several parents, not only one - we are traversing all 1+nparent trees in parallel.

Then we are taking advantage of the new diff tree-walker for speeding up combine-diff, which for linux.git results in ~14 times speedup.

I understand v1.9.0 is going to be released first, but wanted to finally send the patches, so that people could start reviewing them.

Please apply on top of ks/tree-diff-more and thanks beforehand,

Kirill

Kirill Smelkov (2):
tree-diff: rework diff_tree() to generate diffs for multiparent cases as well
combine-diff: speed it up, by using multiparent diff tree-walker directly

 combine-diff.c |  85 +++++++++-
 diff.c         |   2 +
 diff.h         |  10 ++
 tree-diff.c    | 501 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 4 files changed, 529 insertions(+), 69 deletions(-)
Signature · 1 line
1.9.rc1.181.g641f458
#1 lore raw

PATCH 1/2 tree-diff: rework diff_tree() to generate diffs for multiparent cases as well

Previously diff_tree(), which is now named __diff_tree_sha1(), was generating diff_filepair(s) for two trees t1 and t2, and that was usually used for a commit as t1=HEAD~, and t2=HEAD - i.e. to see changes a commit introduces.

In Git, however, we have fundamentally built flexibility in that a commit can have many parents - 1 for a plain commit, 2 for a simple merge, but also more than 2 for merging several heads at once.

For merges there is a so called combine-diff, which shows diff, a merge introduces by itself, omitting changes done by any parent. That works through first finding paths, that are different to all parents, and then showing generalized diff, with separate columns for +/- for each parent. The code lives in combine-diff.c .

There is an impedance mismatch, however, in that a commit could generally have any number of parents, and that while diffing trees, we divide cases for 2-tree diffs and more-than-2-tree diffs. I mean there is no special casing for multiple parents commits in e.g. revision-walker .

That impedance mismatch *hurts* *performance* *badly* for generating combined diffs - in c839f1bd (combine-diff: optimize combine_diff_path sets intersection) I've already removed some slowness from it, but from the timings provided there, it could be seen, that combined diffs still cost more than an order of magnitude more cpu time, compared to diff for usual commits, and that would only be an optimistic estimate, if we take into account that for e.g. linux.git there is only one merge for several dozens of plain commits.

That slowness comes from the fact that currently, while generating combined diff, a lot of time is spent computing diff(commit,commit^2) just to only then intersect that huge diff to almost small set of files from diff(commit,commit^1).

That's because at present, to compute combine-diff, for first finding paths, that "every parent touches", we use the following combine-diff property/definition:

D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (w.r.t. paths)

where

D(A,P1...Pn) is combined diff between commit A, and parents Pi

and

D(A,Pi) is usual two-tree diff Pi..A

So if any of that D(A,Pi) is huge, tracting 1 n-parent combine-diff as n 1-parent diffs and intersecting results will be slow.

And usually, for linux.git and other topic-based workflows, that D(A,P2) is huge, because, if merge-base of A and P2, is several dozens of merges (from A, via first parent) below, that D(A,P2) will be diffing sum of merges from several subsystems to 1 subsystem.

The solution is to avoid computing n 1-parent diffs, and to find changed-to-all-parents paths via scanning A's and all Pi's trees simultaneously, at each step comparing their entries, and based on that comparison, populate paths result, and deduce we could *skip* *recursing* into subdirectories, if at least for 1 parent, sha1 of that dir tree is the same as in A. That would save us from doing significant amount of needless work.

Such approach is very similar to what diff_tree() does, only there we deal with scanning only 2 trees simultaneously, and for n+1 tree, the logic is a bit more complex:

D(A,X1...Xn) calculation scheme
-------------------------------

D(A,X1...Xn) = D(A,X1) ^ ... ^ D(A,Xn) (regarding resulting paths set)

D(A,Xj) - diff between A..Xj
D(A,X1...Xn) - combined diff from A to parents X1,...,Xn

We start from all trees, which are sorted, and compare their entries in lock-step:

      A     X1       Xn
      -     -        -
     |a|   |x1|     |xn|
     |-|   |--| ... |--|      i = argmin(x1...xn)
     | |   |  |     |  |
     |-|   |--|     |--|
     |.|   |. |     |. |
      .     .        .
      .     .        .

at any time there could be 3 cases:

     1)  a < xi;
     2)  a > xi;
     3)  a = xi.

Schematic deduction of what every case means, and what to do, follows:

1) a < xi -> ∀j a ∉ Xj -> "+a" ∈ D(A,Xj) -> D += "+a"; a↓

2) a > xi

    2.1) ∃j: xj > xi  ->  "-xi" ∉ D(A,Xj)  ->  D += ø;  ∀ xk=xi  xk↓
    2.2) ∀j  xj = xi  ->  xj ∉ A  ->  "-xj" ∈ D(A,Xj)  ->  D += "-xi";  ∀j xj↓

3) a = xi

    3.1) ∃j: xj > xi  ->  "+a" ∈ D(A,Xj)  ->  only xk=xi remains to investigate
    3.2) xj = xi  ->  investigate δ(a,xj)
     |
     |
     v

    3.1+3.2) looking at δ(a,xk) ∀k: xk=xi - if all != ø  ->

                      ⎧δ(a,xk)  - if xk=xi
             ->  D += ⎨
                      ⎩"+a"     - if xk>xi

    in any case a↓  ∀ xk=xi  xk↓

~

For comparison, here is how diff_tree() works:

D(A,B) calculation scheme
-------------------------

A B
- -

   |a|   |b|    a < b   ->  a ∉ B   ->   D(A,B) +=  +a    a↓
   |-|   |-|    a > b   ->  b ∉ A   ->   D(A,B) +=  -b    b↓
   | |   | |    a = b   ->  investigate δ(a,b)            a↓ b↓
   |-|   |-|
   |.|   |.|
    .     .
    .     .

~~~~~~~~

This patch generalizes diff tree-walker to work with arbitrary number of parents as described above - i.e. now there is a resulting tree t, and some parents trees tp[i] i=[0..nparent). The generalization builds on the fact that usual diff

D(A,B)

is by definition the same as combined diff

D(A,[B]),

so if we could rework the code for common case and make it be not slower for nparent=1 case, usual diff(t1,t2) generation will not be slower, and multiparent diff tree-walker would greatly benefit generating combine-diff.

What we do is as follows:

1) diff tree-walker __diff_tree_sha1() is internally reworked to be a paths generator (new name diff_tree_paths()), with each generated path being struct combine_diff_path with info for path, new sha1,mode and for every parent which sha1,mode it was in it.

2) From that info, we can still generate usual diff queue with struct diff_filepairs, via "exporting" generated combine_diff_path, if we know we run for nparent=1 case. (see emit_diff() which is now named emit_diff_p0only())

3) In order for diff_can_quit_early(), which checks

DIFF_OPT_TST(opt, HAS_CHANGES))

to work, that exporting have to be happening not in bulk, but incrementally, one diff path at a time.

For such consumers, there is a new callback in diff_options introduced:

       ->pathchange(opt, struct combine_diff_path *)

which, if set to !NULL, is called for every generated path.

(see new compat __diff_tree_sha1() wrapper around new paths
generator for setup)

4) The paths generation itself, is reworked from previous __diff_tree_sha1() code according to "D(A,X1...Xn) calculation scheme" provided above:

On the start we allocate [nparent] arrays in place what was earlier just for one parent tree.

then we just generalize loops, and comparison according to the algorithm.

Some notes(*):

1) For loops,

i = 0; do { ... } while (++i < nparent);

is used instead of

for (i = 0; i < nparent; ++i)

     ...

because for the former case, the compiler have to emit additional prologue code which checks for i >= nparent case before entering the loop.

As we require nparent must be >0, that additional overhead conflicts with the "runs not slower for nparent=1 case than before" goal.

2) alloca(), for small arrays, is used for the same reason - if we change it to xmalloc()/free() the timings get worse

3) For every parent tree, we need to keep a tag, whether entry from that parent equals to entry from minimal parent. For performance reasons I'm keeping that tag in entry's mode field in unused bit - see S_IFXMIN_NEQ. Not doing so, we'd need to alloca another [nparent] array, which hurts performance.

4) For emitted paths, memory could be reused, if we know the path was processed via callback and will not be needed later. We efficient hand-made realloc-style __path_appendnew(), that saves us from ~1-1.5% of potential additional slowdown.

5) goto(s) are used in several places, as the code executes a little bit faster with lowered register pressure.

Also

- we should now check for FIND_COPIES_HARDER not only when two entries names are the same, and their hashes are equal, but also for a case, when a path was removed from some of all parents having it.

The reason is, if we don't, that path won't be emitted at all (see "a > xi" case), and we'll just skip it, and FIND_COPIES_HARDER wants all paths - with diff or without - to be emitted, to be later analyzed for being copies sources.

The new check is only necessary for nparent >1, as for nparent=1 case xmin_eqtotal always =1 =nparent, and a path is always added to diff as removal.

~~~~~~~~

Timings for

    # without -c, i.e. testing only nparent=1 case
    `git log --raw --no-abbrev --no-renames`

before and after the patch are as follows:

                navy.git        linux.git v3.10..v3.11

    before      0.611s          1.889s
    after       0.619s          1.907s
    slowdown    1.3%            0.9%

This timings show we did no harm to usual diff(tree1,tree2) generation. From the table we can see that we actually did ~1% slowdown, but I think I've "earned" that 1% in the previous patch (729f7e8e in pu) so for nparent=1 case, net timings stays approximately the same.

The output also stayed the same.

(*) If we revert 1)-5) to more usual techniques, for nparent=1 case,
we'll get ~2-2.5% of additional slowdown, which I've tried to avoid, as
"do no harm for nparent=1 case" rule.

For linux.git, combined diff will run an order of magnitude faster and appropriate timings will be provided in the next commit, as we'll be taking advantage of the new diff tree-walker for combined-diff generation there.

P.S. and combined diff is not some exotic/for-play-only stuff - for example for a program I write to represent Git archives as readonly filesystem, there is initial scan with

    `git log --reverse --raw --no-abbrev --no-renames -c`

to extract log of what was created/changed when, as a result building a map

    {}  sha1    ->  in which commit (and date) a content was added

that -c means also show combined diff for merges, and without them, if a merge is non-trivial (merges changes from two parents with both having separate changes to a file), or an evil one, the map will not be full, i.e. some valid sha1 would be absent from it.

That case was my initial motivation for combined diffs speedup.

---
 diff.c      |   1 +
 diff.h      |  10 ++
 tree-diff.c | 501 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 448 insertions(+), 64 deletions(-)
diff.c +1 -0
index 8e4a6a9..cda4aa8 100644
@@ -3216,6 +3216,7 @@ void diff_setup(struct diff_options *options)
 	options->context = diff_context_default;
 	DIFF_OPT_SET(options, RENAME_EMPTY);
 
+	/* pathchange left =NULL by default */
 	options->change = diff_change;
 	options->add_remove = diff_addremove;
 	options->use_color = diff_use_color_default;
diff.h +10 -0
index bf834ff..b59e8d6 100644
@@ -15,6 +15,10 @@ struct diff_filespec;
 struct userdiff_driver;
 struct sha1_array;
 struct commit;
+struct combine_diff_path;
+
+typedef int (*pathchange_fn_t)(struct diff_options *options,
+		 struct combine_diff_path *path);
 
 typedef void (*change_fn_t)(struct diff_options *options,
 		 unsigned old_mode, unsigned new_mode,
@@ -157,6 +161,7 @@ struct diff_options {
 	int close_file;
 
 	struct pathspec pathspec;
+	pathchange_fn_t pathchange;
 	change_fn_t change;
 	add_remove_fn_t add_remove;
 	diff_format_fn_t format_callback;
@@ -189,6 +194,11 @@ const char *diff_line_prefix(struct diff_options *);
 
 extern const char mime_boundary_leader[];
 
+extern
+struct combine_diff_path *diff_tree_paths(
+	struct combine_diff_path *p, const unsigned char *sha1,
+	const unsigned char **parent_sha1, int nparent,
+	struct strbuf *base, struct diff_options *opt);
 extern int __diff_tree_sha1(const unsigned char *old, const unsigned char *new,
 			    struct strbuf *base, struct diff_options *opt);
 extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,
tree-diff.c +437 -64

598 lines not on this page · raw message

Signature · 1 line
1.9.rc1.181.g641f458
#2 lore raw

Re: [PATCH 1/2] tree-diff: rework diff_tree() to generate diffs for multiparent cases as well

tree-diff.c static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) · line 140

Quoted 4 lines · Kirill Smelkov
tree-diff.c +4 -0
+		/* until we go to it next round, .next holds how many bytes we
+		 * allocated (for faster realloc - we don't need copying old data).
+		 */
+		p->next = (struct combine_diff_path *)alloclen;

I am getting this here:

tree-diff.c: In function '__path_appendnew':
tree-diff.c:140:13: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]

#3 lore raw

Re: [PATCH 1/2] tree-diff: rework diff_tree() to generate diffs for multiparent cases as well

Quoted 11 lines · Junio C Hamano

Review

Quoted 4 lines · Kirill Smelkov
(quoted diff) +4 -0
+		/* until we go to it next round, .next holds how many bytes we
+		 * allocated (for faster realloc - we don't need copying old data).
+		 */
+		p->next = (struct combine_diff_path *)alloclen;

I am getting this here:

tree-diff.c: In function '__path_appendnew':
tree-diff.c:140:13: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]

Ah, sorry, I only tested on 32 bits, and this indeed could be a valid warning on systems where sizeof(ptr) != sizeof(int).

In our case, alloclen is small enough that the code should be valid, and we can avoid the warning, via proper usage of intptr_t.

Please find corrected patch below.

Thanks,
Kirill

---- 8< ----
From: Kirill Smelkov <[email protected]>
Subject: [PATCH v2 1/2] tree-diff: rework diff_tree() to generate diffs for
multiparent cases as well
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Previously diff_tree(), which is now named __diff_tree_sha1(), was generating diff_filepair(s) for two trees t1 and t2, and that was usually used for a commit as t1=HEAD~, and t2=HEAD - i.e. to see changes a commit introduces.

In Git, however, we have fundamentally built flexibility in that a commit can have many parents - 1 for a plain commit, 2 for a simple merge, but also more than 2 for merging several heads at once.

For merges there is a so called combine-diff, which shows diff, a merge introduces by itself, omitting changes done by any parent. That works through first finding paths, that are different to all parents, and then showing generalized diff, with separate columns for +/- for each parent. The code lives in combine-diff.c .

There is an impedance mismatch, however, in that a commit could generally have any number of parents, and that while diffing trees, we divide cases for 2-tree diffs and more-than-2-tree diffs. I mean there is no special casing for multiple parents commits in e.g. revision-walker .

That impedance mismatch *hurts* *performance* *badly* for generating combined diffs - in c839f1bd (combine-diff: optimize combine_diff_path sets intersection) I've already removed some slowness from it, but from the timings provided there, it could be seen, that combined diffs still cost more than an order of magnitude more cpu time, compared to diff for usual commits, and that would only be an optimistic estimate, if we take into account that for e.g. linux.git there is only one merge for several dozens of plain commits.

That slowness comes from the fact that currently, while generating combined diff, a lot of time is spent computing diff(commit,commit^2) just to only then intersect that huge diff to almost small set of files from diff(commit,commit^1).

That's because at present, to compute combine-diff, for first finding paths, that "every parent touches", we use the following combine-diff property/definition:

D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (w.r.t. paths)

where

D(A,P1...Pn) is combined diff between commit A, and parents Pi

and

D(A,Pi) is usual two-tree diff Pi..A

So if any of that D(A,Pi) is huge, tracting 1 n-parent combine-diff as n 1-parent diffs and intersecting results will be slow.

And usually, for linux.git and other topic-based workflows, that D(A,P2) is huge, because, if merge-base of A and P2, is several dozens of merges (from A, via first parent) below, that D(A,P2) will be diffing sum of merges from several subsystems to 1 subsystem.

The solution is to avoid computing n 1-parent diffs, and to find changed-to-all-parents paths via scanning A's and all Pi's trees simultaneously, at each step comparing their entries, and based on that comparison, populate paths result, and deduce we could *skip* *recursing* into subdirectories, if at least for 1 parent, sha1 of that dir tree is the same as in A. That would save us from doing significant amount of needless work.

Such approach is very similar to what diff_tree() does, only there we deal with scanning only 2 trees simultaneously, and for n+1 tree, the logic is a bit more complex:

D(A,X1...Xn) calculation scheme
-------------------------------

D(A,X1...Xn) = D(A,X1) ^ ... ^ D(A,Xn) (regarding resulting paths set)

D(A,Xj) - diff between A..Xj
D(A,X1...Xn) - combined diff from A to parents X1,...,Xn

We start from all trees, which are sorted, and compare their entries in lock-step:

      A     X1       Xn
      -     -        -
     |a|   |x1|     |xn|
     |-|   |--| ... |--|      i = argmin(x1...xn)
     | |   |  |     |  |
     |-|   |--|     |--|
     |.|   |. |     |. |
      .     .        .
      .     .        .

at any time there could be 3 cases:

     1)  a < xi;
     2)  a > xi;
     3)  a = xi.

Schematic deduction of what every case means, and what to do, follows:

1) a < xi -> ∀j a ∉ Xj -> "+a" ∈ D(A,Xj) -> D += "+a"; a↓

2) a > xi

    2.1) ∃j: xj > xi  ->  "-xi" ∉ D(A,Xj)  ->  D += ø;  ∀ xk=xi  xk↓
    2.2) ∀j  xj = xi  ->  xj ∉ A  ->  "-xj" ∈ D(A,Xj)  ->  D += "-xi";  ∀j xj↓

3) a = xi

    3.1) ∃j: xj > xi  ->  "+a" ∈ D(A,Xj)  ->  only xk=xi remains to investigate
    3.2) xj = xi  ->  investigate δ(a,xj)
     |
     |
     v

    3.1+3.2) looking at δ(a,xk) ∀k: xk=xi - if all != ø  ->

                      ⎧δ(a,xk)  - if xk=xi
             ->  D += ⎨
                      ⎩"+a"     - if xk>xi

    in any case a↓  ∀ xk=xi  xk↓

~

For comparison, here is how diff_tree() works:

D(A,B) calculation scheme
-------------------------

A B
- -

   |a|   |b|    a < b   ->  a ∉ B   ->   D(A,B) +=  +a    a↓
   |-|   |-|    a > b   ->  b ∉ A   ->   D(A,B) +=  -b    b↓
   | |   | |    a = b   ->  investigate δ(a,b)            a↓ b↓
   |-|   |-|
   |.|   |.|
    .     .
    .     .

~~~~~~~~

This patch generalizes diff tree-walker to work with arbitrary number of parents as described above - i.e. now there is a resulting tree t, and some parents trees tp[i] i=[0..nparent). The generalization builds on the fact that usual diff

D(A,B)

is by definition the same as combined diff

D(A,[B]),

so if we could rework the code for common case and make it be not slower for nparent=1 case, usual diff(t1,t2) generation will not be slower, and multiparent diff tree-walker would greatly benefit generating combine-diff.

What we do is as follows:

1) diff tree-walker __diff_tree_sha1() is internally reworked to be a paths generator (new name diff_tree_paths()), with each generated path being struct combine_diff_path with info for path, new sha1,mode and for every parent which sha1,mode it was in it.

2) From that info, we can still generate usual diff queue with struct diff_filepairs, via "exporting" generated combine_diff_path, if we know we run for nparent=1 case. (see emit_diff() which is now named emit_diff_p0only())

3) In order for diff_can_quit_early(), which checks

DIFF_OPT_TST(opt, HAS_CHANGES))

to work, that exporting have to be happening not in bulk, but incrementally, one diff path at a time.

For such consumers, there is a new callback in diff_options introduced:

       ->pathchange(opt, struct combine_diff_path *)

which, if set to !NULL, is called for every generated path.

(see new compat __diff_tree_sha1() wrapper around new paths
generator for setup)

4) The paths generation itself, is reworked from previous __diff_tree_sha1() code according to "D(A,X1...Xn) calculation scheme" provided above:

On the start we allocate [nparent] arrays in place what was earlier just for one parent tree.

then we just generalize loops, and comparison according to the algorithm.

Some notes(*):

1) For loops,

i = 0; do { ... } while (++i < nparent);

is used instead of

for (i = 0; i < nparent; ++i)

     ...

because for the former case, the compiler have to emit additional prologue code which checks for i >= nparent case before entering the loop.

As we require nparent must be >0, that additional overhead conflicts with the "runs not slower for nparent=1 case than before" goal.

2) alloca(), for small arrays, is used for the same reason - if we change it to xmalloc()/free() the timings get worse

3) For every parent tree, we need to keep a tag, whether entry from that parent equals to entry from minimal parent. For performance reasons I'm keeping that tag in entry's mode field in unused bit - see S_IFXMIN_NEQ. Not doing so, we'd need to alloca another [nparent] array, which hurts performance.

4) For emitted paths, memory could be reused, if we know the path was processed via callback and will not be needed later. We efficient hand-made realloc-style __path_appendnew(), that saves us from ~1-1.5% of potential additional slowdown.

5) goto(s) are used in several places, as the code executes a little bit faster with lowered register pressure.

Also

- we should now check for FIND_COPIES_HARDER not only when two entries names are the same, and their hashes are equal, but also for a case, when a path was removed from some of all parents having it.

The reason is, if we don't, that path won't be emitted at all (see "a > xi" case), and we'll just skip it, and FIND_COPIES_HARDER wants all paths - with diff or without - to be emitted, to be later analyzed for being copies sources.

The new check is only necessary for nparent >1, as for nparent=1 case xmin_eqtotal always =1 =nparent, and a path is always added to diff as removal.

~~~~~~~~

Timings for

    # without -c, i.e. testing only nparent=1 case
    `git log --raw --no-abbrev --no-renames`

before and after the patch are as follows:

                navy.git        linux.git v3.10..v3.11

    before      0.611s          1.889s
    after       0.619s          1.907s
    slowdown    1.3%            0.9%

This timings show we did no harm to usual diff(tree1,tree2) generation.

Quoted 1 line

From the table we can see that we actually did ~1% slowdown, but I think

I've "earned" that 1% in the previous patch (729f7e8e in pu) so for nparent=1 case, net timings stays approximately the same.

The output also stayed the same.

(*) If we revert 1)-5) to more usual techniques, for nparent=1 case,
we'll get ~2-2.5% of additional slowdown, which I've tried to avoid, as
"do no harm for nparent=1 case" rule.

For linux.git, combined diff will run an order of magnitude faster and appropriate timings will be provided in the next commit, as we'll be taking advantage of the new diff tree-walker for combined-diff generation there.

P.S. and combined diff is not some exotic/for-play-only stuff - for example for a program I write to represent Git archives as readonly filesystem, there is initial scan with

    `git log --reverse --raw --no-abbrev --no-renames -c`

to extract log of what was created/changed when, as a result building a map

    {}  sha1    ->  in which commit (and date) a content was added

that -c means also show combined diff for merges, and without them, if a merge is non-trivial (merges changes from two parents with both having separate changes to a file), or an evil one, the map will not be full, i.e. some valid sha1 would be absent from it.

That case was my initial motivation for combined diffs speedup.

---
 diff.c      |   1 +
 diff.h      |  10 ++
 tree-diff.c | 510 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 457 insertions(+), 64 deletions(-)

Changes since v1:

- fixed p->next assignment/"dereference" as integer, via proper use of intptr_t;
- reworked multiline comments to start and end with /* and */ on separate lines.

diff.c +1 -0
index 8e4a6a9..cda4aa8 100644
@@ -3216,6 +3216,7 @@ void diff_setup(struct diff_options *options)
 	options->context = diff_context_default;
 	DIFF_OPT_SET(options, RENAME_EMPTY);
 
+	/* pathchange left =NULL by default */
 	options->change = diff_change;
 	options->add_remove = diff_addremove;
 	options->use_color = diff_use_color_default;
diff.h +10 -0
index bf834ff..b59e8d6 100644
@@ -15,6 +15,10 @@ struct diff_filespec;
 struct userdiff_driver;
 struct sha1_array;
 struct commit;
+struct combine_diff_path;
+
+typedef int (*pathchange_fn_t)(struct diff_options *options,
+		 struct combine_diff_path *path);
 
 typedef void (*change_fn_t)(struct diff_options *options,
 		 unsigned old_mode, unsigned new_mode,
@@ -157,6 +161,7 @@ struct diff_options {
 	int close_file;
 
 	struct pathspec pathspec;
+	pathchange_fn_t pathchange;
 	change_fn_t change;
 	add_remove_fn_t add_remove;
 	diff_format_fn_t format_callback;
@@ -189,6 +194,11 @@ const char *diff_line_prefix(struct diff_options *);
 
 extern const char mime_boundary_leader[];
 
+extern
+struct combine_diff_path *diff_tree_paths(
+	struct combine_diff_path *p, const unsigned char *sha1,
+	const unsigned char **parent_sha1, int nparent,
+	struct strbuf *base, struct diff_options *opt);
 extern int __diff_tree_sha1(const unsigned char *old, const unsigned char *new,
 			    struct strbuf *base, struct diff_options *opt);
 extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,
tree-diff.c +446 -64

607 lines not on this page · raw message

Signature · 1 line
1.9.rc1.181.g641f458
#4 lore raw

Re: [PATCH 1/2] tree-diff: rework diff_tree() to generate diffs for multiparent cases as well

Quoted 7 lines · Kirill Smelkov

---- 8< ----
From: Kirill Smelkov <[email protected]>
Subject: [PATCH v2 1/2] tree-diff: rework diff_tree() to generate diffs for
multiparent cases as well
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The last three do not belong here. Only From, Date and Subject are relevant and taken as in-body headers.

For that matter, as the From and Subject above are exactly the same as what you have on the e-mail header, you do not want any of them.

I'll strip them out here, so no need to resend. Thanks.

Quoted 1 line

Previously diff_tree(), which is now named __diff_tree_sha1(), was

That name with two leading underscores is a rather unfortunate, especially for a function that is not a file scope static. No way to rename this to something more sensible?

Quoted 2 lines

That impedance mismatch *hurts* *performance* *badly* for generating combined diffs - in c839f1bd (combine-diff: optimize combine_diff_path

Please avoid referring to a commit that is not in 'master' by its object name. It can be reworked later and get a different name.

Quoted 4 lines

That slowness comes from the fact that currently, while generating combined diff, a lot of time is spent computing diff(commit,commit^2) just to only then intersect that huge diff to almost small set of files from diff(commit,commit^1).

Good observation.

Quoted 3 lines
   |a|   |b|    a < b   ->  a ∉ B   ->   D(A,B) +=  +a    a↓
   |-|   |-|    a > b   ->  b ∉ A   ->   D(A,B) +=  -b    b↓
   | |   | |    a = b   ->  investigate δ(a,b)            a↓ b↓

In both the "n-parallel" and "diff-tree", when an entry 'a' is a tree, I take this "D(A,B) += +a" to mean (recursively) adding all the paths within 'a' to the result as addition. Sounds sensible.

Quoted 10 lines

D(A,B)

is by definition the same as combined diff

D(A,[B]),

so if we could rework the code for common case and make it be not slower for nparent=1 case, usual diff(t1,t2) generation will not be slower, and multiparent diff tree-walker would greatly benefit generating combine-diff.

OK.

Quoted 11 lines

What we do is as follows:

1) diff tree-walker __diff_tree_sha1() is internally reworked to be a paths generator (new name diff_tree_paths()), with each generated path being struct combine_diff_path with info for path, new sha1,mode and for every parent which sha1,mode it was in it.

2) From that info, we can still generate usual diff queue with struct diff_filepairs, via "exporting" generated combine_diff_path, if we know we run for nparent=1 case. (see emit_diff() which is now named emit_diff_p0only())

s/p0/first_parent_/; perhaps?

Quoted 6 lines

3) In order for diff_can_quit_early(), which checks

DIFF_OPT_TST(opt, HAS_CHANGES))

to work, that exporting have to be happening not in bulk, but incrementally, one diff path at a time.

Good thinking.

Quoted 11 lines

Some notes(*):

1) For loops,

i = 0; do { ... } while (++i < nparent);

is used instead of

for (i = 0; i < nparent; ++i)

     ...
Quoted 7 lines

because for the former case, the compiler have to emit additional prologue code which checks for i >= nparent case before entering the loop.

As we require nparent must be >0, that additional overhead conflicts with the "runs not slower for nparent=1 case than before" goal.

Unfortunate. I'd rather see us stick to more readable and familiar form for maintainability if this were not measurable.

Quoted 2 lines

2) alloca(), for small arrays, is used for the same reason - if we change it to xmalloc()/free() the timings get worse

Do you see any use of it outside compat/?

I thought we specifically avoid alloca() for portability. Also we do not use variable-length-arrays on the stack either, I think.

Quoted 3 lines

3) For every parent tree, we need to keep a tag, whether entry from that parent equals to entry from minimal parent. For performance reasons I'm keeping that tag in entry's mode field in unused bit - see S_IFXMIN_NEQ.

Unfortunate, but I do not see another place to keep this information offhand (nor implement this approach without keeping that piece of information).

Quoted 1 line

P.S. and combined diff is not some exotic/for-play-only stuff - for

No need to convince us about that ;-)

Quoted 8 lines

example for a program I write to represent Git archives as readonly filesystem, there is initial scan with

    `git log --reverse --raw --no-abbrev --no-renames -c`

to extract log of what was created/changed when, as a result building a map

Quoted 8 lines
    {}  sha1    ->  in which commit (and date) a content was added

that -c means also show combined diff for merges, and without them, if a merge is non-trivial (merges changes from two parents with both having separate changes to a file), or an evil one, the map will not be full, i.e. some valid sha1 would be absent from it.

That case was my initial motivation for combined diffs speedup.

I wonder if this machinery can be reused for "log -m" as well (or perhaps you do that already?). After all, by performing a single parallel scan, you are gathering all the necessary information to let you pretend that you did N pairwise diff-tree.

Quoted 9 lines
tree-diff.c +1 -0
index ab61a0a..2b7c991 100644
@@ -7,6 +7,25 @@
 #include "tree.h"
 
 /*
+ * internal mode marker, saying a tree entry != entry of tp[imin]

tree-diff.c line 20

Quoted 10 lines
tree-diff.c +10 -0
+ * (see __diff_tree_paths for what it means there)
+ *
+ * it *must* not overlap with any valid modes, and we will update/use/emit
+ * entry for diff only with it unset. Only non-overlapping to valid modes is
+ * required, because mode in tree_desc, comes here canonicalized via
+ * canon_mode().
+ *
+ * the definition assumes unsigned is at least 32 bits.
+ */
+#define S_IFXMIN_NEQ	0x80000000

To allow better coordination across multiple codepaths that deal with modes, I am wondering if this should be defined in cache.h where made-up S_FIGITLINK and S_IFINVALID are defined (note the comment that is there, as well).

tree-diff.c line 26

Quoted 4 lines
tree-diff.c +4 -0
+static struct combine_diff_path *__diff_tree_paths(
+	struct combine_diff_path *p, const unsigned char *sha1,
+	const unsigned char **parents_sha1, int nparent,
+	struct strbuf *base, struct diff_options *opt);

Most of our code do not name private helper functions with leading underscores.

I do like the direction this is going, but it looks to me that "struct combine_diff" is now misnamed, because it no longer is about combined diff. You are introducing a good framework for n-way diff, and producing combined diff (i.e. -c or --cc) is now merely one way to use that framework. We may want to clean these names up after this series settles---perhaps "struct nway_diff" or something.

Quoted 6 lines
tree-diff.c +2 -0
+
+/*
  * Compare two tree entries, taking into account only path/S_ISDIR(mode),
  * but not their sha1's.
  *

tree-diff.c static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) · line 58

Quoted 10 lines
tree-diff.c +4 -3
@@ -33,72 +52,152 @@ static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2)
 }
 
 
-/* convert path, t1/t2 -> opt->diff_*() callbacks */
-static void emit_diff(struct diff_options *opt, struct strbuf *path,
-		      struct tree_desc *t1, struct tree_desc *t2)
+/*
+ * convert path -> opt->diff_*() callbacks
+ *
+ * emits diff to parent0 only.

Please call that "first parent".

tree-diff.c static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) · line 59

Quoted 1 line
tree-diff.c +1 -0
+ */

"Returns 0 to tell the caller that we are done with p and it can be freed" or something?

tree-diff.c static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) · line 86

Quoted 5 lines
tree-diff.c +3 -0
+static int emit_diff_p0only(struct diff_options *opt, struct combine_diff_path *p)
 {
...
+
+	return 0;	/* = no need to keep allocated combine_diff_path */

Curious; what is that equal sign in the comment?

#5 lore raw

Re: [PATCH 1/2] tree-diff: rework diff_tree() to generate diffs for multiparent cases as well

Quoted 7 lines · Junio C Hamano
Quoted 1 line · Kirill Smelkov

Previously diff_tree(), which is now named __diff_tree_sha1(), was

That name with two leading underscores is a rather unfortunate, especially for a function that is not a file scope static. No way to rename this to something more sensible?

I agree. In preparatory patches I thought this will go away, but it stayed. I'll try to come up with something reasonable.

Quoted 5 lines
Quoted 2 lines

That impedance mismatch *hurts* *performance* *badly* for generating combined diffs - in c839f1bd (combine-diff: optimize combine_diff_path

Please avoid referring to a commit that is not in 'master' by its object name. It can be reworked later and get a different name.

I agree, this makes sense. Is it ok to refer to nearby commits, by say HEAD~3, if we know we are referring to 3-times previous commit?

Quoted 7 lines
Quoted 4 lines

That slowness comes from the fact that currently, while generating combined diff, a lot of time is spent computing diff(commit,commit^2) just to only then intersect that huge diff to almost small set of files from diff(commit,commit^1).

Good observation.

Quoted 7 lines
Quoted 3 lines
   |a|   |b|    a < b   ->  a ∉ B   ->   D(A,B) +=  +a    a↓
   |-|   |-|    a > b   ->  b ∉ A   ->   D(A,B) +=  -b    b↓
   | |   | |    a = b   ->  investigate δ(a,b)            a↓ b↓

In both the "n-parallel" and "diff-tree", when an entry 'a' is a tree, I take this "D(A,B) += +a" to mean (recursively) adding all the paths within 'a' to the result as addition. Sounds sensible.

Correct.

Quoted 4 lines
Quoted 4 lines

D(A,B)

is by definition the same as combined diff

Quoted 8 lines
Quoted 6 lines

D(A,[B]),

so if we could rework the code for common case and make it be not slower for nparent=1 case, usual diff(t1,t2) generation will not be slower, and multiparent diff tree-walker would greatly benefit generating combine-diff.

OK.

Thanks. My first goal was to demonstrate it is doable - i.e. we could join two diff tree-walkers into generalized one, and that approach would be sound and have chances to be accepted.

Quoted 7 lines
Quoted 7 lines

What we do is as follows:

1) diff tree-walker __diff_tree_sha1() is internally reworked to be a paths generator (new name diff_tree_paths()), with each generated path being struct combine_diff_path with info for path, new sha1,mode and for every parent which sha1,mode it was in it.

Quoted 6 lines
Quoted 4 lines

2) From that info, we can still generate usual diff queue with struct diff_filepairs, via "exporting" generated combine_diff_path, if we know we run for nparent=1 case. (see emit_diff() which is now named emit_diff_p0only())

s/p0/first_parent_/; perhaps?

Ok.

Quoted 8 lines
Quoted 6 lines

3) In order for diff_can_quit_early(), which checks

DIFF_OPT_TST(opt, HAS_CHANGES))

to work, that exporting have to be happening not in bulk, but incrementally, one diff path at a time.

Good thinking.

Yes. This requirement made the diff-paths producer be real generator, which emits paths incrementally, which is imho, could be a good design for making the component more reusable.

Quoted 15 lines
Quoted 15 lines

Some notes(*):

1) For loops,

i = 0; do { ... } while (++i < nparent);

is used instead of

for (i = 0; i < nparent; ++i)

     ...

because for the former case, the compiler have to emit additional prologue code which checks for i >= nparent case before entering the loop.

Quoted 6 lines
Quoted 3 lines

As we require nparent must be >0, that additional overhead conflicts with the "runs not slower for nparent=1 case than before" goal.

Unfortunate. I'd rather see us stick to more readable and familiar form for maintainability if this were not measurable.

The most effect on performance were avoiding mallocs and reduce register pressure. This too, was measurable, but of lower impact. If giving away some part of percent for nparent=1 case is ok, this could be back to usual for loops.

By the way, I find it a bit unfortunate, we don't have some for-loop form with post-conditions in C, only do-while. Another observation, is that it would be good to say to compiler e.g.

    assume nparent > 0;

and then in e.g.

    for (i = 0; i < nparent; i++)
        ...

the compiler could know it should not do pre-conditions checks before entering the loop.

I've verified, that such behaviour, at least with gcc, could be achieved with

    if (nparent <= 0)
        return;

in function prologue - then the compiler deduces, at least with O2, that after return point, nparent is >0, but this adds slight overhead on each entry to function, and we have as many calls as there would be recursions.

To me, the do-while is still readable, but if fors are preferred, I'll re-measure what it costs and come back.

Quoted 7 lines
Quoted 2 lines

2) alloca(), for small arrays, is used for the same reason - if we change it to xmalloc()/free() the timings get worse

Do you see any use of it outside compat/?

I thought we specifically avoid alloca() for portability. Also we do not use variable-length-arrays on the stack either, I think.

No, no usage outside compat/ and I knew alloca and VLAs are not used in Git codebase for portability, and I understand alloca will be criticized, but wanted to start the discussion rolling.

I've actually started without alloca, and used xmalloc/free for [nparent] vectors, but the impact was measurable, so it just had to be changed to something more optimal.

For me, personally, alloca is ok, but I understand there could be portability issues (by the way, what compiler/system Git cares about does not have working alloca?). Thats why I propose we do the following

1. at configure time, determine, do we have working alloca, and define

    #define HAVE_ALLOCA

if yes.

2. in code

#ifdef HAVE_ALLOCA
# define xalloca(size) (alloca(size))
# define xalloca_free(p) do {} while(0)
#else
# define xalloca(size) (xmalloc(size))
# define xalloca_free(p) (free(p))
#endif

and use it like

   func() {
       p = xalloca(size);
       ...
       xalloca_free(p);

}

This way, for systems, where alloca is available, we'll have optimal on-stack allocations with fast executions. On the other hand, on systems, where alloca is not available, this gracefully fallbacks to xmalloc/free.

Please tell me what you think.

Quoted 7 lines
Quoted 3 lines

3) For every parent tree, we need to keep a tag, whether entry from that parent equals to entry from minimal parent. For performance reasons I'm keeping that tag in entry's mode field in unused bit - see S_IFXMIN_NEQ.

Unfortunate, but I do not see another place to keep this information offhand (nor implement this approach without keeping that piece of information).

Me neither. I've actually started with separate on-stack (via alloca) [nparent] vector for such tags, and removing it was measurable.

Quoted 3 lines
Quoted 1 line

P.S. and combined diff is not some exotic/for-play-only stuff - for

No need to convince us about that ;-)

Thanks ;) May I ask to please keep my "motivation" part in the commit log for historical reasons?

Quoted 15 lines
Quoted 15 lines

example for a program I write to represent Git archives as readonly filesystem, there is initial scan with

    `git log --reverse --raw --no-abbrev --no-renames -c`

to extract log of what was created/changed when, as a result building a map

    {}  sha1    ->  in which commit (and date) a content was added

that -c means also show combined diff for merges, and without them, if a merge is non-trivial (merges changes from two parents with both having separate changes to a file), or an evil one, the map will not be full, i.e. some valid sha1 would be absent from it.

Quoted 6 lines
Quoted 1 line

That case was my initial motivation for combined diffs speedup.

I wonder if this machinery can be reused for "log -m" as well (or perhaps you do that already?). After all, by performing a single parallel scan, you are gathering all the necessary information to let you pretend that you did N pairwise diff-tree.

Unfortunately, as it is now, no, and let me explain why:

The reason that is not true, is that we omit recursing into directories, if we know D(A,some-parent) for that path is empty. That means we don't calculate D(A,any-other-parents) for that path and subpaths.

More structured description is that combined diff and "log -m", which could be though as all diffs D(A,Pi) are different things:

- the combined diff is D(A,B) generalization based on "^" (sets intersection) operator, and

- log -m, aka "all diffs" is D(A,B) generalization based on "v" (sets union) operator.

Intersection means, we can omit calculating parts from other sets, if we know some set does not have an element (remember "don't recurse into subdirectories"?), and unioning does not have this property.

It does so happen, that "^" case (combine-diff) is more interesting, because in the end it allows to see new information - the diff a merge itself introduces. "log -m" does not have this property and is no more interesting to what plain diff(HEAD,HEAD^n) can provide - in other words it's just a convenience.

Now, the diff tree-walker could be generalized once more, to allow clients specify, which diffs combination operator to use - intersection or unioning, but I doubt that for unioning case that would add significant speedup - we can't reduce any diff generation based on another diff and the only saving is that we traverse resulting commit tree once, but for some cases that could be maybe slower, say if result and some parents don't have a path and some parent does, we'll be recursing into that path and do more work compared to plain D(A,Pi) for Pi that lacks the path.

In short: it could be generalized more, if needed, but I propose we first establish the ground with generalizing to just combine-diff.

Quoted 16 lines
Quoted 16 lines
tree-diff.c +8 -0
index ab61a0a..2b7c991 100644
@@ -7,6 +7,25 @@
 #include "tree.h"
 
 /*
+ * internal mode marker, saying a tree entry != entry of tp[imin]
+ * (see __diff_tree_paths for what it means there)
+ *
+ * it *must* not overlap with any valid modes, and we will update/use/emit
+ * entry for diff only with it unset. Only non-overlapping to valid modes is
+ * required, because mode in tree_desc, comes here canonicalized via
+ * canon_mode().
+ *
Quoted 8 lines

tree-diff.c line 20

Quoted 3 lines
tree-diff.c +3 -0
+ * the definition assumes unsigned is at least 32 bits.
+ */
+#define S_IFXMIN_NEQ	0x80000000

To allow better coordination across multiple codepaths that deal with modes, I am wondering if this should be defined in cache.h where made-up S_FIGITLINK and S_IFINVALID are defined (note the comment that is there, as well).

I knew about S_IFGITLINK and S_IFINVALID being located in cache.h with comments.

The reason I decided to place S_IFXMIN_NEQ here, is that it is local tag
- it is used locally in diff-tree, and neither is coming in, nor coming out of it in set state.

So putting it in cache.h would mean we'll reserve the bit for something which is used only temporarily and that bit would be not available for other temporary uses.

On the other hand, it would be better, to mark in cache.h that some bits could be used temporarily for application specific things. Maybe provide a mask there or something similar to S_IFUSR1, S_IFUSR2 :)

S_IFXMIN_NEQ is just has no meaning for code outside of tree-diff.c ...

What do you think?

Quoted 7 lines

tree-diff.c line 26

Quoted 4 lines
tree-diff.c +4 -0
+static struct combine_diff_path *__diff_tree_paths(
+	struct combine_diff_path *p, const unsigned char *sha1,
+	const unsigned char **parents_sha1, int nparent,
+	struct strbuf *base, struct diff_options *opt);

Most of our code do not name private helper functions with leading underscores.

What would be a good name here? In contrast to __diff_tree_sha1, it is static, and adding some prefix/suffix to the name is maybe adding more noise than value? We have diff_tree_paths() which does the setup and cleanup and this worker __diff_tree_paths... Some names come to me as diff_tree_paths_raw, or diff_tree_paths_worker, but having Linux heritage, I like __diff_tree_paths more.

What would be a good approach for Git style here?

Quoted 6 lines

I do like the direction this is going, but it looks to me that "struct combine_diff" is now misnamed, because it no longer is about combined diff. You are introducing a good framework for n-way diff, and producing combined diff (i.e. -c or --cc) is now merely one way to use that framework. We may want to clean these names up after this series settles---perhaps "struct nway_diff" or something.

Thanks. It was my main question whether this reworking will be a good/accepted idea or not. I agree about struct combine_diff_path name and that it should be changed afterwards (my idea was struct diff_path for it).

Given that we had diff_filepair for n=2 diffs, it would logical to call it as maybe

diff_nway,
diff_filevector,
or something else

but maybe emphasizing n/vector/... in the name is not so good in the long run, and using, for generalized diff just

diff_file, or
diff_path

is better.

Naming is important and I don't settled on it however, yet...

Quoted 9 lines
Quoted 9 lines
tree-diff.c +2 -0
+
+/*
  * Compare two tree entries, taking into account only path/S_ISDIR(mode),
  * but not their sha1's.
  *
@@ -33,72 +52,152 @@ static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2)
 }
 
 
Quoted 9 lines

tree-diff.c static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) · line 58

Quoted 7 lines
tree-diff.c +4 -3
-/* convert path, t1/t2 -> opt->diff_*() callbacks */
-static void emit_diff(struct diff_options *opt, struct strbuf *path,
-		      struct tree_desc *t1, struct tree_desc *t2)
+/*
+ * convert path -> opt->diff_*() callbacks
+ *
+ * emits diff to parent0 only.

Please call that "first parent".

Ok.

Quoted 5 lines

tree-diff.c static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) · line 59

Quoted 1 line
tree-diff.c +1 -0
+ */

"Returns 0 to tell the caller that we are done with p and it can be freed" or something?

Quoted 7 lines

tree-diff.c static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) · line 86

Quoted 5 lines
tree-diff.c +3 -0
+static int emit_diff_p0only(struct diff_options *opt, struct combine_diff_path *p)
 {
...
+
+	return 0;	/* = no need to keep allocated combine_diff_path */

Curious; what is that equal sign in the comment?

It stands for "that means", "i.e." or something similar. In other words it is semantically equal to your comment from the above.

I'm open to reworking the commenting style for it to reads more well. Should I?

~~~~

Thanks again for reviewing this and for generally accepting the approach. Please tell me your thoughts. Based on your input, I'll try to come up with something improved on monday.

Thanks,
Kirill

#6 lore raw

Re: [PATCH 1/2] tree-diff: rework diff_tree() to generate diffs for multiparent cases as well

Quoted 45 lines · Kirill Smelkov
Quoted 7 lines
Quoted 2 lines

2) alloca(), for small arrays, is used for the same reason - if we change it to xmalloc()/free() the timings get worse

Do you see any use of it outside compat/?

I thought we specifically avoid alloca() for portability. Also we do not use variable-length-arrays on the stack either, I think.

No, no usage outside compat/ and I knew alloca and VLAs are not used in Git codebase for portability, and I understand alloca will be criticized, but wanted to start the discussion rolling.

I've actually started without alloca, and used xmalloc/free for [nparent] vectors, but the impact was measurable, so it just had to be changed to something more optimal.

For me, personally, alloca is ok, but I understand there could be portability issues (by the way, what compiler/system Git cares about does not have working alloca?). Thats why I propose we do the following

1. at configure time, determine, do we have working alloca, and define

    #define HAVE_ALLOCA

if yes.

2. in code

#ifdef HAVE_ALLOCA
# define xalloca(size) (alloca(size))
# define xalloca_free(p) do {} while(0)
#else
# define xalloca(size) (xmalloc(size))
# define xalloca_free(p) (free(p))
#endif

   and use it like

   func() {
       p = xalloca(size);
       ...

       xalloca_free(p);
   }
Quoted 6 lines

This way, for systems, where alloca is available, we'll have optimal on-stack allocations with fast executions. On the other hand, on systems, where alloca is not available, this gracefully fallbacks to xmalloc/free.

Please tell me what you think.

I guess the above is clean enough, and we cannot do better than that, if we want to use alloca() on platforms where we can.

#7 lore raw

PATCH 2/2 combine-diff: speed it up, by using multiparent diff tree-walker directly

In reply to Kirill Smelkov

As was recently shown (c839f1bd "combine-diff: optimize combine_diff_path sets intersection"), combine-diff runs very slowly. In that commit we optimized paths sets intersection, but that accounted only for ~ 25% of the slowness, and as my tracing showed, for linux.git v3.10..v3.11, for merges a lot of time is spent computing diff(commit,commit^2) just to only then intersect that huge diff to almost small set of files from diff(commit,commit^1).

In previous commit, we described the problem in more details, and reworked the diff tree-walker to be general one - i.e. to work in multiple parent case too. Now is the time to take advantage of it for finding paths for combine diff.

The implementation is straightforward - if we know, we can get generated diff paths directly, and at present that means no diff filtering or rename/copy detection was requested(*), we can call multiparent tree-walker directly and get ready paths.

(*) because e.g. at present, all diffcore transformations work on
diff_filepair queues, but in the future, that limitation can be lifted, if filters would operate directly on combine_diff_paths.

Timings for git log --raw --no-abbrev --no-renames without -c ("git log") and with -c ("git log -c") and with -c --merges ("git log -c --merges") before and after the patch are as follows:

                linux.git v3.10..v3.11

            log     log -c     log -c --merges

    before  1.9s    16.4s      15.2s
    after   1.9s     2.4s       1.1s

The result stayed the same.

---
 combine-diff.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 diff.c         |  1 +
 2 files changed, 81 insertions(+), 5 deletions(-)
combine-diff.c +80 -5
index 1732dfd..ddf7495 100644
@@ -1303,7 +1303,7 @@ static const char *path_path(void *obj)
 
 
 /* find set of paths that every parent touches */
-static struct combine_diff_path *find_paths(const unsigned char *sha1,
+static struct combine_diff_path *find_paths_generic(const unsigned char *sha1,
 	const struct sha1_array *parents, struct diff_options *opt)
 {
 	struct combine_diff_path *paths = NULL;
@@ -1316,6 +1316,7 @@ static struct combine_diff_path *find_paths(const unsigned char *sha1,
 	/* tell diff_tree to emit paths in sorted (=tree) order */
 	opt->orderfile = NULL;
 
+	/* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn)  (wrt paths) */
 	for (i = 0; i < num_parent; i++) {
 		/*
 		 * show stat against the first parent even when doing
@@ -1346,6 +1347,35 @@ static struct combine_diff_path *find_paths(const unsigned char *sha1,
 }
 
 
+/*
+ * find set of paths that everybody touches, assuming diff is run without
+ * rename/copy detection, etc, comparing all trees simultaneously (= faster).
+ */
+static struct combine_diff_path *find_paths_multitree(
+	const unsigned char *sha1, const struct sha1_array *parents,
+	struct diff_options *opt)
+{
+	int i, nparent = parents->nr;
+	const unsigned char **parents_sha1;
+	struct combine_diff_path paths_head;
+	struct strbuf base;
+
+	parents_sha1 = xmalloc(nparent * sizeof(parents_sha1[0]));
+	for (i = 0; i < nparent; i++)
+		parents_sha1[i] = parents->sha1[i];
+
+	/* fake list head, so worker can assume it is non-NULL */
+	paths_head.next = NULL;
+
+	strbuf_init(&base, PATH_MAX);
+	diff_tree_paths(&paths_head, sha1, parents_sha1, nparent, &base, opt);
+
+	strbuf_release(&base);
+	free(parents_sha1);
+	return paths_head.next;
+}
+
+
 void diff_tree_combined(const unsigned char *sha1,
 			const struct sha1_array *parents,
 			int dense,
@@ -1355,6 +1385,7 @@ void diff_tree_combined(const unsigned char *sha1,
 	struct diff_options diffopts;
 	struct combine_diff_path *p, *paths;
 	int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
+	int need_generic_pathscan;
 
 	/* nothing to do, if no parents */
 	if (!num_parent)
@@ -1377,11 +1408,55 @@ void diff_tree_combined(const unsigned char *sha1,
 
 	/* find set of paths that everybody touches
 	 *
-	 * NOTE find_paths() also handles --stat, as it computes
-	 * diff(sha1,parent_i) for all i to do the job, specifically
-	 * for parent0.
+	 * NOTE
+	 *
+	 * Diffcore transformations are bound to diff_filespec and logic
+	 * comparing two entries - i.e. they do not apply directly to combine
+	 * diff.
+	 *
+	 * If some of such transformations is requested - we launch generic
+	 * path scanning, which works significantly slower compared to
+	 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
+	 *
+	 * TODO some of the filters could be ported to work on
+	 * combine_diff_paths - i.e. all functionality that skips paths, so in
+	 * theory, we could end up having only multitree path scanning.
+	 *
+	 * NOTE please keep this semantically in sync with diffcore_std()
 	 */
-	paths = find_paths(sha1, parents, &diffopts);
+	need_generic_pathscan = opt->skip_stat_unmatch	||
+			DIFF_OPT_TST(opt, FOLLOW_RENAMES)	||
+			opt->break_opt != -1	||
+			opt->detect_rename	||
+			opt->pickaxe		||
+			opt->filter;
+
+
+	if (need_generic_pathscan) {
+		/* NOTE generic case also handles --stat, as it computes
+		 * diff(sha1,parent_i) for all i to do the job, specifically
+		 * for parent0.
+		 */
+		paths = find_paths_generic(sha1, parents, &diffopts);
+	}
+	else {
+		paths = find_paths_multitree(sha1, parents, &diffopts);
+
+		/* show stat against the first parent even
+		 * when doing combined diff.
+		 */
+		int stat_opt = (opt->output_format &
+				(DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
+		if (stat_opt) {
+			diffopts.output_format = stat_opt;
+
+			diff_tree_sha1(parents->sha1[0], sha1, "", &diffopts);
+			diffcore_std(&diffopts);
+			if (opt->orderfile)
+				diffcore_order(opt->orderfile);
+			diff_flush(&diffopts);
+		}
+	}
 
 	/* find out number of surviving paths */
 	for (num_paths = 0, p = paths; p; p = p->next)
diff.c +1 -0
index cda4aa8..f2fff46 100644
@@ -4764,6 +4764,7 @@ void diffcore_fix_diff_index(struct diff_options *options)
 
 void diffcore_std(struct diff_options *options)
 {
+	/* NOTE please keep the following in sync with diff_tree_combined() */
 	if (options->skip_stat_unmatch)
 		diffcore_skip_stat_unmatch(options);
 	if (!options->found_follow) {
Signature · 1 line
1.9.rc1.181.g641f458
#8 lore raw

Re: [PATCH 2/2] combine-diff: speed it up, by using multiparent diff tree-walker directly

Quoted 5 lines · Kirill Smelkov
combine-diff.c +5 -0
+	if (need_generic_pathscan) {
+		/* NOTE generic case also handles --stat, as it computes
+		 * diff(sha1,parent_i) for all i to do the job, specifically
+		 * for parent0.
+		 */

combine-diff.c void diff_tree_combined(const unsigned char *sha1, · line 1449

Quoted 10 lines
combine-diff.c +10 -0
+		paths = find_paths_generic(sha1, parents, &diffopts);
+	}
+	else {
+		paths = find_paths_multitree(sha1, parents, &diffopts);
+
+		/* show stat against the first parent even
+		 * when doing combined diff.
+		 */
+		int stat_opt = (opt->output_format &
+				(DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));

/*
* We see decl-after-stmt here.
* Also please have slash-asterisk and asterisk-slash
* at the beginning and the end of a multi-line comment
* block on their own line.
*/

#9 lore raw

Re: [PATCH 2/2] combine-diff: speed it up, by using multiparent diff tree-walker directly

Quoted 16 lines · Junio C Hamano
Quoted 14 lines · Kirill Smelkov
(quoted diff) +14 -0
+	if (need_generic_pathscan) {
+		/* NOTE generic case also handles --stat, as it computes
+		 * diff(sha1,parent_i) for all i to do the job, specifically
+		 * for parent0.
+		 */
+		paths = find_paths_generic(sha1, parents, &diffopts);
+	}
+	else {
+		paths = find_paths_multitree(sha1, parents, &diffopts);
+
+		/* show stat against the first parent even
+		 * when doing combined diff.
+		 */
+		int stat_opt = (opt->output_format &
Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+				(DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));

/*
* We see decl-after-stmt here.
* Also please have slash-asterisk and asterisk-slash
* at the beginning and the end of a multi-line comment
* block on their own line.
*/

Sorry, and thanks for noticing. I usually compile with -Wall, but it seems it is not enough without explicitly specifying -std=c89.

Comments corrected and the decl-after-stmt fixed, and this time I've compiled with -std=c89 -pedantic -Wall -Wextra to assure no new warnings are introduced.

Please apply and thanks beforehand,
Kirill

---- 8< ----
From: Kirill Smelkov <[email protected]>
Subject: [PATCH v2 2/2] combine-diff: speed it up, by using multiparent diff
tree-walker directly

As was recently shown (c839f1bd "combine-diff: optimize combine_diff_path sets intersection"), combine-diff runs very slowly. In that commit we optimized paths sets intersection, but that accounted only for ~ 25% of the slowness, and as my tracing showed, for linux.git v3.10..v3.11, for merges a lot of time is spent computing diff(commit,commit^2) just to only then intersect that huge diff to almost small set of files from diff(commit,commit^1).

In previous commit, we described the problem in more details, and reworked the diff tree-walker to be general one - i.e. to work in multiple parent case too. Now is the time to take advantage of it for finding paths for combine diff.

The implementation is straightforward - if we know, we can get generated diff paths directly, and at present that means no diff filtering or rename/copy detection was requested(*), we can call multiparent tree-walker directly and get ready paths.

(*) because e.g. at present, all diffcore transformations work on
diff_filepair queues, but in the future, that limitation can be lifted, if filters would operate directly on combine_diff_paths.

Timings for git log --raw --no-abbrev --no-renames without -c ("git log") and with -c ("git log -c") and with -c --merges ("git log -c --merges") before and after the patch are as follows:

                linux.git v3.10..v3.11

            log     log -c     log -c --merges

    before  1.9s    16.4s      15.2s
    after   1.9s     2.4s       1.1s

The result stayed the same.

---
 combine-diff.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 diff.c         |  1 +
 2 files changed, 84 insertions(+), 5 deletions(-)

Chages since v1:

- fixed declaration-after-statement, and reworked multiline comments to start and end with /* and */ on separate lines.

combine-diff.c +83 -5
index 1732dfd..12764fb 100644
@@ -1303,7 +1303,7 @@ static const char *path_path(void *obj)
 
 
 /* find set of paths that every parent touches */
-static struct combine_diff_path *find_paths(const unsigned char *sha1,
+static struct combine_diff_path *find_paths_generic(const unsigned char *sha1,
 	const struct sha1_array *parents, struct diff_options *opt)
 {
 	struct combine_diff_path *paths = NULL;
@@ -1316,6 +1316,7 @@ static struct combine_diff_path *find_paths(const unsigned char *sha1,
 	/* tell diff_tree to emit paths in sorted (=tree) order */
 	opt->orderfile = NULL;
 
+	/* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn)  (wrt paths) */
 	for (i = 0; i < num_parent; i++) {
 		/*
 		 * show stat against the first parent even when doing
@@ -1346,6 +1347,35 @@ static struct combine_diff_path *find_paths(const unsigned char *sha1,
 }
 
 
+/*
+ * find set of paths that everybody touches, assuming diff is run without
+ * rename/copy detection, etc, comparing all trees simultaneously (= faster).
+ */
+static struct combine_diff_path *find_paths_multitree(
+	const unsigned char *sha1, const struct sha1_array *parents,
+	struct diff_options *opt)
+{
+	int i, nparent = parents->nr;
+	const unsigned char **parents_sha1;
+	struct combine_diff_path paths_head;
+	struct strbuf base;
+
+	parents_sha1 = xmalloc(nparent * sizeof(parents_sha1[0]));
+	for (i = 0; i < nparent; i++)
+		parents_sha1[i] = parents->sha1[i];
+
+	/* fake list head, so worker can assume it is non-NULL */
+	paths_head.next = NULL;
+
+	strbuf_init(&base, PATH_MAX);
+	diff_tree_paths(&paths_head, sha1, parents_sha1, nparent, &base, opt);
+
+	strbuf_release(&base);
+	free(parents_sha1);
+	return paths_head.next;
+}
+
+
 void diff_tree_combined(const unsigned char *sha1,
 			const struct sha1_array *parents,
 			int dense,
@@ -1355,6 +1385,7 @@ void diff_tree_combined(const unsigned char *sha1,
 	struct diff_options diffopts;
 	struct combine_diff_path *p, *paths;
 	int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
+	int need_generic_pathscan;
 
 	/* nothing to do, if no parents */
 	if (!num_parent)
@@ -1377,11 +1408,58 @@ void diff_tree_combined(const unsigned char *sha1,
 
 	/* find set of paths that everybody touches
 	 *
-	 * NOTE find_paths() also handles --stat, as it computes
-	 * diff(sha1,parent_i) for all i to do the job, specifically
-	 * for parent0.
+	 * NOTE
+	 *
+	 * Diffcore transformations are bound to diff_filespec and logic
+	 * comparing two entries - i.e. they do not apply directly to combine
+	 * diff.
+	 *
+	 * If some of such transformations is requested - we launch generic
+	 * path scanning, which works significantly slower compared to
+	 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
+	 *
+	 * TODO some of the filters could be ported to work on
+	 * combine_diff_paths - i.e. all functionality that skips paths, so in
+	 * theory, we could end up having only multitree path scanning.
+	 *
+	 * NOTE please keep this semantically in sync with diffcore_std()
 	 */
-	paths = find_paths(sha1, parents, &diffopts);
+	need_generic_pathscan = opt->skip_stat_unmatch	||
+			DIFF_OPT_TST(opt, FOLLOW_RENAMES)	||
+			opt->break_opt != -1	||
+			opt->detect_rename	||
+			opt->pickaxe		||
+			opt->filter;
+
+
+	if (need_generic_pathscan) {
+		/*
+		 * NOTE generic case also handles --stat, as it computes
+		 * diff(sha1,parent_i) for all i to do the job, specifically
+		 * for parent0.
+		 */
+		paths = find_paths_generic(sha1, parents, &diffopts);
+	}
+	else {
+		int stat_opt;
+		paths = find_paths_multitree(sha1, parents, &diffopts);
+
+		/*
+		 * show stat against the first parent even
+		 * when doing combined diff.
+		 */
+		stat_opt = (opt->output_format &
+				(DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
+		if (stat_opt) {
+			diffopts.output_format = stat_opt;
+
+			diff_tree_sha1(parents->sha1[0], sha1, "", &diffopts);
+			diffcore_std(&diffopts);
+			if (opt->orderfile)
+				diffcore_order(opt->orderfile);
+			diff_flush(&diffopts);
+		}
+	}
 
 	/* find out number of surviving paths */
 	for (num_paths = 0, p = paths; p; p = p->next)
diff.c +1 -0
index cda4aa8..f2fff46 100644
@@ -4764,6 +4764,7 @@ void diffcore_fix_diff_index(struct diff_options *options)
 
 void diffcore_std(struct diff_options *options)
 {
+	/* NOTE please keep the following in sync with diff_tree_combined() */
 	if (options->skip_stat_unmatch)
 		diffcore_skip_stat_unmatch(options);
 	if (!options->found_follow) {
Signature · 1 line
1.9.rc1.181.g641f458