Skip to content

Commit 3cb9d2b

Browse files
szedergitster
authored andcommitted
line-log: more responsive, incremental 'git log -L'
The current line-level log implementation performs a preprocessing step in prepare_revision_walk(), during which the line_log_filter() function filters and rewrites history to keep only commits modifying the given line range. This preprocessing affects both responsiveness and correctness: - Git doesn't produce any output during this preprocessing step. Checking whether a commit modified the given line range is somewhat expensive, so depending on the size of the given revision range this preprocessing can result in a significant delay before the first commit is shown. - Limiting the number of displayed commits (e.g. 'git log -3 -L...') doesn't limit the amount of work during preprocessing, because that limit is applied during history traversal. Alas, by that point this expensive preprocessing step has already churned through the whole revision range to find all commits modifying the revision range, even though only a few of them need to be shown. - It rewrites parents, with no way to turn it off. Without the user explicitly requesting parent rewriting any parent object ID shown should be that of the immediate parent, just like in case of a pathspec-limited history traversal without parent rewriting. However, after that preprocessing step rewrote history, the subsequent "regular" history traversal (i.e. get_revision() in a loop) only sees commits modifying the given line range. Consequently, it can only show the object ID of the last ancestor that modified the given line range (which might happen to be the immediate parent, but many-many times it isn't). This patch addresses both the correctness and, at least for the common case, the responsiveness issues by integrating line-level log filtering into the regular revision walking machinery: - Make process_ranges_arbitrary_commit(), the static function in 'line-log.c' deciding whether a commit modifies the given line range, public by removing the static keyword and adding the 'line_log_' prefix, so it can be called from other parts of the revision walking machinery. - If the user didn't explicitly ask for parent rewriting (which, I believe, is the most common case): - Call this now-public function during regular history traversal, namely from get_commit_action() to ignore any commits not modifying the given line range. Note that while this check is relatively expensive, it must be performed before other, much cheaper conditions, because the tracked line range must be adjusted even when the commit will end up being ignored by other conditions. - Skip the line_log_filter() call, i.e. the expensive preprocessing step, in prepare_revision_walk(), because, thanks to the above points, the revision walking machinery is now able to filter out commits not modifying the given line range while traversing history. This way the regular history traversal sees the unmodified history, and is therefore able to print the object ids of the immediate parents of the listed commits. The eliminated preprocessing step can greatly reduce the delay before the first commit is shown, see the numbers below. - However, if the user did explicitly ask for parent rewriting via '--parents' or a similar option, then stick with the current implementation for now, i.e. perform that expensive filtering and history rewriting in the preprocessing step just like we did before, leaving the initial delay as long as it was. I tried to integrate line-level log filtering with parent rewriting into the regular history traversal, but, unfortunately, several subtleties resisted... :) Maybe someday we'll figure out how to do that, but until then at least the simple and common (i.e. without parent rewriting) 'git log -L:func:file' commands can benefit from the reduced delay. This change makes the failing 'parent oids without parent rewriting' test in 't4211-line-log.sh' succeed. The reduced delay is most noticable when there's a commit modifying the line range near the tip of a large-ish revision range: # no parent rewriting requested, no commit-graph present $ time git --no-pager log -L:read_alternate_refs:sha1-file.c -1 v2.23.0 Before: real 0m9.570s user 0m9.494s sys 0m0.076s After: real 0m0.718s user 0m0.674s sys 0m0.044s A significant part of the remaining delay is spent reading and parsing commit objects in limit_list(). With the help of the commit-graph we can eliminate most of that reading and parsing overhead, so here are the timing results of the same command as above, but this time using the commit-graph: Before: real 0m8.874s user 0m8.816s sys 0m0.057s After: real 0m0.107s user 0m0.091s sys 0m0.013s The next patch will further reduce the remaining delay. To be clear: this patch doesn't actually optimize the line-level log, but merely moves most of the work from the preprocessing step to the history traversal, so the commits modifying the line range can be shown as soon as they are processed, and the traversal can be terminated as soon as the given number of commits are shown. Consequently, listing the full history of a line range, potentially all the way to the root commit, will take the same time as before (but at least the user might start reading the output earlier). Furthermore, if the most recent commit modifying the line range is far away from the starting revision, then that initial delay will still be significant. Additional testing by Derrick Stolee: In the Linux kernel repository, the MAINTAINERS file was changed ~3,500 times across the ~915,000 commits. In addition to that edit frequency, the file itself is quite large (~18,700 lines). This means that a significant portion of the computation is taken up by computing the patch-diff of the file. This patch improves the real time it takes to output the first result quite a bit: Command: git log -L 100,200:MAINTAINERS -n 1 >/dev/null Before: 3.88 s After: 0.71 s If we drop the "-n 1" in the command, then there is no change in end-to-end process time. This is because the command still needs to walk the entire commit history, which negates the point of this patch. This is expected. As a note for future reference, the ~4.3 seconds in the old code spends ~2.6 seconds computing the patch-diffs, and the rest of the time is spent walking commits and computing diffs for which paths changed at each commit. The changed-path Bloom filters could improve the end-to-end computation time (i.e. no "-n 1" in the command). Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 48da94b commit 3cb9d2b

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

line-log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *comm
12271227
/* NEEDSWORK leaking like a sieve */
12281228
}
12291229

1230-
static int process_ranges_arbitrary_commit(struct rev_info *rev, struct commit *commit)
1230+
int line_log_process_ranges_arbitrary_commit(struct rev_info *rev, struct commit *commit)
12311231
{
12321232
struct line_log_data *range = lookup_line_range(rev, commit);
12331233
int changed = 0;
@@ -1270,7 +1270,7 @@ int line_log_filter(struct rev_info *rev)
12701270
while (list) {
12711271
struct commit_list *to_free = NULL;
12721272
commit = list->item;
1273-
if (process_ranges_arbitrary_commit(rev, commit)) {
1273+
if (line_log_process_ranges_arbitrary_commit(rev, commit)) {
12741274
*pp = list;
12751275
pp = &list->next;
12761276
} else

line-log.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct line_log_data {
5454
void line_log_init(struct rev_info *rev, const char *prefix, struct string_list *args);
5555

5656
int line_log_filter(struct rev_info *rev);
57+
int line_log_process_ranges_arbitrary_commit(struct rev_info *rev,
58+
struct commit *commit);
5759

5860
int line_log_print(struct rev_info *rev, struct commit *commit);
5961

revision.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static const char *term_good;
3939

4040
implement_shared_commit_slab(revision_sources, char *);
4141

42+
static inline int want_ancestry(const struct rev_info *revs);
43+
4244
void show_object_with_name(FILE *out, struct object *obj, const char *name)
4345
{
4446
const char *p;
@@ -3511,7 +3513,14 @@ int prepare_revision_walk(struct rev_info *revs)
35113513
sort_in_topological_order(&revs->commits, revs->sort_order);
35123514
} else if (revs->topo_order)
35133515
init_topo_walk(revs);
3514-
if (revs->line_level_traverse)
3516+
if (revs->line_level_traverse && want_ancestry(revs))
3517+
/*
3518+
* At the moment we can only do line-level log with parent
3519+
* rewriting by performing this expensive pre-filtering step.
3520+
* If parent rewriting is not requested, then we rather
3521+
* perform the line-level log filtering during the regular
3522+
* history traversal.
3523+
*/
35153524
line_log_filter(revs);
35163525
if (revs->simplify_merges)
35173526
simplify_merges(revs);
@@ -3722,6 +3731,22 @@ enum commit_action get_commit_action(struct rev_info *revs, struct commit *commi
37223731
return commit_ignore;
37233732
if (commit->object.flags & UNINTERESTING)
37243733
return commit_ignore;
3734+
if (revs->line_level_traverse && !want_ancestry(revs)) {
3735+
/*
3736+
* In case of line-level log with parent rewriting
3737+
* prepare_revision_walk() already took care of all line-level
3738+
* log filtering, and there is nothing left to do here.
3739+
*
3740+
* If parent rewriting was not requested, then this is the
3741+
* place to perform the line-level log filtering. Notably,
3742+
* this check, though expensive, must come before the other,
3743+
* cheaper filtering conditions, because the tracked line
3744+
* ranges must be adjusted even when the commit will end up
3745+
* being ignored based on other conditions.
3746+
*/
3747+
if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3748+
return commit_ignore;
3749+
}
37253750
if (revs->min_age != -1 &&
37263751
comparison_date(revs, commit) > revs->min_age)
37273752
return commit_ignore;

t/t4211-line-log.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ test_expect_success 'setup for checking line-log and parent oids' '
263263
'
264264

265265
# Parent oid should be from immediate parent.
266-
test_expect_failure 'parent oids without parent rewriting' '
266+
test_expect_success 'parent oids without parent rewriting' '
267267
cat >expect <<-EOF &&
268268
$head_oid $prev_oid Modify func2() in file.c
269269
$root_oid Add func1() and func2() in file.c

0 commit comments

Comments
 (0)