Skip to content

Commit

Permalink
pack-objects: don't traverse objects unnecessarily
Browse files Browse the repository at this point in the history
This brings back some of the performance lost in optimizing recency
order inside pack objects. We were doing extreme amounts of object
re-traversal: for the 2.14 million objects in the Linux kernel
repository, we were calling add_to_write_order() over 1.03 billion times
(a 0.2% hit rate, making 99.8% of of these calls extraneous).

Two optimizations take place here- we can start our objects array
iteration from a known point where we left off before we started trying
to find our tags, and we don't need to do the deep dives required by
add_family_to_write_order() if the object has already been marked as
filled.

These two optimizations bring some pretty spectacular results via `perf
stat`:

task-clock:   83373 ms        --> 43800 ms         (50% faster)
cycles:       221,633,461,676 --> 116,307,209,986  (47% fewer)
instructions: 149,299,179,939 --> 122,998,800,184  (18% fewer)

Helped-by: Ramsay Jones (format string fix in "die" message)
Signed-off-by: Dan McGee <dpmcgee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Dan McGee authored and Junio C Hamano committed Oct 21, 2011
1 parent f380872 commit 38d4deb
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions builtin/pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ static void add_family_to_write_order(struct object_entry **wo,

static struct object_entry **compute_write_order(void)
{
unsigned int i, wo_end;
unsigned int i, wo_end, last_untagged;

struct object_entry **wo = xmalloc(nr_objects * sizeof(*wo));

Expand Down Expand Up @@ -551,14 +551,15 @@ static struct object_entry **compute_write_order(void)
for_each_tag_ref(mark_tagged, NULL);

/*
* Give the commits in the original recency order until
* Give the objects in the original recency order until
* we see a tagged tip.
*/
for (i = wo_end = 0; i < nr_objects; i++) {
if (objects[i].tagged)
break;
add_to_write_order(wo, &wo_end, &objects[i]);
}
last_untagged = i;

/*
* Then fill all the tagged tips.
Expand All @@ -571,7 +572,7 @@ static struct object_entry **compute_write_order(void)
/*
* And then all remaining commits and tags.
*/
for (i = 0; i < nr_objects; i++) {
for (i = last_untagged; i < nr_objects; i++) {
if (objects[i].type != OBJ_COMMIT &&
objects[i].type != OBJ_TAG)
continue;
Expand All @@ -581,7 +582,7 @@ static struct object_entry **compute_write_order(void)
/*
* And then all the trees.
*/
for (i = 0; i < nr_objects; i++) {
for (i = last_untagged; i < nr_objects; i++) {
if (objects[i].type != OBJ_TREE)
continue;
add_to_write_order(wo, &wo_end, &objects[i]);
Expand All @@ -590,8 +591,13 @@ static struct object_entry **compute_write_order(void)
/*
* Finally all the rest in really tight order
*/
for (i = 0; i < nr_objects; i++)
add_family_to_write_order(wo, &wo_end, &objects[i]);
for (i = last_untagged; i < nr_objects; i++) {
if (!objects[i].filled)
add_family_to_write_order(wo, &wo_end, &objects[i]);
}

if (wo_end != nr_objects)
die("ordered %u objects, expected %"PRIu32, wo_end, nr_objects);

return wo;
}
Expand Down

0 comments on commit 38d4deb

Please sign in to comment.