last-modified: implement faster algorithm

The current implementation of git-last-modified(1) works by doing a revision walk, and inspecting the diff at each level of that walk to annotate entries remaining in the hashmap of paths. In other words, if the diff at some level touches a path which has not yet been associated with a commit, then that commit becomes associated with the path. While a perfectly reasonable implementation, it can perform poorly in either one of two scenarios: 1. There are many entries of interest, in which case there is simply a lot of work to do. 2. Or, there are (even a few) entries which have not been updated in a long time, and so we must walk through a lot of history in order to find a commit that touches that path. This patch rewrites the last-modified implementation that addresses the second point. The idea behind the algorithm is to propagate a set of 'active' paths (a path is 'active' if it does not yet belong to a commit) up to parents and do a truncated revision walk. The walk is truncated because it does not produce a revision for every change in the original pathspec, but rather only for active paths. More specifically, consider a priority queue of commits sorted by generation number. First, enqueue the set of boundary commits with all paths in the original spec marked as interesting. Then, while the queue is not empty, do the following: 1. Pop an element, say, 'c', off of the queue, making sure that 'c' isn't reachable by anything in the '--not' set. 2. For each parent 'p' (with index 'parent_i') of 'c', do the following: a. Compute the diff between 'c' and 'p'. b. Pass any active paths that are TREESAME from 'c' to 'p'. c. If 'p' has any active paths, push it onto the queue. 3. Any path that remains active on 'c' is associated to that commit. This ends up being equivalent to doing something like 'git log -1 -- $path' for each path simultaneously. But, it allows us to go much faster than the original implementation by limiting the number of diffs we compute, since we can avoid parts of history that would have been considered by the revision walk in the original implementation, but are known to be uninteresting to us because we have already marked all paths in that area to be inactive. To avoid computing many first-parent diffs, add another trick on top of this and check if all paths active in 'c' are DEFINITELY NOT in c's Bloom filter. Since the commit-graph only stores first-parent diffs in the Bloom filters, we can only apply this trick to first-parent diffs. Comparing the performance of this new algorithm shows about a 2.5x improvement on git.git: Benchmark 1: master no bloom Time (mean ± σ): 2.868 s ± 0.023 s [User: 2.811 s, System: 0.051 s] Range (min … max): 2.847 s … 2.926 s 10 runs Benchmark 2: master with bloom Time (mean ± σ): 949.9 ms ± 15.2 ms [User: 907.6 ms, System: 39.5 ms] Range (min … max): 933.3 ms … 971.2 ms 10 runs Benchmark 3: HEAD no bloom Time (mean ± σ): 782.0 ms ± 6.3 ms [User: 740.7 ms, System: 39.2 ms] Range (min … max): 776.4 ms … 798.2 ms 10 runs Benchmark 4: HEAD with bloom Time (mean ± σ): 307.1 ms ± 1.7 ms [User: 276.4 ms, System: 29.9 ms] Range (min … max): 303.7 ms … 309.5 ms 10 runs Summary HEAD with bloom ran 2.55 ± 0.02 times faster than HEAD no bloom 3.09 ± 0.05 times faster than master with bloom 9.34 ± 0.09 times faster than master no bloom In short, the existing implementation is comparably fast *with* Bloom filters as the new implementation is *without* Bloom filters. So, most repositories should get a dramatic speed-up by just deploying this (even without computing Bloom filters), and all repositories should get faster still when computing Bloom filters. When comparing a more extreme example of `git last-modified -- COPYING t`, the difference is even 5 times better: Benchmark 1: master Time (mean ± σ): 4.372 s ± 0.057 s [User: 4.286 s, System: 0.062 s] Range (min … max): 4.308 s … 4.509 s 10 runs Benchmark 2: HEAD Time (mean ± σ): 826.3 ms ± 22.3 ms [User: 784.1 ms, System: 39.2 ms] Range (min … max): 810.6 ms … 881.2 ms 10 runs Summary HEAD ran 5.29 ± 0.16 times faster than master As an added benefit, results are more consistent now. For example implementation in 'master' gives: $ git log --max-count=1 --format=%H -- pkt-line.h 15df15fe07ef66b51302bb77e393f3c5502629de $ git last-modified -- pkt-line.h 15df15fe07ef66b51302bb77e393f3c5502629de pkt-line.h $ git last-modified | grep pkt-line.h 5b49c1af03e600c286f63d9d9c9fb01403230b9f pkt-line.h With the changes in this patch the results of git-last-modified(1) always match those of `git log --max-count=1`. One thing to note though, the results might be outputted in a different order than before. This is not considerd to be an issue because nowhere is documented the order is guaranteed. Based-on-patches-by: Derrick Stolee <stolee@gmail.com> Based-on-patches-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Toon Claes <toon@iotcl.com> Acked-by: Taylor Blau <me@ttaylorr.com> [jc: tweaked use of xcalloc() to unbreak coccicheck] Signed-off-by: Junio C Hamano <gitster@pobox.com>

Toon Claes committed Oct 23, 2025 at 09:50 UTC 2a04e8c293766a4976ceceb4c663dd2963e0339e
3 files changed +237 -16
builtin/last-modified.c
+235 -15
@@ -2,26 +2,32 @@
2 #include "bloom.h"
3 #include "builtin.h"
4 #include "commit-graph.h"
5 +#include "commit-slab.h"
6 #include "commit.h"
7 #include "config.h"
7 -#include "environment.h"
8 #include "diff.h"
9 #include "diffcore.h"
10 #include "environment.h"
11 +#include "ewah/ewok.h"
12 #include "hashmap.h"
13 #include "hex.h"
13 -#include "log-tree.h"
14 #include "object-name.h"
15 #include "object.h"
16 #include "parse-options.h"
17 +#include "prio-queue.h"
18 #include "quote.h"
19 #include "repository.h"
20 #include "revision.h"
21
22 +/* Remember to update object flag allocation in object.h */
23 +#define PARENT1 (1u<<16) /* used instead of SEEN */
24 +#define PARENT2 (1u<<17) /* used instead of BOTTOM, BOUNDARY */
25 +
26 struct last_modified_entry {
27 struct hashmap_entry hashent;
28 struct object_id oid;
29 struct bloom_key key;
30 + size_t diff_idx;
31 const char path[FLEX_ARRAY];
32 };
33
@@ -37,13 +43,45 @@ static int last_modified_entry_hashcmp(const void *unused UNUSED,
43 return strcmp(ent1->path, path ? path : ent2->path);
44 }
45
46 +/*
47 + * Hold a bitmap for each commit we're working with. In the bitmap, each bit
48 + * represents a path in `lm->all_paths`. An active bit indicates the path still
49 + * needs to be associated to a commit.
50 + */
51 +define_commit_slab(active_paths_for_commit, struct bitmap *);
52 +
53 struct last_modified {
54 struct hashmap paths;
55 struct rev_info rev;
56 bool recursive;
57 bool show_trees;
58 +
59 + const char **all_paths;
60 + size_t all_paths_nr;
61 + struct active_paths_for_commit active_paths;
62 +
63 + /* 'scratch' to avoid allocating a bitmap every process_parent() */
64 + struct bitmap *scratch;
65 };
66
67 +static struct bitmap *active_paths_for(struct last_modified *lm, struct commit *c)
68 +{
69 + struct bitmap **bitmap = active_paths_for_commit_at(&lm->active_paths, c);
70 + if (!*bitmap)
71 + *bitmap = bitmap_word_alloc(lm->all_paths_nr / BITS_IN_EWORD + 1);
72 +
73 + return *bitmap;
74 +}
75 +
76 +static void active_paths_free(struct last_modified *lm, struct commit *c)
77 +{
78 + struct bitmap **bitmap = active_paths_for_commit_at(&lm->active_paths, c);
79 + if (*bitmap) {
80 + bitmap_free(*bitmap);
81 + *bitmap = NULL;
82 + }
83 +}
84 +
85 static void last_modified_release(struct last_modified *lm)
86 {
87 struct hashmap_iter iter;
@@ -54,6 +92,8 @@ static void last_modified_release(struct last_modified *lm)
92
93 hashmap_clear_and_free(&lm->paths, struct last_modified_entry, hashent);
94 release_revisions(&lm->rev);
95 +
96 + free(lm->all_paths);
97 }
98
99 struct last_modified_callback_data {
@@ -146,7 +186,7 @@ static void mark_path(const char *path, const struct object_id *oid,
186 * Is it arriving at a version of interest, or is it from a side branch
187 * which did not contribute to the final state?
188 */
149 - if (!oideq(oid, &ent->oid))
189 + if (oid && !oideq(oid, &ent->oid))
190 return;
191
192 last_modified_emit(data->lm, path, data->commit);
@@ -196,7 +236,17 @@ static void last_modified_diff(struct diff_queue_struct *q,
236 }
237 }
238
199 -static bool maybe_changed_path(struct last_modified *lm, struct commit *origin)
239 +static void pass_to_parent(struct bitmap *c,
240 + struct bitmap *p,
241 + size_t pos)
242 +{
243 + bitmap_unset(c, pos);
244 + bitmap_set(p, pos);
245 +}
246 +
247 +static bool maybe_changed_path(struct last_modified *lm,
248 + struct commit *origin,
249 + struct bitmap *active)
250 {
251 struct bloom_filter *filter;
252 struct last_modified_entry *ent;
@@ -213,6 +263,9 @@ static bool maybe_changed_path(struct last_modified *lm, struct commit *origin)
263 return true;
264
265 hashmap_for_each_entry(&lm->paths, &iter, ent, hashent) {
266 + if (active && !bitmap_get(active, ent->diff_idx))
267 + continue;
268 +
269 if (bloom_filter_contains(filter, &ent->key,
270 lm->rev.bloom_filter_settings))
271 return true;
@@ -220,42 +273,202 @@ static bool maybe_changed_path(struct last_modified *lm, struct commit *origin)
273 return false;
274 }
275
276 +static void process_parent(struct last_modified *lm,
277 + struct prio_queue *queue,
278 + struct commit *c, struct bitmap *active_c,
279 + struct commit *parent, int parent_i)
280 +{
281 + struct bitmap *active_p;
282 +
283 + repo_parse_commit(lm->rev.repo, parent);
284 + active_p = active_paths_for(lm, parent);
285 +
286 + /*
287 + * The first time entering this function for this commit (i.e. first parent)
288 + * see if Bloom filters will tell us it's worth to do the diff.
289 + */
290 + if (parent_i || maybe_changed_path(lm, c, active_c)) {
291 + diff_tree_oid(&parent->object.oid,
292 + &c->object.oid, "", &lm->rev.diffopt);
293 + diffcore_std(&lm->rev.diffopt);
294 + }
295 +
296 + /*
297 + * Test each path for TREESAME-ness against the parent. If a path is
298 + * TREESAME, pass it on to this parent.
299 + *
300 + * First, collect all paths that are *not* TREESAME in 'scratch'.
301 + * Then, pass paths that *are* TREESAME and active to the parent.
302 + */
303 + for (int i = 0; i < diff_queued_diff.nr; i++) {
304 + struct diff_filepair *fp = diff_queued_diff.queue[i];
305 + const char *path = fp->two->path;
306 + struct last_modified_entry *ent =
307 + hashmap_get_entry_from_hash(&lm->paths, strhash(path), path,
308 + struct last_modified_entry, hashent);
309 + if (ent) {
310 + size_t k = ent->diff_idx;
311 + if (bitmap_get(active_c, k))
312 + bitmap_set(lm->scratch, k);
313 + }
314 + }
315 + for (size_t i = 0; i < lm->all_paths_nr; i++) {
316 + if (bitmap_get(active_c, i) && !bitmap_get(lm->scratch, i))
317 + pass_to_parent(active_c, active_p, i);
318 + }
319 +
320 + /*
321 + * If parent has any active paths, put it on the queue (if not already).
322 + */
323 + if (!bitmap_is_empty(active_p) && !(parent->object.flags & PARENT1)) {
324 + parent->object.flags |= PARENT1;
325 + prio_queue_put(queue, parent);
326 + }
327 + if (!(parent->object.flags & PARENT1))
328 + active_paths_free(lm, parent);
329 +
330 + memset(lm->scratch->words, 0x0, lm->scratch->word_alloc);
331 + diff_queue_clear(&diff_queued_diff);
332 +}
333 +
334 static int last_modified_run(struct last_modified *lm)
335 {
336 + int max_count, queue_popped = 0;
337 + struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
338 + struct prio_queue not_queue = { compare_commits_by_gen_then_commit_date };
339 + struct commit_list *list;
340 struct last_modified_callback_data data = { .lm = lm };
341
342 lm->rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
343 lm->rev.diffopt.format_callback = last_modified_diff;
344 lm->rev.diffopt.format_callback_data = &data;
345 + lm->rev.no_walk = 1;
346
347 prepare_revision_walk(&lm->rev);
348
233 - while (hashmap_get_size(&lm->paths)) {
234 - data.commit = get_revision(&lm->rev);
235 - if (!data.commit)
236 - BUG("paths remaining beyond boundary in last-modified");
349 + max_count = lm->rev.max_count;
350 +
351 + init_active_paths_for_commit(&lm->active_paths);
352 + lm->scratch = bitmap_word_alloc(lm->all_paths_nr);
353 +
354 + /*
355 + * lm->rev.commits holds the set of boundary commits for our walk.
356 + *
357 + * Loop through each such commit, and place it in the appropriate queue.
358 + */
359 + for (list = lm->rev.commits; list; list = list->next) {
360 + struct commit *c = list->item;
361 +
362 + if (c->object.flags & BOTTOM) {
363 + prio_queue_put(&not_queue, c);
364 + c->object.flags |= PARENT2;
365 + } else if (!(c->object.flags & PARENT1)) {
366 + /*
367 + * If the commit is a starting point (and hasn't been
368 + * seen yet), then initialize the set of interesting
369 + * paths, too.
370 + */
371 + struct bitmap *active;
372 +
373 + prio_queue_put(&queue, c);
374 + c->object.flags |= PARENT1;
375 +
376 + active = active_paths_for(lm, c);
377 + for (size_t i = 0; i < lm->all_paths_nr; i++)
378 + bitmap_set(active, i);
379 + }
380 + }
381
238 - if (data.commit->object.flags & BOUNDARY) {
382 + while (queue.nr) {
383 + int parent_i;
384 + struct commit_list *p;
385 + struct commit *c = prio_queue_get(&queue);
386 + struct bitmap *active_c = active_paths_for(lm, c);
387 +
388 + if ((0 <= max_count && max_count < ++queue_popped) ||
389 + (c->object.flags & PARENT2)) {
390 + /*
391 + * Either a boundary commit, or we have already seen too
392 + * many others. Either way, stop here.
393 + */
394 + c->object.flags |= PARENT2 | BOUNDARY;
395 + data.commit = c;
396 diff_tree_oid(lm->rev.repo->hash_algo->empty_tree,
240 - &data.commit->object.oid, "",
241 - &lm->rev.diffopt);
397 + &c->object.oid,
398 + "", &lm->rev.diffopt);
399 diff_flush(&lm->rev.diffopt);
400 + goto cleanup;
401 + }
402
244 - break;
403 + /*
404 + * Otherwise, make sure that 'c' isn't reachable from anything
405 + * in the '--not' queue.
406 + */
407 + repo_parse_commit(lm->rev.repo, c);
408 +
409 + while (not_queue.nr) {
410 + struct commit_list *np;
411 + struct commit *n = prio_queue_get(&not_queue);
412 +
413 + repo_parse_commit(lm->rev.repo, n);
414 +
415 + for (np = n->parents; np; np = np->next) {
416 + if (!(np->item->object.flags & PARENT2)) {
417 + prio_queue_put(&not_queue, np->item);
418 + np->item->object.flags |= PARENT2;
419 + }
420 + }
421 +
422 + if (commit_graph_generation(n) < commit_graph_generation(c))
423 + break;
424 }
425
247 - if (!maybe_changed_path(lm, data.commit))
248 - continue;
426 + /*
427 + * Look at each parent and pass on each path that's TREESAME
428 + * with that parent. Stop early when no active paths remain.
429 + */
430 + for (p = c->parents, parent_i = 0; p; p = p->next, parent_i++) {
431 + process_parent(lm, &queue,
432 + c, active_c,
433 + p->item, parent_i);
434 +
435 + if (bitmap_is_empty(active_c))
436 + break;
437 + }
438 +
439 + /*
440 + * Paths that remain active, or not TREESAME with any parent,
441 + * were changed by 'c'.
442 + */
443 + if (!bitmap_is_empty(active_c)) {
444 + data.commit = c;
445 + for (size_t i = 0; i < lm->all_paths_nr; i++) {
446 + if (bitmap_get(active_c, i))
447 + mark_path(lm->all_paths[i], NULL, &data);
448 + }
449 + }
450
250 - log_tree_commit(&lm->rev, data.commit);
451 +cleanup:
452 + active_paths_free(lm, c);
453 }
454
455 + if (hashmap_get_size(&lm->paths))
456 + BUG("paths remaining beyond boundary in last-modified");
457 +
458 + clear_prio_queue(&not_queue);
459 + clear_prio_queue(&queue);
460 + clear_active_paths_for_commit(&lm->active_paths);
461 + bitmap_free(lm->scratch);
462 +
463 return 0;
464 }
465
466 static int last_modified_init(struct last_modified *lm, struct repository *r,
467 const char *prefix, int argc, const char **argv)
468 {
469 + struct hashmap_iter iter;
470 + struct last_modified_entry *ent;
471 +
472 hashmap_init(&lm->paths, last_modified_entry_hashcmp, NULL, 0);
473
474 repo_init_revisions(r, &lm->rev, prefix);
@@ -280,6 +493,13 @@ static int last_modified_init(struct last_modified *lm, struct repository *r,
493 if (populate_paths_from_revs(lm) < 0)
494 return error(_("unable to setup last-modified"));
495
496 + CALLOC_ARRAY(lm->all_paths, hashmap_get_size(&lm->paths));
497 + lm->all_paths_nr = 0;
498 + hashmap_for_each_entry(&lm->paths, &iter, ent, hashent) {
499 + ent->diff_idx = lm->all_paths_nr++;
500 + lm->all_paths[ent->diff_idx] = ent->path;
501 + }
502 +
503 return 0;
504 }
505
object.h
+1
@@ -75,6 +75,7 @@ void object_array_init(struct object_array *array);
75 * http-push.c: 11-----14
76 * commit-graph.c: 15
77 * commit-reach.c: 16-----19
78 + * builtin/last-modified.c: 1617
79 * sha1-name.c: 20
80 * list-objects-filter.c: 21
81 * bloom.c: 2122
t/t8020-last-modified.sh
+1 -1
@@ -57,9 +57,9 @@ test_expect_success 'last-modified recursive' '
57
58 test_expect_success 'last-modified recursive with show-trees' '
59 check_last_modified -r -t <<-\EOF
60 - 3 a
60 3 a/b
61 3 a/b/file
62 + 3 a
63 2 a/file
64 1 file
65 EOF