Skip to contentSkip to conversation

unpack-trees: fix accidentally quadratic behavior

PATCH In review David Turner 2 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unpack-trees: fix accidentally quadratic behavior

While unpacking trees (e.g. during git checkout), when we hit a cache entry that's past and outside our path, we cut off iteration.

This provides about a 45% speedup on git checkout between master and master^20000 on Twitter's monorepo. Speedup in general will depend on repostitory structure, number of changes, and packfile packing decisions.

---
 unpack-trees.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
unpack-trees.c +18 -1
index 5f541c2..b18a611 100644
@@ -695,8 +695,25 @@ static int find_cache_pos(struct traverse_info *info,
 				++o->cache_bottom;
 			continue;
 		}
-		if (!ce_in_traverse_path(ce, info))
+		if (!ce_in_traverse_path(ce, info)) {
+			/*
+			 * Check if we can skip future cache checks
+			 * (because we're already past all possible
+			 * entries in the traverse path).
+			 */
+			if (info->prev && info->traverse_path) {
+				int prefix_cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
+				if (prefix_cmp > 0)
+					break;
+				else if (prefix_cmp == 0 &&
+					 ce_namelen(ce) >= info->pathlen &&
+					 strcmp(ce->name + info->pathlen,
+						 info->name.path) > 0) {
+					break;
+				}
+			}
 			continue;
+		}
 		ce_name = ce->name + pfxlen;
 		ce_slash = strchr(ce_name, '/');
 		if (ce_slash)
Signature · 1 line
2.4.2.749.g730654d-twtrsrc
#1 lore raw
Quoted 10 lines · David Turner

While unpacking trees (e.g. during git checkout), when we hit a cache entry that's past and outside our path, we cut off iteration.

This provides about a 45% speedup on git checkout between master and master^20000 on Twitter's monorepo. Speedup in general will depend on repostitory structure, number of changes, and packfile packing decisions.

---

I haven't thought things through, but does this get fooled by the somewhat strange ordering rules of tree entries (i.e. a subtree sorts as if its name is suffixed with a '/' in a tree object)?

Other than that, I like this. "We know the list is sorted, and after seeing this entry we know there is nothing that will match" is an obvious optimization that we already use elsewhere.

Thanks.

Quoted 34 lines
 unpack-trees.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
unpack-trees.c +18 -1
index 5f541c2..b18a611 100644
@@ -695,8 +695,25 @@ static int find_cache_pos(struct traverse_info *info,
 				++o->cache_bottom;
 			continue;
 		}
-		if (!ce_in_traverse_path(ce, info))
+		if (!ce_in_traverse_path(ce, info)) {
+			/*
+			 * Check if we can skip future cache checks
+			 * (because we're already past all possible
+			 * entries in the traverse path).
+			 */
+			if (info->prev && info->traverse_path) {
+				int prefix_cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
+				if (prefix_cmp > 0)
+					break;
+				else if (prefix_cmp == 0 &&
+					 ce_namelen(ce) >= info->pathlen &&
+					 strcmp(ce->name + info->pathlen,
+						 info->name.path) > 0) {
+					break;
+				}
+			}
 			continue;
+		}
 		ce_name = ce->name + pfxlen;
 		ce_slash = strchr(ce_name, '/');
 		if (ce_slash)
#2 lore raw
Quoted 20 lines · Junio C Hamano
Quoted 11 lines · David Turner

While unpacking trees (e.g. during git checkout), when we hit a cache
entry that's past and outside our path, we cut off iteration.

This provides about a 45% speedup on git checkout between master and
master^20000 on Twitter's monorepo. Speedup in general will depend on
repostitory structure, number of changes, and packfile packing decisions.

Quoted 2 lines

---

I haven't thought things through, but does this get fooled by the somewhat strange ordering rules of tree entries (i.e. a subtree sorts as if its name is suffixed with a '/' in a tree object)?

Quoted 5 lines

Other than that, I like this. "We know the list is sorted, and after seeing this entry we know there is nothing that will match" is an obvious optimization that we already use elsewhere.

Thanks.

I think this is correct, because we first do the more complicated check (ce_in_traverse_path), and only check the ordering once that has failed. The tests all pass, so this should be good.

#3 lore raw
Quoted 25 lines · David Turner
Quoted 24 lines · Junio C Hamano
Quoted 11 lines · David Turner

While unpacking trees (e.g. during git checkout), when we hit a cache
entry that's past and outside our path, we cut off iteration.

This provides about a 45% speedup on git checkout between master and
master^20000 on Twitter's monorepo. Speedup in general will depend on
repostitory structure, number of changes, and packfile packing decisions.

Quoted 2 lines

---

I haven't thought things through, but does this get fooled by the somewhat strange ordering rules of tree entries (i.e. a subtree sorts as if its name is suffixed with a '/' in a tree object)?

Other than that, I like this. "We know the list is sorted, and after seeing this entry we know there is nothing that will match" is an obvious optimization that we already use elsewhere.

Quoted 5 lines
Quoted 1 line

Thanks.

I think this is correct, because we first do the more complicated check (ce_in_traverse_path), and only check the ordering once that has failed.

But the patch does this:

unpack-trees.c static int find_cache_pos(struct traverse_info *info, · line 715

Quoted 12 lines
unpack-trees.c +11 -0
+			if (info->prev && info->traverse_path) {
+				int prefix_cmp = strncmp(ce->name, info->traverse_path, info->pathlen);
+				if (prefix_cmp > 0)
+					break;
+				else if (prefix_cmp == 0 &&
+					 ce_namelen(ce) >= info->pathlen &&
+					 strcmp(ce->name + info->pathlen,
+						 info->name.path) > 0) {
+					break;
+				}
+			}
 			continue;

The first break is correct, but I am not sure about the "else if" part. Shouldn't it be doing something similar to the logic to "keep looking" that talks about "t-i", "t" and "t/a" at the end of the loop?

Quoted 1 line

The tests all pass, so this should be good.

Please don't ever say that again. The existing tests do not cover all corner cases, and they fundamentally cannot cover the corner cases future changes may introduce.

Saying "Even test X breaks, so it is clearly broken. Why did you send it out without testing?" is OK, though.

#4 lore raw
Quoted 51 lines · Junio C Hamano
Quoted 28 lines · David Turner
Quoted 27 lines · Junio C Hamano
Quoted 13 lines · David Turner

While unpacking trees (e.g. during git checkout), when we hit a cache
entry that's past and outside our path, we cut off iteration.

This provides about a 45% speedup on git checkout between master
and
master^20000 on Twitter's monorepo. Speedup in general will depend
on
repostitory structure, number of changes, and packfile packing decisions.

Quoted 2 lines

---

I haven't thought things through, but does this get fooled by the somewhat strange ordering rules of tree entries (i.e. a subtree sorts as if its name is suffixed with a '/' in a tree object)?

Other than that, I like this. "We know the list is sorted, and after seeing this entry we know there is nothing that will match" is
an obvious optimization that we already use elsewhere.

Quoted 6 lines
Quoted 1 line

Thanks.

I think this is correct, because we first do the more complicated check
(ce_in_traverse_path), and only check the ordering once that has failed.

But the patch does this:

Quoted 12 lines
(quoted diff) +2 -0
+			if (info->prev && info->traverse_path) {
+				int prefix_cmp = strncmp(ce->name,

info->traverse_path, info->pathlen);

(quoted diff) +6 -2
+				if (prefix_cmp > 0)
+					break;
+				else if (prefix_cmp == 0 &&
+					 ce_namelen(ce) >= info
->pathlen &&
+					 strcmp(ce->name + info
->pathlen,
+						 info->name.path)
Quoted 1 line

0) {

Quoted 9 lines

unpack-trees.c static int find_cache_pos(struct traverse_info *info, · line 715

Quoted 4 lines
unpack-trees.c +3 -0
+					break;
+				}
+			}
 			continue;

The first break is correct, but I am not sure about the "else if" part. Shouldn't it be doing something similar to the logic to "keep looking" that talks about "t-i", "t" and "t/a" at the end of the loop?

Rather than doing more complicated logic, let's just do the first check; it seems about as fast for our repo, and I think will usually be so. does that seem reasonable to you?

Quoted 3 lines
Quoted 1 line

The tests all pass, so this should be good.

Please don't ever say that again.

OK.

#5 lore raw
Quoted 8 lines · David Turner
Quoted 4 lines

The first break is correct, but I am not sure about the "else if" part. Shouldn't it be doing something similar to the logic to "keep looking" that talks about "t-i", "t" and "t/a" at the end of the loop?

Rather than doing more complicated logic, let's just do the first check; it seems about as fast for our repo, and I think will usually be so. does that seem reasonable to you?

Very reasonable. I was mostly afraid of leaving the loop prematurely and giving a wrong answer to the caller.