Skip to contentSkip to conversation

suboptimal behavior of fast-import in some cases with "from"

PATCH In review Mike Hommey 2 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hi,

I did something "stupid" with a script using fast-import, and that made the whole process ~20% slower on Linux and 400~500% slower on Mac. The reason this happened is that the script was essentially adding a "from" to every "commit" command, even when the "from" commit is the current head of the branch.

One of the first things parse_from does is unconditionally throw away the tree for the given branch, and then the "from" tree is loaded. So when the "from" commit is the current head of the branch, that make fast-import do more work than necessary.

Even more so when the pack flush code in gfi_unpack_entry is triggered, which, on mac, is extra slow (and explains the huge slow down there).

Now, I do understand that my script is doing something stupid. So the question is whether it's worth fixing in fast-import or not. I already fixed my script anyways.

Mike

#1 lore raw
Quoted 4 lines · Mike Hommey

One of the first things parse_from does is unconditionally throw away the tree for the given branch, and then the "from" tree is loaded. So when the "from" commit is the current head of the branch, that make fast-import do more work than necessary.

If it is very common that the next commit the input stream wants to create is often on top of the commit that was just created, and if it is very common that the input stream producer knows what the commit object name of the commit that was just created, then optimising for that case does not sound too bad. It really depends on two factors:

- How likely is it that other people make the same mistake?

- How bad the damage to parse_from() would be if we wanted to optimize for this case?

#2 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Mike Hommey

One of the first things parse_from does is unconditionally throw away the tree for the given branch, and then the "from" tree is loaded. So when the "from" commit is the current head of the branch, that make fast-import do more work than necessary.

Quoted 8 lines

If it is very common that the next commit the input stream wants to create is often on top of the commit that was just created, and if it is very common that the input stream producer knows what the commit object name of the commit that was just created, then optimising for that case does not sound too bad. It really depends on two factors:

- How likely is it that other people make the same mistake?

Looks like the answer is: very. Assuming my quick glance at the code is not mistaken, the same mistake is made in at least git-p4.py (yes, the one that comes with git), felipec's git-remote-hg, and hg-fast-export, and that's 100% of the sample I looked at.

I won't claim to know what fast-import is doing, not having looked at more than the parse_from* functions and the commit message for 4cabf858, but it seems plausible this also skips making tree deltas for those trees.

Quoted 2 lines

- How bad the damage to parse_from() would be if we wanted to optimize for this case?

I /think/ it would look like this (untested), which doesn't seem too damaging:

fast-import.c +7 -4
index e78ca10..cb232e0 100644
@@ -2588,14 +2588,12 @@ static int parse_from(struct branch *b)
 {
        const char *from;
        struct branch *s;
+       unsigned char sha1[20];
 
        if (!skip_prefix(command_buf.buf, "from ", &from))
                return 0;
 
-       if (b->branch_tree.tree) {
-               release_tree_content_recursive(b->branch_tree.tree);
-               b->branch_tree.tree = NULL;
-       }
+       hashcpy(sha1, b->branch_tree.versions[1].sha1);
 
        s = lookup_branch(from);
        if (b == s)
@@ -2626,6 +2624,11 @@ static int parse_from(struct branch *b)
        else
                die("Invalid ref name or SHA1 expression: %s", from);
 
+       if (b->branch_tree.tree && hashcmp(sha1, b->branch_tree.versions[1].sha1)) {
+               release_tree_content_recursive(b->branch_tree.tree);
+               b->branch_tree.tree = NULL;
+       }
+
        read_next_command();
        return 1;
 }

Mike

#3 lore raw
Quoted 22 lines · Mike Hommey
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Mike Hommey

One of the first things parse_from does is unconditionally throw away the tree for the given branch, and then the "from" tree is loaded. So when the "from" commit is the current head of the branch, that make fast-import do more work than necessary.

Quoted 8 lines

If it is very common that the next commit the input stream wants to create is often on top of the commit that was just created, and if it is very common that the input stream producer knows what the commit object name of the commit that was just created, then optimising for that case does not sound too bad. It really depends on two factors:

- How likely is it that other people make the same mistake?

Looks like the answer is: very. Assuming my quick glance at the code is not mistaken, the same mistake is made in at least git-p4.py (yes, the one that comes with git), felipec's git-remote-hg, and hg-fast-export, and that's 100% of the sample I looked at.

Quoted 4 lines

I won't claim to know what fast-import is doing, not having looked at more than the parse_from* functions and the commit message for 4cabf858, but it seems plausible this also skips making tree deltas for those trees.

It doesn't /seem/ to be the case.

Quoted 5 lines
Quoted 2 lines

- How bad the damage to parse_from() would be if we wanted to optimize for this case?

I /think/ it would look like this (untested), which doesn't seem too damaging:

It's actually not enough. It does avoid to reread trees, but it doesn't avoid the pack to be closed/reopened.

Mike

#4 lore raw

PATCH 1/1 fast-import: Do less work when given "from" matches current branch head

When building a fast-import stream, it's easy to forget the fact that for non-merge commits happening on top of the current branch head, there is no need for a "from" command. That is corroborated by the fact that at least git-p4, hg-fast-export and felipec's git-remote-hg all unconditionally use a "from" command.

Unfortunately, giving a "from" command always resets the branch tree, forcing it to be re-read, and in many cases, the pack is also closed and reopened through gfi_unpack_entry. Both are extra unwanted overhead, and the latter is particularly slow at least on OSX.

So, avoid resetting the tree when it's unmodified, and avoid calling gfi_unpack_entry when the given mark points to the same commit as the current branch head.

---
 fast-import.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
fast-import.c +17 -12
index e78ca10..372d63a 100644
@@ -2588,14 +2588,12 @@ static int parse_from(struct branch *b)
 {
 	const char *from;
 	struct branch *s;
+	unsigned char sha1[20];
 
 	if (!skip_prefix(command_buf.buf, "from ", &from))
 		return 0;
 
-	if (b->branch_tree.tree) {
-		release_tree_content_recursive(b->branch_tree.tree);
-		b->branch_tree.tree = NULL;
-	}
+	hashcpy(sha1, b->branch_tree.versions[1].sha1);
 
 	s = lookup_branch(from);
 	if (b == s)
@@ -2610,14 +2608,16 @@ static int parse_from(struct branch *b)
 		struct object_entry *oe = find_mark(idnum);
 		if (oe->type != OBJ_COMMIT)
 			die("Mark :%" PRIuMAX " not a commit", idnum);
-		hashcpy(b->sha1, oe->idx.sha1);
-		if (oe->pack_id != MAX_PACK_ID) {
-			unsigned long size;
-			char *buf = gfi_unpack_entry(oe, &size);
-			parse_from_commit(b, buf, size);
-			free(buf);
-		} else
-			parse_from_existing(b);
+		if (hashcmp(b->sha1, oe->idx.sha1)) {
+			hashcpy(b->sha1, oe->idx.sha1);
+			if (oe->pack_id != MAX_PACK_ID) {
+				unsigned long size;
+				char *buf = gfi_unpack_entry(oe, &size);
+				parse_from_commit(b, buf, size);
+				free(buf);
+			} else
+				parse_from_existing(b);
+		}
 	} else if (!get_sha1(from, b->sha1)) {
 		parse_from_existing(b);
 		if (is_null_sha1(b->sha1))
@@ -2626,6 +2626,11 @@ static int parse_from(struct branch *b)
 	else
 		die("Invalid ref name or SHA1 expression: %s", from);
 
+	if (b->branch_tree.tree && hashcmp(sha1, b->branch_tree.versions[1].sha1)) {
+		release_tree_content_recursive(b->branch_tree.tree);
+		b->branch_tree.tree = NULL;
+	}
+
 	read_next_command();
 	return 1;
 }
Signature · 1 line
2.4.3.2.gf0a024e.dirty
#5 lore raw

Re: [PATCH] fast-import: Do less work when given "from" matches current branch head

Mike Hommey <[email protected]> writes:

Cc'ed a few people who appear at the top of "shortlog --no-merges"; I think the end result is not incorrect, but I want to hear second opinions on this one. I do not know Shawn still remembers this code, but what is under discussion seems to have come mostly from ea5e370a (fast-import: Support reusing 'from' and brown paper bag fix reset., 2007-02-12).

fast-import.c static int parse_from(struct branch *b) · line 2599

Quoted 11 lines
fast-import.c +1 -4
 	if (!skip_prefix(command_buf.buf, "from ", &from))
 		return 0;
 
-	if (b->branch_tree.tree) {
-		release_tree_content_recursive(b->branch_tree.tree);
-		b->branch_tree.tree = NULL;
-	}
+	hashcpy(sha1, b->branch_tree.versions[1].sha1);
 
 	s = lookup_branch(from);
 	if (b == s)

The part that deals with a branch that is different from the current one is not visible in the context (i.e. when s = lookup_branch(from) returned a non-NULL result that is different from b) but it used to, and continues to with this patch, copy sha1 from branch_tree.sha1 and branch_tree.versions[] from sha1 and branch_tree.versions[1] of the specified branch.

That codepath used to release the contents of branch_tree.tree when it did so, but it no longer does so after this patch because of the removal we see above.

Does that mean the original code was doing a release that was unnecessary? Or does it mean this patch changes what happens on that codepath, namely (1) leaking resource, and/or (2) keeping a tree of the original 'b' that does not have anything to do with the tree of 's', preventing the later lazy-load code from reading the tree of 's' and instead of building on top of a wrong tree content?

... me goes and reads on ...

Quoted 15 lines
fast-import.c +3 -8
@@ -2610,14 +2608,16 @@ static int parse_from(struct branch *b)
 		struct object_entry *oe = find_mark(idnum);
 		if (oe->type != OBJ_COMMIT)
 			die("Mark :%" PRIuMAX " not a commit", idnum);
-		hashcpy(b->sha1, oe->idx.sha1);
-		if (oe->pack_id != MAX_PACK_ID) {
-			unsigned long size;
-			char *buf = gfi_unpack_entry(oe, &size);
-			parse_from_commit(b, buf, size);
-			free(buf);
-		} else
-			parse_from_existing(b);
+		if (hashcmp(b->sha1, oe->idx.sha1)) {
+			hashcpy(b->sha1, oe->idx.sha1);
+			if (oe->pack_id != MAX_PACK_ID) {

fast-import.c static int parse_from(struct branch *b) · line 2623

Quoted 10 lines
fast-import.c +7 -0
+				unsigned long size;
+				char *buf = gfi_unpack_entry(oe, &size);
+				parse_from_commit(b, buf, size);
+				free(buf);
+			} else
+				parse_from_existing(b);
+		}
 	} else if (!get_sha1(from, b->sha1)) {
 		parse_from_existing(b);
 		if (is_null_sha1(b->sha1))

This part is straight-forward.

fast-import.c static int parse_from(struct branch *b) · line 2633

Quoted 9 lines
fast-import.c +5 -0
@@ -2626,6 +2626,11 @@ static int parse_from(struct branch *b)
 	else
 		die("Invalid ref name or SHA1 expression: %s", from);
 
+	if (b->branch_tree.tree && hashcmp(sha1, b->branch_tree.versions[1].sha1)) {
+		release_tree_content_recursive(b->branch_tree.tree);
+		b->branch_tree.tree = NULL;
+	}
+

This looks like an attempt to compensate for that "what happens if (s != NULL && s != b)?" issue, and also for the surviving codepaths.

As both parse_from_commit() and parse_from_existing() only touch branch_tree.versions[] and they do not seem to get affected if b->branch_tree.tree holds a stale and unrelated content, this looks OK to me from a cursory reading, but it does make me feel dirty that it has to put *b temporarily into an inconsistent state.

#6 lore raw

Re: [PATCH] fast-import: Do less work when given "from" matches current branch head

Quoted 32 lines · Junio C Hamano

Mike Hommey <[email protected]> writes:

Cc'ed a few people who appear at the top of "shortlog --no-merges"; I think the end result is not incorrect, but I want to hear second opinions on this one. I do not know Shawn still remembers this code, but what is under discussion seems to have come mostly from ea5e370a (fast-import: Support reusing 'from' and brown paper bag fix reset., 2007-02-12).

fast-import.c static int parse_from(struct branch *b) · line 2599

Quoted 11 lines
fast-import.c +1 -4
 	if (!skip_prefix(command_buf.buf, "from ", &from))
 		return 0;
 
-	if (b->branch_tree.tree) {
-		release_tree_content_recursive(b->branch_tree.tree);
-		b->branch_tree.tree = NULL;
-	}
+	hashcpy(sha1, b->branch_tree.versions[1].sha1);
 
 	s = lookup_branch(from);
 	if (b == s)

The part that deals with a branch that is different from the current one is not visible in the context (i.e. when s = lookup_branch(from) returned a non-NULL result that is different from b) but it used to, and continues to with this patch, copy sha1 from branch_tree.sha1 and branch_tree.versions[] from sha1 and branch_tree.versions[1] of the specified branch.

That codepath used to release the contents of branch_tree.tree when it did so, but it no longer does so after this patch because of the removal we see above.

Quoted 6 lines

Does that mean the original code was doing a release that was unnecessary? Or does it mean this patch changes what happens on that codepath, namely (1) leaking resource, and/or (2) keeping a tree of the original 'b' that does not have anything to do with the tree of 's', preventing the later lazy-load code from reading the tree of 's' and instead of building on top of a wrong tree content?

I guess the question is whether branch_tree.tree can be in a state that doesn't match that of branch_tree.versions[1].sha1. If not, then if s and b have the same branch_tree.versions[1].sha1 for some reason, then keeping the old branch_tree.tree makes no practical difference from resetting it. Except it skips the busy-work.

Mike

#7 lore raw

Re: [PATCH] fast-import: Do less work when given "from" matches current branch head

Quoted 7 lines · Mike Hommey
Quoted 6 lines

Does that mean the original code was doing a release that was unnecessary? Or does it mean this patch changes what happens on that codepath, namely (1) leaking resource, and/or (2) keeping a tree of the original 'b' that does not have anything to do with the tree of 's', preventing the later lazy-load code from reading the tree of 's' and instead of building on top of a wrong tree content?

Quoted 5 lines

I guess the question is whether branch_tree.tree can be in a state that doesn't match that of branch_tree.versions[1].sha1. If not, then if s and b have the same branch_tree.versions[1].sha1 for some reason, then keeping the old branch_tree.tree makes no practical difference from resetting it. Except it skips the busy-work.

Perhaps my comment was misleading. I _think_ the state at the end of this function (i.e. the latter hunk you added to the function makes the above issue I raised go away). It just made me feel uneasy to leave branch_tree.tree and branch_tree.versions[] in an inconsistent state inside this function, while it calls a few helper functions (hence my comment on the fact that they do not seem to be affected by this inconsistency).

#8 lore raw

Re: [PATCH] fast-import: Do less work when given "from" matches current branch head

Quoted 15 lines · Junio C Hamano
Quoted 7 lines · Mike Hommey
Quoted 6 lines

Does that mean the original code was doing a release that was unnecessary? Or does it mean this patch changes what happens on that codepath, namely (1) leaking resource, and/or (2) keeping a tree of the original 'b' that does not have anything to do with the tree of 's', preventing the later lazy-load code from reading the tree of 's' and instead of building on top of a wrong tree content?

Quoted 5 lines

I guess the question is whether branch_tree.tree can be in a state that doesn't match that of branch_tree.versions[1].sha1. If not, then if s and b have the same branch_tree.versions[1].sha1 for some reason, then keeping the old branch_tree.tree makes no practical difference from resetting it. Except it skips the busy-work.

Quoted 3 lines

Perhaps my comment was misleading. I _think_ the state at the end of this function (i.e. the latter hunk you added to the function makes the above issue I raised go away). It just made me feel

s/this function (/this function is good (/;

Quoted 4 lines

uneasy to leave branch_tree.tree and branch_tree.versions[] in an inconsistent state inside this function, while it calls a few helper functions (hence my comment on the fact that they do not seem to be affected by this inconsistency).