Skip to content

Commit 1385bb7

Browse files
peffgitster
authored andcommitted
reachable: only mark local objects as recent
When pruning and repacking a repository that has an alternate object store configured, we may traverse a large number of objects in the alternate. This serves no purpose, and may be expensive to do. A longer explanation is below. Commits d3038d2 and abcb865 taught prune and pack-objects (respectively) to treat "recent" objects as tips for reachability, so that we keep whole chunks of history. They built on the object traversal in 660c889 (sha1_file: add for_each iterators for loose and packed objects, 2014-10-15), which covers both local and alternate objects. In both cases, covering alternate objects is unnecessary, as both commands can only drop objects from the local repository. In the case of prune, we traverse only the local object directory. And in the case of repacking, while we may or may not include local objects in our pack, we will never reach into the alternate with "repack -d". The "-l" option is only a question of whether we are migrating objects from the alternate into our repository, or leaving them untouched. It is possible that we may drop an object that is depended upon by another object in the alternate. For example, imagine two repositories, A and B, with A pointing to B as an alternate. Now imagine a commit that is in B which references a tree that is only in A. Traversing from recent objects in B might prevent A from dropping that tree. But this case isn't worth covering. Repo B should take responsibility for its own objects. It would never have had the commit in the first place if it did not also have the tree, and assuming it is using the same "keep recent chunks of history" scheme, then it would itself keep the tree, as well. So checking the alternate objects is not worth doing, and come with a significant performance impact. In both cases, we skip any recent objects that have already been marked SEEN (i.e., that we know are already reachable for prune, or included in the pack for a repack). So there is a slight waste of time in opening the alternate packs at all, only to notice that we have already considered each object. But much worse, the alternate repository may have a large number of objects that are not reachable from the local repository at all, and we end up adding them to the traversal. We can fix this by considering only local unseen objects. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b0a4264 commit 1385bb7

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

cache.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,14 +1283,16 @@ int for_each_loose_file_in_objdir_buf(struct strbuf *path,
12831283

12841284
/*
12851285
* Iterate over loose and packed objects in both the local
1286-
* repository and any alternates repositories.
1286+
* repository and any alternates repositories (unless the
1287+
* LOCAL_ONLY flag is set).
12871288
*/
1289+
#define FOR_EACH_OBJECT_LOCAL_ONLY 0x1
12881290
typedef int each_packed_object_fn(const unsigned char *sha1,
12891291
struct packed_git *pack,
12901292
uint32_t pos,
12911293
void *data);
1292-
extern int for_each_loose_object(each_loose_object_fn, void *);
1293-
extern int for_each_packed_object(each_packed_object_fn, void *);
1294+
extern int for_each_loose_object(each_loose_object_fn, void *, unsigned flags);
1295+
extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags);
12941296

12951297
struct object_info {
12961298
/* Request */

reachable.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,12 @@ int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
142142
data.revs = revs;
143143
data.timestamp = timestamp;
144144

145-
r = for_each_loose_object(add_recent_loose, &data);
145+
r = for_each_loose_object(add_recent_loose, &data,
146+
FOR_EACH_OBJECT_LOCAL_ONLY);
146147
if (r)
147148
return r;
148-
return for_each_packed_object(add_recent_packed, &data);
149+
return for_each_packed_object(add_recent_packed, &data,
150+
FOR_EACH_OBJECT_LOCAL_ONLY);
149151
}
150152

151153
void mark_reachable_objects(struct rev_info *revs, int mark_reflog,

sha1_file.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,7 +3418,7 @@ static int loose_from_alt_odb(struct alternate_object_database *alt,
34183418
return r;
34193419
}
34203420

3421-
int for_each_loose_object(each_loose_object_fn cb, void *data)
3421+
int for_each_loose_object(each_loose_object_fn cb, void *data, unsigned flags)
34223422
{
34233423
struct loose_alt_odb_data alt;
34243424
int r;
@@ -3428,6 +3428,9 @@ int for_each_loose_object(each_loose_object_fn cb, void *data)
34283428
if (r)
34293429
return r;
34303430

3431+
if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
3432+
return 0;
3433+
34313434
alt.cb = cb;
34323435
alt.data = data;
34333436
return foreach_alt_odb(loose_from_alt_odb, &alt);
@@ -3452,13 +3455,15 @@ static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn c
34523455
return r;
34533456
}
34543457

3455-
int for_each_packed_object(each_packed_object_fn cb, void *data)
3458+
int for_each_packed_object(each_packed_object_fn cb, void *data, unsigned flags)
34563459
{
34573460
struct packed_git *p;
34583461
int r = 0;
34593462

34603463
prepare_packed_git();
34613464
for (p = packed_git; p; p = p->next) {
3465+
if ((flags & FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
3466+
continue;
34623467
r = for_each_object_in_pack(p, cb, data);
34633468
if (r)
34643469
break;

0 commit comments

Comments
 (0)