Skip to content

Commit 3d11275

Browse files
garimasi514gitster
authored andcommitted
commit-graph: examine commits by generation number
When running 'git commit-graph write --changed-paths', we sort the commits by pack-order to save time when computing the changed-paths bloom filters. This does not help when finding the commits via the '--reachable' flag. If not using pack-order, then sort by generation number before examining the diff. Commits with similar generation are more likely to have many trees in common, making the diff faster. On the Linux kernel repository, this change reduced the computation time for 'git commit-graph write --reachable --changed-paths' from 3m00s to 1m37s. Helped-by: Jeff King <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Garima Singh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d21ee7d commit 3d11275

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

commit-graph.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ static int commit_pos_cmp(const void *va, const void *vb)
7070
commit_pos_at(&commit_pos, b);
7171
}
7272

73+
static int commit_gen_cmp(const void *va, const void *vb)
74+
{
75+
const struct commit *a = *(const struct commit **)va;
76+
const struct commit *b = *(const struct commit **)vb;
77+
78+
/* lower generation commits first */
79+
if (a->generation < b->generation)
80+
return -1;
81+
else if (a->generation > b->generation)
82+
return 1;
83+
84+
/* use date as a heuristic when generations are equal */
85+
if (a->date < b->date)
86+
return -1;
87+
else if (a->date > b->date)
88+
return 1;
89+
return 0;
90+
}
91+
7392
char *get_commit_graph_filename(struct object_directory *obj_dir)
7493
{
7594
return xstrfmt("%s/info/commit-graph", obj_dir->path);
@@ -815,7 +834,8 @@ struct write_commit_graph_context {
815834
report_progress:1,
816835
split:1,
817836
check_oids:1,
818-
changed_paths:1;
837+
changed_paths:1,
838+
order_by_pack:1;
819839

820840
const struct split_commit_graph_opts *split_opts;
821841
size_t total_bloom_filter_data_size;
@@ -1178,7 +1198,11 @@ static void compute_bloom_filters(struct write_commit_graph_context *ctx)
11781198

11791199
ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
11801200
COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1181-
QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1201+
1202+
if (ctx->order_by_pack)
1203+
QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1204+
else
1205+
QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
11821206

11831207
for (i = 0; i < ctx->commits.nr; i++) {
11841208
struct commit *c = sorted_commits[i];
@@ -1884,6 +1908,7 @@ int write_commit_graph(struct object_directory *odb,
18841908
}
18851909

18861910
if (pack_indexes) {
1911+
ctx->order_by_pack = 1;
18871912
if ((res = fill_oids_from_packs(ctx, pack_indexes)))
18881913
goto cleanup;
18891914
}
@@ -1893,8 +1918,10 @@ int write_commit_graph(struct object_directory *odb,
18931918
goto cleanup;
18941919
}
18951920

1896-
if (!pack_indexes && !commit_hex)
1921+
if (!pack_indexes && !commit_hex) {
1922+
ctx->order_by_pack = 1;
18971923
fill_oids_from_all_packs(ctx);
1924+
}
18981925

18991926
close_reachable(ctx);
19001927

0 commit comments

Comments
 (0)