1 #include "git-compat-util.h"
7 #include "commit-graph.h"
9 #include "commit-slab.h"
11 define_commit_slab(bloom_filter_slab
, struct bloom_filter
);
13 static struct bloom_filter_slab bloom_filters
;
15 struct pathmap_hash_entry
{
16 struct hashmap_entry entry
;
17 const char path
[FLEX_ARRAY
];
20 static uint32_t rotate_left(uint32_t value
, int32_t count
)
22 uint32_t mask
= 8 * sizeof(uint32_t) - 1;
24 return ((value
<< count
) | (value
>> ((-count
) & mask
)));
27 static inline unsigned char get_bitmask(uint32_t pos
)
29 return ((unsigned char)1) << (pos
& (BITS_PER_WORD
- 1));
32 static int check_bloom_offset(struct commit_graph
*g
, uint32_t pos
,
36 * Note that we allow offsets equal to the data size, which would set
37 * our pointers at one past the end of the chunk memory. This is
38 * necessary because the on-disk index points to the end of the
39 * entries (so we can compute size by comparing adjacent ones). And
40 * naturally the final entry's end is one-past-the-end of the chunk.
42 if (offset
<= g
->chunk_bloom_data_size
- BLOOMDATA_CHUNK_HEADER_SIZE
)
45 warning("ignoring out-of-range offset (%"PRIuMAX
") for changed-path"
46 " filter at pos %"PRIuMAX
" of %s (chunk size: %"PRIuMAX
")",
47 (uintmax_t)offset
, (uintmax_t)pos
,
48 g
->filename
, (uintmax_t)g
->chunk_bloom_data_size
);
52 static int load_bloom_filter_from_graph(struct commit_graph
*g
,
53 struct bloom_filter
*filter
,
56 uint32_t lex_pos
, start_index
, end_index
;
58 while (graph_pos
< g
->num_commits_in_base
)
61 /* The commit graph commit 'c' lives in doesn't carry Bloom filters. */
62 if (!g
->chunk_bloom_indexes
)
65 lex_pos
= graph_pos
- g
->num_commits_in_base
;
67 end_index
= get_be32(g
->chunk_bloom_indexes
+ 4 * lex_pos
);
70 start_index
= get_be32(g
->chunk_bloom_indexes
+ 4 * (lex_pos
- 1));
74 if (check_bloom_offset(g
, lex_pos
, end_index
) < 0 ||
75 check_bloom_offset(g
, lex_pos
- 1, start_index
) < 0)
78 if (end_index
< start_index
) {
79 warning("ignoring decreasing changed-path index offsets"
80 " (%"PRIuMAX
" > %"PRIuMAX
") for positions"
81 " %"PRIuMAX
" and %"PRIuMAX
" of %s",
82 (uintmax_t)start_index
, (uintmax_t)end_index
,
83 (uintmax_t)(lex_pos
-1), (uintmax_t)lex_pos
,
88 filter
->len
= end_index
- start_index
;
89 filter
->data
= (unsigned char *)(g
->chunk_bloom_data
+
90 sizeof(unsigned char) * start_index
+
91 BLOOMDATA_CHUNK_HEADER_SIZE
);
97 * Calculate the murmur3 32-bit hash value for the given data
98 * using the given seed.
99 * Produces a uniformly distributed hash value.
100 * Not considered to be cryptographically secure.
101 * Implemented as described in https://en.wikipedia.org/wiki/MurmurHash#Algorithm
103 uint32_t murmur3_seeded(uint32_t seed
, const char *data
, size_t len
)
105 const uint32_t c1
= 0xcc9e2d51;
106 const uint32_t c2
= 0x1b873593;
107 const uint32_t r1
= 15;
108 const uint32_t r2
= 13;
109 const uint32_t m
= 5;
110 const uint32_t n
= 0xe6546b64;
115 int len4
= len
/ sizeof(uint32_t);
118 for (i
= 0; i
< len4
; i
++) {
119 uint32_t byte1
= (uint32_t)data
[4*i
];
120 uint32_t byte2
= ((uint32_t)data
[4*i
+ 1]) << 8;
121 uint32_t byte3
= ((uint32_t)data
[4*i
+ 2]) << 16;
122 uint32_t byte4
= ((uint32_t)data
[4*i
+ 3]) << 24;
123 k
= byte1
| byte2
| byte3
| byte4
;
125 k
= rotate_left(k
, r1
);
129 seed
= rotate_left(seed
, r2
) * m
+ n
;
132 tail
= (data
+ len4
* sizeof(uint32_t));
134 switch (len
& (sizeof(uint32_t) - 1)) {
136 k1
^= ((uint32_t)tail
[2]) << 16;
139 k1
^= ((uint32_t)tail
[1]) << 8;
142 k1
^= ((uint32_t)tail
[0]) << 0;
144 k1
= rotate_left(k1
, r1
);
150 seed
^= (uint32_t)len
;
151 seed
^= (seed
>> 16);
153 seed
^= (seed
>> 13);
155 seed
^= (seed
>> 16);
160 void fill_bloom_key(const char *data
,
162 struct bloom_key
*key
,
163 const struct bloom_filter_settings
*settings
)
166 const uint32_t seed0
= 0x293ae76f;
167 const uint32_t seed1
= 0x7e646e2c;
168 const uint32_t hash0
= murmur3_seeded(seed0
, data
, len
);
169 const uint32_t hash1
= murmur3_seeded(seed1
, data
, len
);
171 key
->hashes
= (uint32_t *)xcalloc(settings
->num_hashes
, sizeof(uint32_t));
172 for (i
= 0; i
< settings
->num_hashes
; i
++)
173 key
->hashes
[i
] = hash0
+ i
* hash1
;
176 void clear_bloom_key(struct bloom_key
*key
)
178 FREE_AND_NULL(key
->hashes
);
181 void add_key_to_filter(const struct bloom_key
*key
,
182 struct bloom_filter
*filter
,
183 const struct bloom_filter_settings
*settings
)
186 uint64_t mod
= filter
->len
* BITS_PER_WORD
;
188 for (i
= 0; i
< settings
->num_hashes
; i
++) {
189 uint64_t hash_mod
= key
->hashes
[i
] % mod
;
190 uint64_t block_pos
= hash_mod
/ BITS_PER_WORD
;
192 filter
->data
[block_pos
] |= get_bitmask(hash_mod
);
196 void init_bloom_filters(void)
198 init_bloom_filter_slab(&bloom_filters
);
201 static int pathmap_cmp(const void *hashmap_cmp_fn_data UNUSED
,
202 const struct hashmap_entry
*eptr
,
203 const struct hashmap_entry
*entry_or_key
,
204 const void *keydata UNUSED
)
206 const struct pathmap_hash_entry
*e1
, *e2
;
208 e1
= container_of(eptr
, const struct pathmap_hash_entry
, entry
);
209 e2
= container_of(entry_or_key
, const struct pathmap_hash_entry
, entry
);
211 return strcmp(e1
->path
, e2
->path
);
214 static void init_truncated_large_filter(struct bloom_filter
*filter
)
216 filter
->data
= xmalloc(1);
217 filter
->data
[0] = 0xFF;
221 struct bloom_filter
*get_or_compute_bloom_filter(struct repository
*r
,
223 int compute_if_not_present
,
224 const struct bloom_filter_settings
*settings
,
225 enum bloom_filter_computed
*computed
)
227 struct bloom_filter
*filter
;
229 struct diff_options diffopt
;
232 *computed
= BLOOM_NOT_COMPUTED
;
234 if (!bloom_filters
.slab_size
)
237 filter
= bloom_filter_slab_at(&bloom_filters
, c
);
241 if (repo_find_commit_pos_in_graph(r
, c
, &graph_pos
))
242 load_bloom_filter_from_graph(r
->objects
->commit_graph
,
246 if (filter
->data
&& filter
->len
)
248 if (!compute_if_not_present
)
251 repo_diff_setup(r
, &diffopt
);
252 diffopt
.flags
.recursive
= 1;
253 diffopt
.detect_rename
= 0;
254 diffopt
.max_changes
= settings
->max_changed_paths
;
255 diff_setup_done(&diffopt
);
257 /* ensure commit is parsed so we have parent information */
258 repo_parse_commit(r
, c
);
261 diff_tree_oid(&c
->parents
->item
->object
.oid
, &c
->object
.oid
, "", &diffopt
);
263 diff_tree_oid(NULL
, &c
->object
.oid
, "", &diffopt
);
264 diffcore_std(&diffopt
);
266 if (diff_queued_diff
.nr
<= settings
->max_changed_paths
) {
267 struct hashmap pathmap
= HASHMAP_INIT(pathmap_cmp
, NULL
);
268 struct pathmap_hash_entry
*e
;
269 struct hashmap_iter iter
;
271 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
272 const char *path
= diff_queued_diff
.queue
[i
]->two
->path
;
275 * Add each leading directory of the changed file, i.e. for
276 * 'dir/subdir/file' add 'dir' and 'dir/subdir' as well, so
277 * the Bloom filter could be used to speed up commands like
278 * 'git log dir/subdir', too.
280 * Note that directories are added without the trailing '/'.
283 char *last_slash
= strrchr(path
, '/');
285 FLEX_ALLOC_STR(e
, path
, path
);
286 hashmap_entry_init(&e
->entry
, strhash(path
));
288 if (!hashmap_get(&pathmap
, &e
->entry
, NULL
))
289 hashmap_add(&pathmap
, &e
->entry
);
294 last_slash
= (char*)path
;
299 diff_free_filepair(diff_queued_diff
.queue
[i
]);
302 if (hashmap_get_size(&pathmap
) > settings
->max_changed_paths
) {
303 init_truncated_large_filter(filter
);
305 *computed
|= BLOOM_TRUNC_LARGE
;
309 filter
->len
= (hashmap_get_size(&pathmap
) * settings
->bits_per_entry
+ BITS_PER_WORD
- 1) / BITS_PER_WORD
;
312 *computed
|= BLOOM_TRUNC_EMPTY
;
315 CALLOC_ARRAY(filter
->data
, filter
->len
);
317 hashmap_for_each_entry(&pathmap
, &iter
, e
, entry
) {
318 struct bloom_key key
;
319 fill_bloom_key(e
->path
, strlen(e
->path
), &key
, settings
);
320 add_key_to_filter(&key
, filter
, settings
);
321 clear_bloom_key(&key
);
325 hashmap_clear_and_free(&pathmap
, struct pathmap_hash_entry
, entry
);
327 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
328 diff_free_filepair(diff_queued_diff
.queue
[i
]);
329 init_truncated_large_filter(filter
);
332 *computed
|= BLOOM_TRUNC_LARGE
;
336 *computed
|= BLOOM_COMPUTED
;
338 free(diff_queued_diff
.queue
);
339 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
344 int bloom_filter_contains(const struct bloom_filter
*filter
,
345 const struct bloom_key
*key
,
346 const struct bloom_filter_settings
*settings
)
349 uint64_t mod
= filter
->len
* BITS_PER_WORD
;
354 for (i
= 0; i
< settings
->num_hashes
; i
++) {
355 uint64_t hash_mod
= key
->hashes
[i
] % mod
;
356 uint64_t block_pos
= hash_mod
/ BITS_PER_WORD
;
357 if (!(filter
->data
[block_pos
] & get_bitmask(hash_mod
)))