Skip to contentSkip to conversation

Teach builtin-clone to pack refs

PATCH Superseded Johan Herland 3 participants 13 messages Jun 15, 2016 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Teach builtin-clone to pack refs

The following series builds on top of Barkalow's existing builtin-clone work, available in the "builtin-clone" branch at:
git://iabervon.org/~barkalow/git.git

This patch series teaches builtin-clone to create packed refs. Creating packed refs directly in clone (instead of creating loose refs and have the next "git gc" (re)pack them), makes cloning considerably faster on repos with many refs. In a test repo with 11000 refs (1000 branches, 10000 tags) I get the following numbers (Core 2 Quad, 4GB RAM running Gentoo Linux):

- Current "next": ~24.8 seconds
- Barkalow's "builtin-clone" branch: 1.47 seconds
- "builtin-clone" plus this series: 0.31 seconds

Although most of the speedup from current "next" is achieved by the builtin-clone work, there is still a considerable additional improvement from writing all refs to a single file instead of writing one file per ref. I expect the performance improvement to be much bigger on platforms with slower filesystem (aka. Windows).

A side-effect of this series is that the cloned refs will not get reflog entries. I don't know how important these "clone: from $URL" entries are to people; I, for one, wouldn't miss them at all.

Have fun! :)

...Johan

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net
#1 lore raw

PATCH 1/3 Move pack_refs() and friends into libgit

This moves pack_refs() and underlying functionality into the library, to make pack-refs functionality easily available to all git programs.

Most of builtin-pack-refs.c has been moved verbatim into a new file pack-refs.c that is compiled into libgit.a. A corresponding header file, pack-refs.h, has also been added, declaring pack_refs() and the #defines associated with the flags parameter to pack_refs().

This patch introduces no other changes in functionality.

---
 Makefile            |    5 +-
 builtin-pack-refs.c |  121 +--------------------------------------------------
 pack-refs.c         |  117 +++++++++++++++++++++++++++++++++++++++++++++++++
 pack-refs.h         |   18 ++++++++
 4 files changed, 139 insertions(+), 122 deletions(-)
 create mode 100644 pack-refs.c
 create mode 100644 pack-refs.h
Makefile +3 -2
index 623c639..0804904 100644
@@ -307,7 +307,8 @@ LIB_H = \
 	run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
 	tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
 	utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h \
-	mailmap.h remote.h parse-options.h transport.h diffcore.h hash.h ll-merge.h fsck.h pack-revindex.h
+	mailmap.h remote.h parse-options.h transport.h diffcore.h hash.h \
+	ll-merge.h fsck.h pack-revindex.h pack-refs.h
 
 DIFF_OBJS = \
 	diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -331,7 +332,7 @@ LIB_OBJS = \
 	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
 	convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
 	transport.o bundle.o walker.o parse-options.o ws.o archive.o branch.o \
-	ll-merge.o alias.o fsck.o pack-revindex.o
+	ll-merge.o alias.o fsck.o pack-revindex.o pack-refs.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
builtin-pack-refs.c +1 -120

126 lines not on this page · raw message

pack-refs.c new file +117 -0

117 lines not on this page · raw message

pack-refs.h new file +18 -0
new file mode 100644
index 0000000..518acfb
@@ -0,0 +1,18 @@
+#ifndef PACK_REFS_H
+#define PACK_REFS_H
+
+/*
+ * Flags for controlling behaviour of pack_refs()
+ * PACK_REFS_PRUNE: Prune loose refs after packing
+ * PACK_REFS_ALL:   Pack _all_ refs, not just tags and already packed refs
+ */
+#define PACK_REFS_PRUNE 0x0001
+#define PACK_REFS_ALL   0x0002
+
+/*
+ * Write a packed-refs file for the current repository.
+ * flags: Combination of the above PACK_REFS_* flags.
+ */
+int pack_refs(unsigned int flags);
+
+#endif /* PACK_REFS_H */
Signature · 1 line
1.5.5.rc0.117.ga5237
#2 lore raw

PATCH 2/3 Prepare testsuite for a "git clone" that packs refs

In reply to Johan Herland
---
 t/t5515-fetch-merge-logic.sh |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
t/t5515-fetch-merge-logic.sh +1 -0
index 31c1081..4569a13 100755
@@ -143,6 +143,7 @@ do
 			rm -f .git/refs/heads/*
 			rm -f .git/refs/remotes/rem/*
 			rm -f .git/refs/tags/*
+			rm -f .git/packed-refs
 			git fetch "$@" >/dev/null
 			cat .git/FETCH_HEAD
 		} >"$actual" &&
Signature · 1 line
1.5.5.rc0.117.ga5237
#3 lore raw

Re: [RFC/PATCH 2/3] Prepare testsuite for a "git clone" that packs refs

t/t5515-fetch-merge-logic.sh do · line 149

Quoted 12 lines · Johan Herland
t/t5515-fetch-merge-logic.sh +1 -0
index 31c1081..4569a13 100755
@@ -143,6 +143,7 @@ do
 			rm -f .git/refs/heads/*
 			rm -f .git/refs/remotes/rem/*
 			rm -f .git/refs/tags/*
+			rm -f .git/packed-refs
 			git fetch "$@" >/dev/null
 			cat .git/FETCH_HEAD
 		} >"$actual" &&

I was just looking over this again, and it doesn't actually work, because it removes the copies of refs/remotes/origin/*, which the tests are expecting to find.

-Daniel
*This .sig left intentionally blank*

#4 lore raw

Re: [RFC/PATCH 2/3] Prepare testsuite for a "git clone" that packs refs

Quoted 18 lines · Daniel Barkalow

t/t5515-fetch-merge-logic.sh do · line 149

Quoted 12 lines · Johan Herland
t/t5515-fetch-merge-logic.sh +1 -0
index 31c1081..4569a13 100755
@@ -143,6 +143,7 @@ do
 			rm -f .git/refs/heads/*
 			rm -f .git/refs/remotes/rem/*
 			rm -f .git/refs/tags/*
+			rm -f .git/packed-refs
 			git fetch "$@" >/dev/null
 			cat .git/FETCH_HEAD
 		} >"$actual" &&

I was just looking over this again, and it doesn't actually work, because it removes the copies of refs/remotes/origin/*, which the tests are expecting to find.

Yes, I also found that (after rebasing on current 'next'). :)

Will shortly post a new series making builtin-clone do packed refs, suitable for current 'next'.

Have fun! :)

...Johan

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net
#5 lore raw

PATCH 1/4 Incorporate fetched packs in future object traversal

Immediately after fetching a pack, we should call reprepare_packed_git() to make sure the objects in the pack are reachable. Otherwise, we will fail to look up objects that are present only in the fetched pack.

---
 builtin-fetch-pack.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
builtin-fetch-pack.c +1 -0
index 65350ca..309ae03 100644
@@ -820,5 +820,6 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
 		}
 	}
 
+	reprepare_packed_git();
 	return ref_cpy;
 }
Signature · 1 line
1.5.5.159.g8c84b
#6 lore raw

PATCH 2/4 Move pack_refs() and friends into libgit

In reply to Johan Herland

This moves pack_refs() and underlying functionality into the library, to make pack-refs functionality easily available to all git programs.

Most of builtin-pack-refs.c has been moved verbatim into a new file pack-refs.c that is compiled into libgit.a. A corresponding header file, pack-refs.h, has also been added, declaring pack_refs() and the #defines associated with the flags parameter to pack_refs().

This patch introduces no other changes in functionality.

---
 Makefile            |    2 +
 builtin-pack-refs.c |  121 +--------------------------------------------------
 pack-refs.c         |  117 +++++++++++++++++++++++++++++++++++++++++++++++++
 pack-refs.h         |   18 ++++++++
 4 files changed, 138 insertions(+), 120 deletions(-)
 create mode 100644 pack-refs.c
 create mode 100644 pack-refs.h
Makefile +2 -0
index 854527d..6a1c23c 100644
@@ -353,6 +353,7 @@ LIB_H += log-tree.h
 LIB_H += mailmap.h
 LIB_H += object.h
 LIB_H += pack.h
+LIB_H += pack-refs.h
 LIB_H += pack-revindex.h
 LIB_H += parse-options.h
 LIB_H += patch-ids.h
@@ -425,6 +426,7 @@ LIB_OBJS += merge-file.o
 LIB_OBJS += name-hash.o
 LIB_OBJS += object.o
 LIB_OBJS += pack-check.o
+LIB_OBJS += pack-refs.o
 LIB_OBJS += pack-revindex.o
 LIB_OBJS += pack-write.o
 LIB_OBJS += pager.o
builtin-pack-refs.c +1 -120

126 lines not on this page · raw message

pack-refs.c new file +117 -0

117 lines not on this page · raw message

pack-refs.h new file +18 -0
new file mode 100644
index 0000000..518acfb
@@ -0,0 +1,18 @@
+#ifndef PACK_REFS_H
+#define PACK_REFS_H
+
+/*
+ * Flags for controlling behaviour of pack_refs()
+ * PACK_REFS_PRUNE: Prune loose refs after packing
+ * PACK_REFS_ALL:   Pack _all_ refs, not just tags and already packed refs
+ */
+#define PACK_REFS_PRUNE 0x0001
+#define PACK_REFS_ALL   0x0002
+
+/*
+ * Write a packed-refs file for the current repository.
+ * flags: Combination of the above PACK_REFS_* flags.
+ */
+int pack_refs(unsigned int flags);
+
+#endif /* PACK_REFS_H */
Signature · 1 line
1.5.5.159.g8c84b
#7 lore raw

PATCH 3/4 Prepare testsuite for a "git clone" that packs refs

In reply to Johan Herland

t5515-fetch-merge-logic removes many, but not all, refs between each test. This is done by removing the corresponding refs/foo/* files in the .git/refs hierarchy. However, once "git clone" starts producing packed refs, these refs will no longer be in the .git/refs hierarchy, but rather listed in .git/packed-refs. This patch therefore teaches t5515-fetch-merge-logic to also remove the refs in question from the packed-refs file.

---
 t/t5515-fetch-merge-logic.sh |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
t/t5515-fetch-merge-logic.sh +19 -0
index 65c3774..8a8c35c 100755
@@ -145,6 +145,25 @@ do
 			rm -f .git/refs/heads/*
 			rm -f .git/refs/remotes/rem/*
 			rm -f .git/refs/tags/*
+			cat .git/packed-refs | \
+			while read sha1 refname
+			do
+				case "$sha1" in
+				^*) # remove peeled tags
+					;;
+				*)
+					case "$refname" in
+					refs/heads/*|\
+					refs/remotes/rem/*|\
+					refs/tags/*) # remove same as above
+						;;
+					*) # keep everything else
+						echo "$sha1 $refname"
+						;;
+					esac
+				esac
+			done > .git/packed-refs.new
+			mv .git/packed-refs.new .git/packed-refs
 			git fetch "$@" >/dev/null
 			cat .git/FETCH_HEAD
 		} >"$actual_f" &&
Signature · 1 line
1.5.5.159.g8c84b
#8 lore raw

PATCH 4/4 Teach "git clone" to pack refs

In reply to Johan Herland

In repos with many refs, it is unlikely that most refs will ever change. This fact is already exploited by "git gc" by executing "git pack-refs" to consolidate all refs into a single file.

When cloning a repo with many refs, it does not make sense to create the loose refs in the first place, just to have the next "git gc" consolidate them into one file. Instead, make "git clone" create the packed refs file immediately, and forego the loose refs completely.

---
 builtin-clone.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
builtin-clone.c +6 -2
index 0a9c873..01c595e 100644
@@ -18,6 +18,7 @@
 #include "transport.h"
 #include "strbuf.h"
 #include "dir.h"
+#include "pack-refs.h"
 
 /*
  * Overall FIXMEs:
@@ -313,8 +314,11 @@ static struct ref *write_remote_refs(const struct ref *refs, struct refspec *ref
 	get_fetch_map(refs, tag_refspec, &tail, 0);
 
 	for (r = local_refs; r; r = r->next)
-		update_ref(reflog,
-			   r->peer_ref->name, r->old_sha1, NULL, 0, DIE_ON_ERR);
+		add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
+
+	pack_refs(PACK_REFS_ALL);
+	clear_extra_refs();
+
 	return local_refs;
 }
 
Signature · 1 line
1.5.5.159.g8c84b
#9 lore raw

PATCH 3/3 Teach "git clone" to pack refs

In reply to Johan Herland

In repos with many refs, it is unlikely that most refs will ever change. This fact is already exploited by "git gc" by executing "git pack-refs" to consolidate all refs into a single file.

When cloning a repo with many refs, it does not make sense to create the loose refs in the first place, just to have the next "git gc" consolidate them into one file. Instead, make "git clone" create the packed refs file immediately, and forego the loose refs completely.

---
 builtin-clone.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
builtin-clone.c +6 -2
index 0a9c873..01c595e 100644
@@ -18,6 +18,7 @@
 #include "transport.h"
 #include "strbuf.h"
 #include "dir.h"
+#include "pack-refs.h"
 
 /*
  * Overall FIXMEs:
@@ -313,8 +314,11 @@ static struct ref *write_remote_refs(const struct ref *refs, struct refspec *ref
 	get_fetch_map(refs, tag_refspec, &tail, 0);
 
 	for (r = local_refs; r; r = r->next)
-		update_ref(reflog,
-			   r->peer_ref->name, r->old_sha1, NULL, 0, DIE_ON_ERR);
+		add_extra_ref(r->peer_ref->name, r->old_sha1, 0);
+
+	pack_refs(PACK_REFS_ALL);
+	clear_extra_refs();
+
 	return local_refs;
 }
 
Signature · 1 line
1.5.5.rc0.117.ga5237
Acked-by on v1 3/3
#10 lore raw

Re: [PATCH 3/3] Teach "git clone" to pack refs

Quoted 8 lines · Johan Herland

In repos with many refs, it is unlikely that most refs will ever change. This fact is already exploited by "git gc" by executing "git pack-refs" to consolidate all refs into a single file.

When cloning a repo with many refs, it does not make sense to create the loose refs in the first place, just to have the next "git gc" consolidate them into one file. Instead, make "git clone" create the packed refs file immediately, and forego the loose refs completely.

Quoted 18 lines
---
 builtin-clone.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
builtin-clone.c +1 -0
index 0a9c873..01c595e 100644
@@ -18,6 +18,7 @@
 #include "transport.h"
 #include "strbuf.h"
 #include "dir.h"
+#include "pack-refs.h"
 
 /*
  * Overall FIXMEs:

builtin-clone.c static struct ref *write_remote_refs(const struct ref *refs, struct refspec *ref · line 317

Quoted 6 lines
builtin-clone.c +1 -2
@@ -313,8 +314,11 @@ static struct ref *write_remote_refs(const struct ref *refs, struct refspec *ref
 	get_fetch_map(refs, tag_refspec, &tail, 0);
 
 	for (r = local_refs; r; r = r->next)
-		update_ref(reflog,
-			   r->peer_ref->name, r->old_sha1, NULL, 0, DIE_ON_ERR);
+		add_extra_ref(r->peer_ref->name, r->old_sha1, 0);

Wow, clever. Yes, this should work perfectly.

Quoted 11 lines
builtin-clone.c +4 -0
+
+	pack_refs(PACK_REFS_ALL);
+	clear_extra_refs();
+
 	return local_refs;
 }
 
Signature · 1 line
1.5.5.rc0.117.ga5237
#11 lore raw
In reply to Johan Herland
Quoted 5 lines · Johan Herland

Although most of the speedup from current "next" is achieved by the builtin-clone work, there is still a considerable additional improvement from writing all refs to a single file instead of writing one file per ref. I expect the performance improvement to be much bigger on platforms with slower filesystem (aka. Windows).

At some point, additional speedups are hidden in the noise.

Not writing reflogs is a _different_ behaviour from the previous, but I suspect it might even be an improvement. When you have 1000 remote branches, probably most of them are not even active.

#12 lore raw
Quoted 9 lines · Junio C Hamano
Quoted 5 lines · Johan Herland

Although most of the speedup from current "next" is achieved by the builtin-clone work, there is still a considerable additional improvement from writing all refs to a single file instead of writing one file per ref. I expect the performance improvement to be much bigger on platforms with slower filesystem (aka. Windows).

At some point, additional speedups are hidden in the noise.

Yes, however, I bet you'll notice the difference between writing 11000 files on Windows, and writing 1.

Unfortunately, this scenario is not as far fetched as some may think: At $dayjob I'm working on converting old CVS modules with up to ~10 years of history, and some of the resulting repos have ~30000 refs (mostly build tags). Although I'll probably remove the build tags before preparing the repos that most other developers will see, I'll still have to keep the build tags around somewhere, in case devs need to refer to them. And, of course, most devs are still Windows-users. Keeping refs packed is pretty much crucial in this scenario.

Quoted 3 lines

Not writing reflogs is a _different_ behaviour from the previous, but I suspect it might even be an improvement. When you have 1000 remote branches, probably most of them are not even active.

Exactly my thinking as well. And for those few that _are_ active, you'll still of course get regular reflog entries when they _do_ change.

...Johan

Signature · 2 lines
Johan Herland, <[email protected]>
www.herland.net