Skip to content

Commit 65c1a28

Browse files
derrickstoleegitster
authored andcommitted
bloom: de-duplicate directory entries
When computing a changed-path Bloom filter, we need to take the files that changed from the diff computation and extract the parent directories. That way, a directory pathspec such as "Documentation" could match commits that change "Documentation/git.txt". However, the current code does a poor job of this process. The paths are added to a hashmap, but we do not check if an entry already exists with that path. This can create many duplicate entries and cause the filter to have a much larger length than it should. This means that the filter is more sparse than intended, which helps the false positive rate, but wastes a lot of space. Properly use hashmap_get() before hashmap_add(). Also be sure to include a comparison function so these can be matched correctly. This has an effect on a test in t0095-bloom.sh. This makes sense, there are ten changes inside "smallDir" so the total number of paths in the filter should be 11. This would result in 11 * 10 bits required, and with 8 bits per byte, this results in 14 bytes. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8809328 commit 65c1a28

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

bloom.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ void init_bloom_filters(void)
158158
init_bloom_filter_slab(&bloom_filters);
159159
}
160160

161+
static int pathmap_cmp(const void *hashmap_cmp_fn_data,
162+
const struct hashmap_entry *eptr,
163+
const struct hashmap_entry *entry_or_key,
164+
const void *keydata)
165+
{
166+
const struct pathmap_hash_entry *e1, *e2;
167+
168+
e1 = container_of(eptr, const struct pathmap_hash_entry, entry);
169+
e2 = container_of(entry_or_key, const struct pathmap_hash_entry, entry);
170+
171+
return strcmp(e1->path, e2->path);
172+
}
173+
161174
struct bloom_filter *get_bloom_filter(struct repository *r,
162175
struct commit *c,
163176
int compute_if_not_present)
@@ -206,25 +219,29 @@ struct bloom_filter *get_bloom_filter(struct repository *r,
206219
struct hashmap pathmap;
207220
struct pathmap_hash_entry *e;
208221
struct hashmap_iter iter;
209-
hashmap_init(&pathmap, NULL, NULL, 0);
222+
hashmap_init(&pathmap, pathmap_cmp, NULL, 0);
210223

211224
for (i = 0; i < diff_queued_diff.nr; i++) {
212225
const char *path = diff_queued_diff.queue[i]->two->path;
213226

214227
/*
215-
* Add each leading directory of the changed file, i.e. for
216-
* 'dir/subdir/file' add 'dir' and 'dir/subdir' as well, so
217-
* the Bloom filter could be used to speed up commands like
218-
* 'git log dir/subdir', too.
219-
*
220-
* Note that directories are added without the trailing '/'.
221-
*/
228+
* Add each leading directory of the changed file, i.e. for
229+
* 'dir/subdir/file' add 'dir' and 'dir/subdir' as well, so
230+
* the Bloom filter could be used to speed up commands like
231+
* 'git log dir/subdir', too.
232+
*
233+
* Note that directories are added without the trailing '/'.
234+
*/
222235
do {
223236
char *last_slash = strrchr(path, '/');
224237

225238
FLEX_ALLOC_STR(e, path, path);
226239
hashmap_entry_init(&e->entry, strhash(path));
227-
hashmap_add(&pathmap, &e->entry);
240+
241+
if (!hashmap_get(&pathmap, &e->entry, NULL))
242+
hashmap_add(&pathmap, &e->entry);
243+
else
244+
free(e);
228245

229246
if (!last_slash)
230247
last_slash = (char*)path;

t/t0095-bloom.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ test_expect_success 'get bloom filter for commit with 10 changes' '
8989
git add smallDir &&
9090
git commit -m "commit with 10 changes" &&
9191
cat >expect <<-\EOF &&
92-
Filter_Length:25
93-
Filter_Data:82|a0|65|47|0c|92|90|c0|a1|40|02|a0|e2|40|e0|04|0a|9a|66|cf|80|19|85|42|23|
92+
Filter_Length:14
93+
Filter_Data:02|b3|c4|a0|34|e7|fe|eb|cb|47|fe|a0|e8|72|
9494
EOF
9595
test-tool bloom get_filter_for_commit "$(git rev-parse HEAD)" >actual &&
9696
test_cmp expect actual

0 commit comments

Comments
 (0)