Skip to content

Commit b16a827

Browse files
derrickstoleegitster
authored andcommitted
bloom/diff: properly short-circuit on max_changes
Commit e369698 (diff: halt tree-diff early after max_changes, 2020-03-30) intended to create a mechanism to short-circuit a diff calculation after a certain number of paths were modified. By incrementing a "num_changes" counter throughout the recursive ll_diff_tree_paths(), this was supposed to match the number of changes that would be written into the changed-path Bloom filters. Unfortunately, this was not implemented correctly and instead misses simple cases like file modifications. This then does not stop very large changed-path filters from being written (unless they add or remove many files). To start, change the implementation in ll_diff_tree_paths() to instead use the global diff_queue_diff struct's 'nr' member as the count. This is a way to simplify the logic instead of making more mistakes in the complicated diff code. This has a drawback: the diff_queue_diff struct only lists the paths corresponding to blob changes, not their leading directories. Thus, get_or_compute_bloom_filter() needs an additional check to see if the hashmap with the leading directories becomes too large. One reason why this was not caught by test cases was that the test in t4216-log-bloom.sh that was supposed to check this "too many changes" condition only checked this on the initial commit of a repository. The old logic counted these values correctly. Update this test in a few ways: 1. Use GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS to reduce the limit, allowing smaller commits to engage with this logic. 2. Create several interesting cases of edits, adds, removes, and mode changes (in the second commit). By testing both sides of the inequality with the *_MAX_CHANGED_PATHS variable, we can see that the count is exactly correct, so none of these changes are missed or over-counted. 3. Use the trace2 data value filter_found_large to verify that these commits are on the correct side of the limit. Another way to verify the behavior is correct is through performance tests. By testing on my local copies of the Git repository and the Linux kernel repository, I could measure the effect of these short-circuits when computing a fresh commit-graph file with changed-path Bloom filters using the command GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS=N time \ git commit-graph write --reachable --changed-paths and reporting the wall time and resulting commit-graph size. For Git, the results are | | N=1 | N=10 | N=512 | |--------|----------------|----------------|----------------| | HEAD~1 | 10.90s 9.18MB | 11.11s 9.34MB | 11.31s 9.35MB | | HEAD | 9.21s 8.62MB | 11.11s 9.29MB | 11.29s 9.34MB | For Linux, the results are | | N=1 | N=20 | N=512 | |--------|----------------|---------------|---------------| | HEAD~1 | 61.28s 64.3MB | 76.9s 72.6MB | 77.6s 72.6MB | | HEAD | 49.44s 56.3MB | 68.7s 65.9MB | 69.2s 65.9MB | Naturally, the improvement becomes much less as the limit grows, as fewer commits satisfy the short-circuit. Reported-by: SZEDER Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9a7a9ed commit b16a827

File tree

4 files changed

+100
-16
lines changed

4 files changed

+100
-16
lines changed

bloom.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
222222
diff_tree_oid(NULL, &c->object.oid, "", &diffopt);
223223
diffcore_std(&diffopt);
224224

225-
if (diffopt.num_changes <= settings->max_changed_paths) {
225+
if (diff_queued_diff.nr <= settings->max_changed_paths) {
226226
struct hashmap pathmap;
227227
struct pathmap_hash_entry *e;
228228
struct hashmap_iter iter;
@@ -259,6 +259,12 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
259259
diff_free_filepair(diff_queued_diff.queue[i]);
260260
}
261261

262+
if (hashmap_get_size(&pathmap) > settings->max_changed_paths) {
263+
if (computed)
264+
*computed |= BLOOM_TRUNC_LARGE;
265+
goto cleanup;
266+
}
267+
262268
filter->len = (hashmap_get_size(&pathmap) * settings->bits_per_entry + BITS_PER_WORD - 1) / BITS_PER_WORD;
263269
filter->data = xcalloc(filter->len, sizeof(unsigned char));
264270

@@ -268,6 +274,7 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
268274
add_key_to_filter(&key, filter, settings);
269275
}
270276

277+
cleanup:
271278
hashmap_free_entries(&pathmap, struct pathmap_hash_entry, entry);
272279
} else {
273280
for (i = 0; i < diff_queued_diff.nr; i++)

diff.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,6 @@ struct diff_options {
287287

288288
/* If non-zero, then stop computing after this many changes. */
289289
int max_changes;
290-
/* For internal use only. */
291-
int num_changes;
292290

293291
int ita_invisible_in_index;
294292
/* white-space error highlighting */

t/t4216-log-bloom.sh

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,103 @@ test_expect_success 'persist filter settings' '
176176
grep "{\"hash_version\":1,\"num_hashes\":9,\"bits_per_entry\":15,\"max_changed_paths\":512" trace2-auto.txt
177177
'
178178

179+
test_max_changed_paths () {
180+
grep "\"max_changed_paths\":$1" $2
181+
}
182+
183+
test_filter_computed () {
184+
grep "\"key\":\"filter-computed\",\"value\":\"$1\"" $2
185+
}
186+
187+
test_filter_trunc_large () {
188+
grep "\"key\":\"filter-trunc-large\",\"value\":\"$1\"" $2
189+
}
190+
179191
test_expect_success 'correctly report changes over limit' '
180-
git init 513changes &&
192+
git init limits &&
181193
(
182-
cd 513changes &&
183-
for i in $(test_seq 1 513)
194+
cd limits &&
195+
mkdir d &&
196+
mkdir d/e &&
197+
198+
for i in $(test_seq 1 2)
184199
do
185-
echo $i >file$i.txt || return 1
200+
printf $i >d/file$i.txt &&
201+
printf $i >d/e/file$i.txt || return 1
186202
done &&
187-
git add . &&
203+
204+
mkdir mode &&
205+
printf bash >mode/script.sh &&
206+
207+
mkdir foo &&
208+
touch foo/bar &&
209+
touch foo.txt &&
210+
211+
git add d foo foo.txt mode &&
188212
git commit -m "files" &&
189-
git commit-graph write --reachable --changed-paths &&
190-
for i in $(test_seq 1 513)
213+
214+
# Commit has 7 file and 4 directory adds
215+
GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS=10 \
216+
GIT_TRACE2_EVENT="$(pwd)/trace" \
217+
git commit-graph write --reachable --changed-paths &&
218+
test_max_changed_paths 10 trace &&
219+
test_filter_computed 1 trace &&
220+
test_filter_trunc_large 1 trace &&
221+
222+
for path in $(git ls-tree -r --name-only HEAD)
223+
do
224+
git -c commitGraph.readChangedPaths=false log \
225+
-- $path >expect &&
226+
git log -- $path >actual &&
227+
test_cmp expect actual || return 1
228+
done &&
229+
230+
# Make a variety of path changes
231+
printf new1 >d/e/file1.txt &&
232+
printf new2 >d/file2.txt &&
233+
rm d/e/file2.txt &&
234+
rm -r foo &&
235+
printf text >foo &&
236+
mkdir f &&
237+
printf new1 >f/file1.txt &&
238+
239+
# including a mode-only change (counts as modified)
240+
git update-index --chmod=+x mode/script.sh &&
241+
242+
git add foo d f &&
243+
git commit -m "complicated" &&
244+
245+
# start from scratch and rebuild
246+
rm -f .git/objects/info/commit-graph &&
247+
GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS=10 \
248+
GIT_TRACE2_EVENT="$(pwd)/trace-edit" \
249+
git commit-graph write --reachable --changed-paths &&
250+
test_max_changed_paths 10 trace-edit &&
251+
test_filter_computed 2 trace-edit &&
252+
test_filter_trunc_large 2 trace-edit &&
253+
254+
for path in $(git ls-tree -r --name-only HEAD)
255+
do
256+
git -c commitGraph.readChangedPaths=false log \
257+
-- $path >expect &&
258+
git log -- $path >actual &&
259+
test_cmp expect actual || return 1
260+
done &&
261+
262+
# start from scratch and rebuild
263+
rm -f .git/objects/info/commit-graph &&
264+
GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS=11 \
265+
GIT_TRACE2_EVENT="$(pwd)/trace-update" \
266+
git commit-graph write --reachable --changed-paths &&
267+
test_max_changed_paths 11 trace-update &&
268+
test_filter_computed 2 trace-update &&
269+
test_filter_trunc_large 0 trace-update &&
270+
271+
for path in $(git ls-tree -r --name-only HEAD)
191272
do
192-
git -c core.commitGraph=false log -- file$i.txt >expect &&
193-
git log -- file$i.txt >actual &&
273+
git -c commitGraph.readChangedPaths=false log \
274+
-- $path >expect &&
275+
git log -- $path >actual &&
194276
test_cmp expect actual || return 1
195277
done
196278
)

tree-diff.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ static struct combine_diff_path *ll_diff_tree_paths(
434434
if (diff_can_quit_early(opt))
435435
break;
436436

437-
if (opt->max_changes && opt->num_changes > opt->max_changes)
437+
if (opt->max_changes && diff_queued_diff.nr > opt->max_changes)
438438
break;
439439

440440
if (opt->pathspec.nr) {
@@ -521,7 +521,6 @@ static struct combine_diff_path *ll_diff_tree_paths(
521521

522522
/* t↓ */
523523
update_tree_entry(&t);
524-
opt->num_changes++;
525524
}
526525

527526
/* t > p[imin] */
@@ -539,7 +538,6 @@ static struct combine_diff_path *ll_diff_tree_paths(
539538
skip_emit_tp:
540539
/* ∀ pi=p[imin] pi↓ */
541540
update_tp_entries(tp, nparent);
542-
opt->num_changes++;
543541
}
544542
}
545543

@@ -557,7 +555,6 @@ struct combine_diff_path *diff_tree_paths(
557555
const struct object_id **parents_oid, int nparent,
558556
struct strbuf *base, struct diff_options *opt)
559557
{
560-
opt->num_changes = 0;
561558
p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt);
562559

563560
/*

0 commit comments

Comments
 (0)