limit "contains" traversals based on commit timestamp

When looking for commits that contain other commits (e.g., via "git tag --contains"), we can end up traversing useless portions of the graph. For example, if I am looking for a tag that contains a commit made last week, there is not much point in traversing portions of the history graph made five years ago. This optimization can provide massive speedups. For example, doing "git tag --contains HEAD~200" in the linux-2.6 repository goes from: real 0m5.302s user 0m5.116s sys 0m0.184s to: real 0m0.030s user 0m0.020s sys 0m0.008s The downside is that we will no longer find some answers in the face of extreme clock skew, as we will stop the traversal early when seeing commits skewed too far into the past. Name-rev already implements a similar optimization, using a "slop" of one day to allow for a certain amount of clock skew in commit timestamps. This patch introduces a "core.clockskew" variable, which allows specifying the allowable amount of clock skew in seconds. For safety, it defaults to "none", causing a full traversal (i.e., no change in behavior from previous versions). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 11, 2011 at 19:04 UTC 0c811a7a6f93691604d48b1c13a6868c89971528
1 file changed +33 -3
builtin/tag.c
+33 -3
@@ -25,6 +25,8 @@ static const char * const git_tag_usage[] = {
25
26 static char signingkey[1000];
27
28 +static int core_clock_skew = -1;
29 +
30 struct tag_filter {
31 const char *pattern;
32 int lines;
@@ -42,7 +44,8 @@ static int in_commit_list(const struct commit_list *want, struct commit *c)
44 }
45
46 static int contains_recurse(struct commit *candidate,
45 - const struct commit_list *want)
47 + const struct commit_list *want,
48 + unsigned long cutoff)
49 {
50 struct commit_list *p;
51
@@ -59,9 +62,13 @@ static int contains_recurse(struct commit *candidate,
62 if (parse_commit(candidate) < 0)
63 return 0;
64
65 + /* stop searching if we go too far back in time */
66 + if (candidate->date < cutoff)
67 + return 0;
68 +
69 /* Otherwise recurse and mark ourselves for future traversals. */
70 for (p = candidate->parents; p; p = p->next) {
64 - if (contains_recurse(p->item, want)) {
71 + if (contains_recurse(p->item, want, cutoff)) {
72 candidate->object.flags |= TMP_MARK;
73 return 1;
74 }
@@ -72,7 +79,22 @@ static int contains_recurse(struct commit *candidate,
79
80 static int contains(struct commit *candidate, const struct commit_list *want)
81 {
75 - return contains_recurse(candidate, want);
82 + unsigned long cutoff = 0;
83 +
84 + if (core_clock_skew >= 0) {
85 + const struct commit_list *c;
86 + unsigned long min_date = ULONG_MAX;
87 + for (c = want; c; c = c->next) {
88 + if (parse_commit(c->item) < 0)
89 + continue;
90 + if (c->item->date < min_date)
91 + min_date = c->item->date;
92 + }
93 + if (min_date > core_clock_skew)
94 + cutoff = min_date - core_clock_skew;
95 + }
96 +
97 + return contains_recurse(candidate, want, cutoff);
98 }
99
100 static int show_reference(const char *refname, const unsigned char *sha1,
@@ -279,6 +301,14 @@ static int git_tag_config(const char *var, const char *value, void *cb)
301 return 0;
302 }
303
304 + if (!strcmp(var, "core.clockskew")) {
305 + if (!value || !strcmp(value, "none"))
306 + core_clock_skew = -1;
307 + else
308 + core_clock_skew = git_config_int(var, value);
309 + return 0;
310 + }
311 +
312 return git_default_config(var, value, cb);
313 }
314