Skip to content

Commit 736eb88

Browse files
peffgitster
authored andcommitted
for_each_packed_object: support iterating in pack-order
We currently iterate over objects within a pack in .idx order, which uses the object hashes. That means that it is effectively random with respect to the location of the object within the pack. If you're going to access the actual object data, there are two reasons to move linearly through the pack itself: 1. It improves the locality of access in the packfile. In the cold-cache case, this may mean fewer disk seeks, or better usage of disk cache. 2. We store related deltas together in the packfile. Which means that the delta base cache can operate much more efficiently if we visit all of those related deltas in sequence, as the earlier items are likely to still be in the cache. Whereas if we visit the objects in random order, our cache entries are much more likely to have been evicted by unrelated deltas in the meantime. So in general, if you're going to access the object contents pack order is generally going to end up more efficient. But if you're simply generating a list of object names, or if you're going to end up sorting the result anyway, you're better off just using the .idx order, as finding the pack order means generating the in-memory pack-revindex. According to the numbers in 8b8dfd5 (pack-revindex: radix-sort the revindex, 2013-07-11), that takes about 200ms for linux.git, and 20ms for git.git (those numbers are a few years old but are still a good ballpark). That makes it a good optimization for some cases (we can save tens of seconds in git.git by having good locality of delta access, for a 20ms cost), but a bad one for others (e.g., right now "cat-file --batch-all-objects --batch-check="%(objectname)" is 170ms in git.git, so adding 20ms to that is noticeable). Hence this patch makes it an optional flag. You can't actually do any interesting timings yet, as it's not plumbed through to any user-facing tools like cat-file. That will come in a later patch. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8b36155 commit 736eb88

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

cache.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,11 @@ enum for_each_object_flags {
16331633

16341634
/* Only iterate over packs obtained from the promisor remote. */
16351635
FOR_EACH_OBJECT_PROMISOR_ONLY = (1<<1),
1636+
1637+
/*
1638+
* Visit objects within a pack in packfile order rather than .idx order
1639+
*/
1640+
FOR_EACH_OBJECT_PACK_ORDER = (1<<2),
16361641
};
16371642

16381643
/*

commit-graph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ void write_commit_graph(const char *obj_dir,
730730
die("error adding pack %s", packname.buf);
731731
if (open_pack_index(p))
732732
die("error opening index for %s", packname.buf);
733-
for_each_object_in_pack(p, add_packed_commits, &oids);
733+
for_each_object_in_pack(p, add_packed_commits, &oids, 0);
734734
close_pack(p);
735735
}
736736
strbuf_release(&packname);

packfile.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,19 +1885,30 @@ int has_pack_index(const unsigned char *sha1)
18851885
return 1;
18861886
}
18871887

1888-
int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn cb, void *data)
1888+
int for_each_object_in_pack(struct packed_git *p,
1889+
each_packed_object_fn cb, void *data,
1890+
enum for_each_object_flags flags)
18891891
{
18901892
uint32_t i;
18911893
int r = 0;
18921894

1895+
if (flags & FOR_EACH_OBJECT_PACK_ORDER)
1896+
load_pack_revindex(p);
1897+
18931898
for (i = 0; i < p->num_objects; i++) {
1899+
uint32_t pos;
18941900
struct object_id oid;
18951901

1896-
if (!nth_packed_object_oid(&oid, p, i))
1902+
if (flags & FOR_EACH_OBJECT_PACK_ORDER)
1903+
pos = p->revindex[i].nr;
1904+
else
1905+
pos = i;
1906+
1907+
if (!nth_packed_object_oid(&oid, p, pos))
18971908
return error("unable to get sha1 of object %u in %s",
1898-
i, p->pack_name);
1909+
pos, p->pack_name);
18991910

1900-
r = cb(&oid, p, i, data);
1911+
r = cb(&oid, p, pos, data);
19011912
if (r)
19021913
break;
19031914
}
@@ -1922,7 +1933,7 @@ int for_each_packed_object(each_packed_object_fn cb, void *data,
19221933
pack_errors = 1;
19231934
continue;
19241935
}
1925-
r = for_each_object_in_pack(p, cb, data);
1936+
r = for_each_object_in_pack(p, cb, data, flags);
19261937
if (r)
19271938
break;
19281939
}

packfile.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,18 @@ extern int has_pack_index(const unsigned char *sha1);
153153
* By default, this includes both local and alternate packs.
154154
*
155155
* Note that some objects may appear twice if they are found in multiple packs.
156-
* Each pack is visited in an unspecified order. Objects within a pack are
157-
* visited in pack-idx order (i.e., sorted by oid).
156+
* Each pack is visited in an unspecified order. By default, objects within a
157+
* pack are visited in pack-idx order (i.e., sorted by oid).
158158
*
159159
* The list of flags can be found in cache.h.
160160
*/
161161
typedef int each_packed_object_fn(const struct object_id *oid,
162162
struct packed_git *pack,
163163
uint32_t pos,
164164
void *data);
165-
int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn, void *data);
165+
int for_each_object_in_pack(struct packed_git *p,
166+
each_packed_object_fn, void *data,
167+
enum for_each_object_flags flags);
166168
int for_each_packed_object(each_packed_object_fn, void *,
167169
enum for_each_object_flags flags);
168170

0 commit comments

Comments
 (0)