last-modified: use Bloom filters when available

Our 'git last-modified' performs a revision walk, and computes a diff at each point in the walk to figure out whether a given revision changed any of the paths it considers interesting. When changed-path Bloom filters are available, we can avoid computing many such diffs. Before computing a diff, we first check if any of the remaining paths of interest were possibly changed at a given commit by consulting its Bloom filter. If any of them are, we are resigned to compute the diff. If none of those queries returned "maybe", we know that the given commit doesn't contain any changed paths which are interesting to us. So, we can avoid computing it in this case. Comparing the perf test results on git.git: Test HEAD~ HEAD ------------------------------------------------------------------------------------ 8020.1: top-level last-modified 4.49(4.34+0.11) 2.22(2.05+0.09) -50.6% 8020.2: top-level recursive last-modified 5.64(5.45+0.11) 5.62(5.30+0.11) -0.4% 8020.3: subdir last-modified 0.11(0.06+0.04) 0.07(0.03+0.04) -36.4% Based-on-patch-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Toon Claes <toon@iotcl.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Toon Claes committed Aug 5, 2025 at 11:33 UTC 8d9a7cdfda4c883e83d6ea7b57d0a1d989a7d439
2 files changed +52 -3
builtin/last-modified.c
+46 -2
@@ -1,5 +1,7 @@
1 #include "git-compat-util.h"
2 +#include "bloom.h"
3 #include "builtin.h"
4 +#include "commit-graph.h"
5 #include "commit.h"
6 #include "config.h"
7 #include "diff.h"
@@ -18,6 +20,7 @@
20 struct last_modified_entry {
21 struct hashmap_entry hashent;
22 struct object_id oid;
23 + struct bloom_key key;
24 const char path[FLEX_ARRAY];
25 };
26
@@ -42,6 +45,12 @@ struct last_modified {
45
46 static void last_modified_release(struct last_modified *lm)
47 {
48 + struct hashmap_iter iter;
49 + struct last_modified_entry *ent;
50 +
51 + hashmap_for_each_entry(&lm->paths, &iter, ent, hashent)
52 + bloom_key_clear(&ent->key);
53 +
54 hashmap_clear_and_free(&lm->paths, struct last_modified_entry, hashent);
55 release_revisions(&lm->rev);
56 }
@@ -63,6 +72,9 @@ static void add_path_from_diff(struct diff_queue_struct *q,
72
73 FLEX_ALLOC_STR(ent, path, path);
74 oidcpy(&ent->oid, &p->two->oid);
75 + if (lm->rev.bloom_filter_settings)
76 + bloom_key_fill(&ent->key, path, strlen(path),
77 + lm->rev.bloom_filter_settings);
78 hashmap_entry_init(&ent->hashent, strhash(ent->path));
79 hashmap_add(&lm->paths, &ent->hashent);
80 }
@@ -139,6 +151,7 @@ static void mark_path(const char *path, const struct object_id *oid,
151 last_modified_emit(data->lm, path, data->commit);
152
153 hashmap_remove(&data->lm->paths, &ent->hashent, path);
154 + bloom_key_clear(&ent->key);
155 free(ent);
156 }
157
@@ -182,6 +195,30 @@ static void last_modified_diff(struct diff_queue_struct *q,
195 }
196 }
197
198 +static bool maybe_changed_path(struct last_modified *lm, struct commit *origin)
199 +{
200 + struct bloom_filter *filter;
201 + struct last_modified_entry *ent;
202 + struct hashmap_iter iter;
203 +
204 + if (!lm->rev.bloom_filter_settings)
205 + return true;
206 +
207 + if (commit_graph_generation(origin) == GENERATION_NUMBER_INFINITY)
208 + return true;
209 +
210 + filter = get_bloom_filter(lm->rev.repo, origin);
211 + if (!filter)
212 + return true;
213 +
214 + hashmap_for_each_entry(&lm->paths, &iter, ent, hashent) {
215 + if (bloom_filter_contains(filter, &ent->key,
216 + lm->rev.bloom_filter_settings))
217 + return true;
218 + }
219 + return false;
220 +}
221 +
222 static int last_modified_run(struct last_modified *lm)
223 {
224 struct last_modified_callback_data data = { .lm = lm };
@@ -202,9 +239,14 @@ static int last_modified_run(struct last_modified *lm)
239 &data.commit->object.oid, "",
240 &lm->rev.diffopt);
241 diff_flush(&lm->rev.diffopt);
205 - } else {
206 - log_tree_commit(&lm->rev, data.commit);
242 +
243 + break;
244 }
245 +
246 + if (!maybe_changed_path(lm, data.commit))
247 + continue;
248 +
249 + log_tree_commit(&lm->rev, data.commit);
250 }
251
252 return 0;
@@ -231,6 +273,8 @@ static int last_modified_init(struct last_modified *lm, struct repository *r,
273 return argc;
274 }
275
276 + lm->rev.bloom_filter_settings = get_bloom_filter_settings(lm->rev.repo);
277 +
278 if (populate_paths_from_revs(lm) < 0)
279 return error(_("unable to setup last-modified"));
280
commit-graph.c
+6 -1
@@ -820,7 +820,12 @@ int corrected_commit_dates_enabled(struct repository *r)
820
821 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
822 {
823 - struct commit_graph *g = r->objects->commit_graph;
823 + struct commit_graph *g;
824 +
825 + if (!prepare_commit_graph(r))
826 + return NULL;
827 +
828 + g = r->objects->commit_graph;
829 while (g) {
830 if (g->bloom_filter_settings)
831 return g->bloom_filter_settings;