Skip to content

Commit 38d4deb

Browse files
toofishesgitster
authored andcommitted
pack-objects: don't traverse objects unnecessarily
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f380872 commit 38d4deb

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

builtin/pack-objects.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ static void add_family_to_write_order(struct object_entry **wo,
520520

521521
static struct object_entry **compute_write_order(void)
522522
{
523-
unsigned int i, wo_end;
523+
unsigned int i, wo_end, last_untagged;
524524

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

@@ -551,14 +551,15 @@ static struct object_entry **compute_write_order(void)
551551
for_each_tag_ref(mark_tagged, NULL);
552552

553553
/*
554-
* Give the commits in the original recency order until
554+
* Give the objects in the original recency order until
555555
* we see a tagged tip.
556556
*/
557557
for (i = wo_end = 0; i < nr_objects; i++) {
558558
if (objects[i].tagged)
559559
break;
560560
add_to_write_order(wo, &wo_end, &objects[i]);
561561
}
562+
last_untagged = i;
562563

563564
/*
564565
* Then fill all the tagged tips.
@@ -571,7 +572,7 @@ static struct object_entry **compute_write_order(void)
571572
/*
572573
* And then all remaining commits and tags.
573574
*/
574-
for (i = 0; i < nr_objects; i++) {
575+
for (i = last_untagged; i < nr_objects; i++) {
575576
if (objects[i].type != OBJ_COMMIT &&
576577
objects[i].type != OBJ_TAG)
577578
continue;
@@ -581,7 +582,7 @@ static struct object_entry **compute_write_order(void)
581582
/*
582583
* And then all the trees.
583584
*/
584-
for (i = 0; i < nr_objects; i++) {
585+
for (i = last_untagged; i < nr_objects; i++) {
585586
if (objects[i].type != OBJ_TREE)
586587
continue;
587588
add_to_write_order(wo, &wo_end, &objects[i]);
@@ -590,8 +591,13 @@ static struct object_entry **compute_write_order(void)
590591
/*
591592
* Finally all the rest in really tight order
592593
*/
593-
for (i = 0; i < nr_objects; i++)
594-
add_family_to_write_order(wo, &wo_end, &objects[i]);
594+
for (i = last_untagged; i < nr_objects; i++) {
595+
if (!objects[i].filled)
596+
add_family_to_write_order(wo, &wo_end, &objects[i]);
597+
}
598+
599+
if (wo_end != nr_objects)
600+
die("ordered %u objects, expected %"PRIu32, wo_end, nr_objects);
595601

596602
return wo;
597603
}

0 commit comments

Comments
 (0)