Skip to contentSkip to conversation

diffcore-rename speedup

PATCH New [email protected] 4 participants 13 messages Apr 18, 2017 – Apr 21, 2017

lore
Share & tools
lore
#1 lore raw

PATCH 1/1 diffcore-rename: speed up register_rename_src

In reply to [email protected]

From: Jeff Hostetler <[email protected]>

Teach register_rename_src() to see if new file pair can simply be appended to the rename_src[] array before performing the binary search to find the proper insertion point.

This is a performance optimization. This routine is called during run_diff_files in status and the caller is iterating over the sorted index, so we should expect to be able to append in the normal case. The existing insert logic is preserved so we don't have to assume that, but simply take advantage of it if possible.

Using t/perf/p0005-status.h (from a parallel patch series), we get the following improvement on a 4.2M file repo:

Test                                            HEAD~1               HEAD
0005.2: read-tree status br_ballast (4194305)   59.14(31.85+18.79)   55.48(28.52+20.71) -6.2%

On a 1M file repo:

Test                                            HEAD~1            HEAD
0005.2: read-tree status br_ballast (1000001)   8.20(4.82+3.35)   7.91(4.57+3.27) -3.5%

On a smaller repo, like linux.git (58K files), results are masked by normal I/O variance.

Test                                          HEAD~1            HEAD
0005.2: read-tree status br_ballast (57994)   0.43(0.30+0.13)   0.42(0.31+0.12) -2.3%
0005.2: read-tree status br_ballast (57994)   0.42(0.32+0.09)   0.43(0.34+0.10) +2.4%
0005.2: read-tree status br_ballast (57994)   0.44(0.33+0.10)   0.42(0.26+0.16) -4.5%
---
 diffcore-rename.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
diffcore-rename.c +13 -0
index f7444c8..543a409 100644
@@ -81,6 +81,18 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
 
 	first = 0;
 	last = rename_src_nr;
+
+	if (last > 0) {
+		struct diff_rename_src *src = &(rename_src[last-1]);
+		int cmp = strcmp(one->path, src->p->one->path);
+		if (!cmp)
+			return src;
+		if (cmp > 0) {
+			first = last;
+			goto append_it;
+		}
+	}
+
 	while (last > first) {
 		int next = (last + first) >> 1;
 		struct diff_rename_src *src = &(rename_src[next]);
@@ -94,6 +106,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
 		first = next+1;
 	}
 
+append_it:
 	/* insert to make it at "first" */
 	ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
 	rename_src_nr++;
Signature · 1 line
2.9.3
#0 lore raw

PATCH 1/1 diffcore-rename speedup

From: Jeff Hostetler <[email protected]>

Here is another micro-optimization for very large repositories. Speed up register_rename_src() in diffcore-rename.c

Jeff Hostetler (1):
diffcore-rename: speed up register_rename_src

 diffcore-rename.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Signature · 1 line
2.9.3
#2 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

In reply to [email protected]
Quoted 6 lines · On Tue, Apr 18, 2017 at 07:44:21PM +0000, [email protected] wrote:

From: Jeff Hostetler <[email protected]>

Teach register_rename_src() to see if new file pair can simply be appended to the rename_src[] array before performing the binary search to find the proper insertion point.

I guess your perf results show some minor improvement. But I suspect this is because your synthetic repo does not resemble the real world very much. You're saving a few strcmps, but for each of those files you're potentially going to have actually zlib inflate the object contents and do similarity analysis.

So "absurd number of files doing 100% exact renames" is the absolute best case, and it saves a few percent.

I dunno. It is not that much code _here_, but I'm not excited about the prospect of sprinkling this same "check the last one" optimization all over the code base. I wonder if there's some way to generalize it.

-Peff

#3 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

Quoted 15 lines · Jeff King
Quoted 6 lines · On Tue, Apr 18, 2017 at 07:44:21PM +0000, [email protected] wrote:

From: Jeff Hostetler <[email protected]>

Teach register_rename_src() to see if new file pair can simply be appended to the rename_src[] array before performing the binary search to find the proper insertion point.

I guess your perf results show some minor improvement. But I suspect this is because your synthetic repo does not resemble the real world very much. You're saving a few strcmps, but for each of those files you're potentially going to have actually zlib inflate the object contents and do similarity analysis.

Quoted 6 lines

So "absurd number of files doing 100% exact renames" is the absolute best case, and it saves a few percent.

I dunno. It is not that much code _here_, but I'm not excited about the prospect of sprinkling this same "check the last one" optimization all over the code base. I wonder if there's some way to generalize it.

When adding many things, we often just append and then sort at the end after we finished adding. I wonder if recent "check the last one and append" optimization beats that strategy.

#4 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

Quoted 20 lines · Junio C Hamano
Quoted 18 lines · Jeff King
Quoted 6 lines · On Tue, Apr 18, 2017 at 07:44:21PM +0000, [email protected] wrote:

From: Jeff Hostetler <[email protected]>

Teach register_rename_src() to see if new file pair can simply be appended to the rename_src[] array before performing the binary search to find the proper insertion point.

I guess your perf results show some minor improvement. But I suspect this is because your synthetic repo does not resemble the real world very much. You're saving a few strcmps, but for each of those files you're potentially going to have actually zlib inflate the object contents and do similarity analysis.

So "absurd number of files doing 100% exact renames" is the absolute best case, and it saves a few percent.

Quoted 7 lines
Quoted 3 lines

I dunno. It is not that much code _here_, but I'm not excited about the prospect of sprinkling this same "check the last one" optimization all over the code base. I wonder if there's some way to generalize it.

When adding many things, we often just append and then sort at the end after we finished adding. I wonder if recent "check the last one and append" optimization beats that strategy.

The big question is whether we need to detect duplicates while we're appending to the list, which is hard on an unsorted list. In this function, at least, we do detect when the path already exists and return the existing entry. I'm not sure under what circumstances we would see such a duplicate, though, as each filename should appear only once in the tree diff. I would think.

Doing:

diffcore-rename.c +1 -1
index f7444c86b..56a493d97 100644
@@ -86,7 +86,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
 		struct diff_rename_src *src = &(rename_src[next]);
 		int cmp = strcmp(one->path, src->p->one->path);
 		if (!cmp)
-			return src;
+			die("BUG: duplicate rename src: %s", one->path);
 		if (cmp < 0) {
 			last = next;
 			continue;

passes the test suite, at least. :)

-Peff

#5 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

Quoted 27 lines · Jeff King
Quoted 3 lines

When adding many things, we often just append and then sort at the end after we finished adding. I wonder if recent "check the last one and append" optimization beats that strategy.

The big question is whether we need to detect duplicates while we're appending to the list, which is hard on an unsorted list. In this function, at least, we do detect when the path already exists and return the existing entry. I'm not sure under what circumstances we would see such a duplicate, though, as each filename should appear only once in the tree diff. I would think.

Doing:

diffcore-rename.c +1 -1
index f7444c86b..56a493d97 100644
@@ -86,7 +86,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
 		struct diff_rename_src *src = &(rename_src[next]);
 		int cmp = strcmp(one->path, src->p->one->path);
 		if (!cmp)
-			return src;
+			die("BUG: duplicate rename src: %s", one->path);
 		if (cmp < 0) {
 			last = next;
 			continue;
Quoted 1 line

passes the test suite, at least. :)

Maybe relevant: 4d6be03b9 (diffcore-rename: avoid processing duplicate destinations, 2015-02-26). That's on the dst side, but possibly we should do something similar on the src side.

BTW, I think the return value from register_rename_src() is questionable. It points to a "struct diff_rename_src" that may be reallocated by further calls to the function. Fortunately nobody actually looks at it, let alone saves it, so there's no bug.

We may want to convert that return value to a void (if not just return an int for "hey, there's a duplicate", like we do for add_rename_dst()).

Also, presumably that function could learn the same "check the last one" trick that the src side does. Which leads me back to "surely we can generalize this". I don't think bsearch() is quite what we want, because its interface doesn't tell us where to put the item when it isn't found. But I think we could make a general bsearch-like function that has similar semantics to index_name_pos(), with its negative-integer return.

And then that would be a general lookup function, and we could easily build a general "look up and add" function around that. And the "check the last one" optimization would go in the latter.

-Peff

#11 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

In reply to Jeff King

Hi Peff,

Quoted 11 lines · Jeff King
Quoted 5 lines · On Tue, Apr 18, 2017 at 07:44:21PM +0000, [email protected] wrote:

From: Jeff Hostetler <[email protected]>

Teach register_rename_src() to see if new file pair can simply be appended to the rename_src[] array before performing the binary search to find the proper insertion point.

I guess your perf results show some minor improvement. But I suspect this is because your synthetic repo does not resemble the real world very much.

Please note that the synthetic test repo was added *after* coming up with the patch, *after* performance benchmarking on a certain really big repository (it is not hard to guess what use case we are optimizing, right?).

In that light, I would like to register the fact that Jeff's performance work is trying to improve a very real world, that of more than 2,000 developers in our company [*1*].

Ciao,
Johannes

Footnote *1*: https://twitter.com/GabeAul/status/846189637945110528

#6 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

In reply to Jeff King
Quoted 50 lines · Jeff King
Quoted 27 lines · Jeff King
Quoted 3 lines

When adding many things, we often just append and then sort at the end after we finished adding. I wonder if recent "check the last one and append" optimization beats that strategy.

The big question is whether we need to detect duplicates while we're appending to the list, which is hard on an unsorted list. In this function, at least, we do detect when the path already exists and return the existing entry. I'm not sure under what circumstances we would see such a duplicate, though, as each filename should appear only once in the tree diff. I would think.

Doing:

diffcore-rename.c +1 -1
index f7444c86b..56a493d97 100644
@@ -86,7 +86,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
 		struct diff_rename_src *src = &(rename_src[next]);
 		int cmp = strcmp(one->path, src->p->one->path);
 		if (!cmp)
-			return src;
+			die("BUG: duplicate rename src: %s", one->path);
 		if (cmp < 0) {
 			last = next;
 			continue;
Quoted 1 line

passes the test suite, at least. :)

Maybe relevant: 4d6be03b9 (diffcore-rename: avoid processing duplicate destinations, 2015-02-26). That's on the dst side, but possibly we should do something similar on the src side.

BTW, I think the return value from register_rename_src() is questionable. It points to a "struct diff_rename_src" that may be reallocated by further calls to the function. Fortunately nobody actually looks at it, let alone saves it, so there's no bug.

We may want to convert that return value to a void (if not just return an int for "hey, there's a duplicate", like we do for add_rename_dst()).

Also, presumably that function could learn the same "check the last one" trick that the src side does. Which leads me back to "surely we can generalize this". I don't think bsearch() is quite what we want, because its interface doesn't tell us where to put the item when it isn't found. But I think we could make a general bsearch-like function that has similar semantics to index_name_pos(), with its negative-integer return.

Quoted 3 lines

And then that would be a general lookup function, and we could easily build a general "look up and add" function around that. And the "check the last one" optimization would go in the latter.

I do see your point. And I do get tired of littering the code with "check the last before binary searching" tricks. It would be nice if we could better isolate this. I've tried to follow the path of least change/damage -- don't change functionality, but just short-cut when possible, so the patches I've pushed up this month have mostly taken that form.

And you're right, the gains on this particular patch are relatively minor and it is a bit of a contrived case (lots of renames??), but it did come up while testing on the Windows repo. It doesn't happen often, but it did happen.

So I'm OK with rejecting this one.

Perhaps the thing to learn from this (and the other ones) is that we have lots of places where we are building a sorted list by iterating over a sorted list. The insert routines are general purpose and cannot assume this, so they search first. Perhaps it would be clearer to have independent _append and _insert functions and have the caller explicitly call the appropriate one. The mainline iterations on the existing index could just call the _append form and never have to worry about searching or the negative-integer return trick. Whereas, the random iterations (such as on the command's arg list), would always call the _insert form.

Jeff

#12 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

In reply to Johannes Schindelin
Quoted 8 lines · Johannes Schindelin
Quoted 7 lines
Quoted 3 lines

Teach register_rename_src() to see if new file pair can simply be appended to the rename_src[] array before performing the binary search to find the proper insertion point.

I guess your perf results show some minor improvement. But I suspect this is because your synthetic repo does not resemble the real world very much.

Quoted 8 lines

Please note that the synthetic test repo was added *after* coming up with the patch, *after* performance benchmarking on a certain really big repository (it is not hard to guess what use case we are optimizing, right?).

In that light, I would like to register the fact that Jeff's performance work is trying to improve a very real world, that of more than 2,000 developers in our company [*1*].

Sure; I didn't think it came out of thin air. What are the benchmarks on this real-world repository, then?

Specifically, it looks like this optimization isn't really about the number of files in the repository so much as the number of additions/deletions in a particular diff (which is what become rename sources and destinations).

Is it common to add or delete 4 million tiny files and then run "git status"?

Note that I think the optimization probably _is_ worth doing in the general case. These "is it sorted" tradeoffs can backfire if we sometimes get unsorted input, but I don't think that would ever be the case here. My main complaint is not that it's not worth doing, but that I'm not excited about sprinkling these checks ad-hoc throughout the code base.

-Peff

#7 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

In reply to Jeff Hostetler
Quoted 10 lines · Jeff Hostetler

Perhaps the thing to learn from this (and the other ones) is that we have lots of places where we are building a sorted list by iterating over a sorted list. The insert routines are general purpose and cannot assume this, so they search first. Perhaps it would be clearer to have independent _append and _insert functions and have the caller explicitly call the appropriate one. The mainline iterations on the existing index could just call the _append form and never have to worry about searching or the negative-integer return trick. Whereas, the random iterations (such as on the command's arg list), would always call the _insert form.

Yes. I'd be much happier if your patch was flipping between two general-purpose insertion functions. And if that same trick was used on the dst side.

Or even, given that this these functions are called from a single location that has sorted input, the binary search was just replaced completely with an append combined with a sort-check.

That's not the minimal change you were going for, but I think the end result is simpler and more consistent.

E.g., something like this:

diffcore-rename.c +27 -42
index f7444c86b..a5c017198 100644
@@ -43,26 +43,20 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two)
 }
 
 /*
- * Returns 0 on success, -1 if we found a duplicate.
+ * Returns 0 on success, -1 if we found a duplicate or a sorting problem.
  */
 static int add_rename_dst(struct diff_filespec *two)
 {
-	int first = find_rename_dst(two);
-
-	if (first >= 0)
+	if (rename_dst_nr > 0 &&
+	    strcmp(two->path, rename_dst[rename_dst_nr - 1].two->path) <= 0)
 		return -1;
-	first = -first - 1;
 
-	/* insert to make it at "first" */
 	ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
+	rename_dst[rename_dst_nr].two = alloc_filespec(two->path);
+	fill_filespec(rename_dst[rename_dst_nr].two,
+		      two->oid.hash, two->oid_valid, two->mode);
+	rename_dst[rename_dst_nr].pair = NULL;
 	rename_dst_nr++;
-	if (first < rename_dst_nr)
-		memmove(rename_dst + first + 1, rename_dst + first,
-			(rename_dst_nr - first - 1) * sizeof(*rename_dst));
-	rename_dst[first].two = alloc_filespec(two->path);
-	fill_filespec(rename_dst[first].two, two->oid.hash, two->oid_valid,
-		      two->mode);
-	rename_dst[first].pair = NULL;
 	return 0;
 }
 
@@ -73,36 +67,17 @@ static struct diff_rename_src {
 } *rename_src;
 static int rename_src_nr, rename_src_alloc;
 
-static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
+static int register_rename_src(struct diff_filepair *p)
 {
-	int first, last;
-	struct diff_filespec *one = p->one;
-	unsigned short score = p->score;
-
-	first = 0;
-	last = rename_src_nr;
-	while (last > first) {
-		int next = (last + first) >> 1;
-		struct diff_rename_src *src = &(rename_src[next]);
-		int cmp = strcmp(one->path, src->p->one->path);
-		if (!cmp)
-			return src;
-		if (cmp < 0) {
-			last = next;
-			continue;
-		}
-		first = next+1;
-	}
+	if (rename_src_nr > 0 &&
+	    strcmp(p->one->path, rename_src[rename_src_nr - 1].p->one->path) <= 0)
+		return -1;
 
-	/* insert to make it at "first" */
 	ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
+	rename_src[rename_src_nr].p = p;
+	rename_src[rename_src_nr].score = p->score;
 	rename_src_nr++;
-	if (first < rename_src_nr)
-		memmove(rename_src + first + 1, rename_src + first,
-			(rename_src_nr - first - 1) * sizeof(*rename_src));
-	rename_src[first].p = p;
-	rename_src[first].score = score;
-	return &(rename_src[first]);
+	return 0;
 }
 
 static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
@@ -468,7 +443,7 @@ void diffcore_rename(struct diff_options *options)
 				continue;
 			else if (add_rename_dst(p->two) < 0) {
 				warning("skipping rename detection, detected"
-					" duplicate destination '%s'",
+					" duplicate or out-of-order destination '%s'",
 					p->two->path);
 				goto cleanup;
 			}
@@ -486,7 +461,12 @@ void diffcore_rename(struct diff_options *options)
 			 */
 			if (p->broken_pair && !p->score)
 				p->one->rename_used++;
-			register_rename_src(p);
+			if (register_rename_src(p) < 0) {
+				warning("skipping rename detection, detected"
+					" duplicate or out-of-order source '%s'",
+					p->one->path);
+				goto cleanup;
+			}
 		}
 		else if (detect_rename == DIFF_DETECT_COPY) {
 			/*
@@ -494,7 +474,12 @@ void diffcore_rename(struct diff_options *options)
 			 * one, to indicate ourselves as a user.
 			 */
 			p->one->rename_used++;
-			register_rename_src(p);
+			if (register_rename_src(p) < 0) {
+				warning("skipping rename detection, detected"
+					" duplicate or out-of-order source '%s'",
+					p->one->path);
+				goto cleanup;
+			}
 		}
 	}
 	if (rename_dst_nr == 0 || rename_src_nr == 0)
#8 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

Quoted 17 lines · Jeff King
Quoted 10 lines · Jeff Hostetler

Perhaps the thing to learn from this (and the other ones) is that we have lots of places where we are building a sorted list by iterating over a sorted list. The insert routines are general purpose and cannot assume this, so they search first. Perhaps it would be clearer to have independent _append and _insert functions and have the caller explicitly call the appropriate one. The mainline iterations on the existing index could just call the _append form and never have to worry about searching or the negative-integer return trick. Whereas, the random iterations (such as on the command's arg list), would always call the _insert form.

Yes. I'd be much happier if your patch was flipping between two general-purpose insertion functions. And if that same trick was used on the dst side.

Quoted 6 lines

Or even, given that this these functions are called from a single location that has sorted input, the binary search was just replaced completely with an append combined with a sort-check.

That's not the minimal change you were going for, but I think the end result is simpler and more consistent.

OK, let me take a stab at something like that and
see where it takes me.

WRT your earlier comment about how often we add or delete 4M files and then run status. The use case that started this was a 1% sparse-checkout followed by a read-tree (which reset the skip-worktree bits) and then status (which thought 99% of the worktree had been deleted or maybe renamed). There are probably other ways to get into this state, but that's how this started. The more subtle point is that -- for these obscenely large values of n -- any time I see an O(n log n) operation that could or should be O(n), I want to stop and look at it.

#9 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

Quoted 5 lines · Jeff Hostetler
Quoted 2 lines

That's not the minimal change you were going for, but I think the end result is simpler and more consistent.

OK, let me take a stab at something like that and
see where it takes me.

Thanks.

I set the patch as a lump, but I think there are a few things going on there:

- the return value of register_rename_src() is actively dangerous (it points to memory which may be reallocated), so it's good that it goes away in favor of an "int"

- we already refuse to do rename detection when there are duplicate dsts. This adds the same for srcs. I don't know if the same safety rules apply there, but it certainly seems like a reasonable and consistent precaution to say "this tree looks broken, let's skip rename detection". But it does mean a potential change in functionality in that corner case.

- this patch probably adds "unsorted tree" to the list of breakages that would cause us to skip rename detection. I don't know if that's actually possible in practice (i.e., do we end up sorting the diffq elsewhere anyway?). I also wondered if it might run afoul of diffcore_order(), but that is applied after rename detection, so we're OK.

Quoted 6 lines

WRT your earlier comment about how often we add or delete 4M files and then run status. The use case that started this was a 1% sparse-checkout followed by a read-tree (which reset the skip-worktree bits) and then status (which thought 99% of the worktree had been deleted or maybe renamed). There are probably other ways to get into this state, but that's how this started.

Right, that sounds plausible. I guess I just wondered if this is something an average developer runs daily, or something that they would run into once a year. Shaving 4s of CPU off of a once-a-year operation is less exciting.

Quoted 3 lines

The more subtle point is that -- for these obscenely large values of n -- any time I see an O(n log n) operation that could or should be O(n), I want to stop and look at it.

Heh. I spent a fair bit of time in Git's past turning O(n^2) operations into O(n log n), so I feel your pain. I do think it's important to pay attention to whole-operation numbers, though. Quite often you have an O(n log n) with a small constant (like a single strcmp) coupled with something linear but with a huge constant (like loading blob contents), and micro-optimizations to the former get drowned out by the latter.

-Peff

#10 lore raw

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

Quoted 6 lines · Jeff King

- this patch probably adds "unsorted tree" to the list of breakages that would cause us to skip rename detection. I don't know if that's actually possible in practice (i.e., do we end up sorting the diffq elsewhere anyway?). I also wondered if it might run afoul of diffcore_order(), but that is applied after rename detection, so we're OK.

One of the frontends (I think it was diff-index) couldn't generate sorted output (which is input to diffcore-* machinery) but I think diffq is sorted before getting passed to the diffcore-* machinery in that codepath, so we should be also OK on that front.