From f751ee94c4dbd490eaa95bec83da9a383fd9acc4 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 17 Jul 2026 17:46:59 +0200 Subject: [PATCH 1/4] revision: move bloom keyvec precondition into function There are currently two callsites calling check_maybe_different_in_bloom_filter(). They both check if revs->bloom_keyvecs_nr is not zero before they call that function. Move bloom_keyvecs_nr precondition into check_maybe_different_in_bloom_filter() to simplify the code. Note that this changes `bloom_ret` to become -1 when there are no Bloom key vectors, which results in `count_bloom_filter_false_positive` not being incremented. This is unobservable, as the Bloom statistics are only reported when key vectors were set up. Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- revision.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/revision.c b/revision.c index ccbe2e03d1..087d64d572 100644 --- a/revision.c +++ b/revision.c @@ -752,6 +752,9 @@ static int check_maybe_different_in_bloom_filter(struct rev_info *revs, struct bloom_filter *filter; int result = 0; + if (!revs->bloom_keyvecs_nr) + return -1; + if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY) return -1; @@ -806,7 +809,7 @@ static int rev_compare_tree(struct rev_info *revs, return REV_TREE_SAME; } - if (revs->bloom_keyvecs_nr && !nth_parent) { + if (!nth_parent) { bloom_ret = check_maybe_different_in_bloom_filter(revs, commit); if (bloom_ret == 0) @@ -833,7 +836,7 @@ static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit, if (!t1) return 0; - if (!nth_parent && revs->bloom_keyvecs_nr) { + if (!nth_parent) { bloom_ret = check_maybe_different_in_bloom_filter(revs, commit); if (!bloom_ret) return 1; From 6e43a0623ecbebbd4898cd79fc33272b420ef35a Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 17 Jul 2026 17:47:00 +0200 Subject: [PATCH 2/4] revision: expose check for paths maybe changed in Bloom filter check_maybe_different_in_bloom_filter() looks up a commit's changed-path Bloom filter and consults it to see whether the commit might have modified any of the paths in the pathspec that `revs` was set up with. In a follow-up commit we want to reuse this logic from another builtin. That caller, however, has already looked up the commit's Bloom filter for its own purposes, so having the function look it up again would mean a redundant lookup. Extract the filter-consulting part into a new public function, revs_maybe_changed_in_bloom(). This function takes an already looked-up `struct bloom_filter` instead of a commit. The existing check_maybe_different_in_bloom_filter() becomes a thin wrapper that looks up the filter and delegates. Expose the new function via revision.h so other builtins can reuse the exact same filtering that `git log ` performs. Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- revision.c | 31 +++++++++++++++++++++---------- revision.h | 17 +++++++++++++++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/revision.c b/revision.c index 087d64d572..deaabec3e4 100644 --- a/revision.c +++ b/revision.c @@ -750,32 +750,43 @@ static int check_maybe_different_in_bloom_filter(struct rev_info *revs, struct commit *commit) { struct bloom_filter *filter; - int result = 0; - - if (!revs->bloom_keyvecs_nr) - return -1; + int result; if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY) return -1; filter = get_bloom_filter(revs->repo, commit); - if (!filter) { count_bloom_filter_not_present++; return -1; } + result = revs_maybe_changed_in_bloom(revs, filter); + if (result < 0) + return result; + + if (result) + count_bloom_filter_maybe++; + else + count_bloom_filter_definitely_not++; + + return result; +} + +int revs_maybe_changed_in_bloom(struct rev_info *revs, + struct bloom_filter *filter) +{ + int result = 0; + + if (!revs->bloom_keyvecs_nr) + return -1; + for (size_t nr = 0; !result && nr < revs->bloom_keyvecs_nr; nr++) { result = bloom_filter_contains_vec(filter, revs->bloom_keyvecs[nr], revs->bloom_filter_settings); } - if (result) - count_bloom_filter_maybe++; - else - count_bloom_filter_definitely_not++; - return result; } diff --git a/revision.h b/revision.h index 569b3fa1cb..7569c210cc 100644 --- a/revision.h +++ b/revision.h @@ -68,6 +68,7 @@ struct string_list; struct saved_parents; struct follow_pathspec_slab; struct bloom_keyvec; +struct bloom_filter; struct bloom_filter_settings; struct option; struct parse_opt_ctx_t; @@ -493,6 +494,22 @@ void reset_revision_walk(void); */ int prepare_revision_walk(struct rev_info *revs); +/** + * Take in a changed-path Bloom filter that belongs to a commit, and consult it + * to see if it might have modified any of the paths in the `revs`. + * The caller should look up `filter`, probably with get_bloom_filter(). + * prepare_revision_walk() needs to be called in advance to ensure + * pathspec key vectors are set up. + * + * Returns -1 if no sensible answer could be given because of missing + * preconditions (no pathspec key vectors). + * Returns 0 if the commit definitely did not change any of the paths and 1 if + * the commit maybe has changed one of them, although that might be a + * false-positive. + */ +int revs_maybe_changed_in_bloom(struct rev_info *revs, + struct bloom_filter *filter); + /* Drain the commits linked list into the priority queue. */ void rev_info_commit_list_to_queue(struct rev_info *revs); /** From 31f4a59adba09f327e965694cae9c71e876266eb Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 17 Jul 2026 17:47:01 +0200 Subject: [PATCH 3/4] last-modified: check pathspec against Bloom filter first When git-last-modified(1) starts, it builds a list of all the paths matching the pathspec it needs to find the last modifying commit for. For example, every file and subdirectory listed by: $ git last-modified -t --max-depth=0 -- src/ As it resolves a commit for each path during the revision walk, it drops that path from the list. To avoid diffing trees for every commit, Bloom filters are used when available. For each remaining path, the commit's Bloom filter is checked to see whether the commit changed that path. The Bloom filter says either "no" or "maybe", and only in the latter case is the diff calculated. git-log(1) does this differently. It does not expand the pathspec but checks the Bloom filter against the pathspec itself. This way, commits not touching any path matching the pathspec can be discarded as a whole. Apply this same check to git-last-modified(1). In a previous commit the function revs_maybe_changed_in_bloom(), used by git-log(1), was made public. Use this as a pre-filter in git-last-modified(1). After this pre-filter, paths are still checked one-by-one to only find those which don't have a "last commit" yet. Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- builtin/last-modified.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/builtin/last-modified.c b/builtin/last-modified.c index 5478182f2e..e8ee610404 100644 --- a/builtin/last-modified.c +++ b/builtin/last-modified.c @@ -272,6 +272,9 @@ static bool maybe_changed_path(struct last_modified *lm, if (!filter) return true; + if (revs_maybe_changed_in_bloom(&lm->rev, filter) == 0) + return false; + hashmap_for_each_entry(&lm->paths, &iter, ent, hashent) { if (active && !bitmap_get(active, ent->diff_idx)) continue; From 3a60e8d3564a106cd6695afc55a5766d4dfc67b0 Mon Sep 17 00:00:00 2001 From: Toon Claes Date: Fri, 17 Jul 2026 17:47:02 +0200 Subject: [PATCH 4/4] last-modified: keep per-path Bloom filters for wildcard pathspecs The last-modified builtin expands the pathspec to a set of literal paths and builds a Bloom key for each. During the walk it looks those keys up in the commit's filter to decide whether the commit is worth diffing. These lookups need `bloom_filter_settings` for the key hashing. prepare_revision_walk() runs prepare_to_use_bloom_filter() to build the pathspec key vectors. For a pathspec that cannot be turned into a Bloom key, such as a top-level wildcard like "*.c", that function gives up and clears `bloom_filter_settings`. Restore `bloom_filter_settings` after prepare_revision_walk() so the per-path check keeps working for wildcard pathspecs. Signed-off-by: Toon Claes Signed-off-by: Junio C Hamano --- builtin/last-modified.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/builtin/last-modified.c b/builtin/last-modified.c index e8ee610404..adc7cd8c74 100644 --- a/builtin/last-modified.c +++ b/builtin/last-modified.c @@ -360,6 +360,14 @@ static int last_modified_run(struct last_modified *lm) prepare_revision_walk(&lm->rev); + /* + * prepare_revision_walk() clears bloom_filter_settings for pathspecs + * without a Bloom key. Restore it so the per-path check keeps working. + */ + if (!lm->rev.bloom_filter_settings) + lm->rev.bloom_filter_settings = + get_bloom_filter_settings(lm->rev.repo); + max_count = lm->rev.max_count; init_active_paths_for_commit(&lm->active_paths);