1 #include "git-compat-util.h"
6 #include "commit-graph.h"
8 #include "commit-slab.h"
10 define_commit_slab(bloom_filter_slab
, struct bloom_filter
);
12 static struct bloom_filter_slab bloom_filters
;
14 struct pathmap_hash_entry
{
15 struct hashmap_entry entry
;
16 const char path
[FLEX_ARRAY
];
19 static uint32_t rotate_left(uint32_t value
, int32_t count
)
21 uint32_t mask
= 8 * sizeof(uint32_t) - 1;
23 return ((value
<< count
) | (value
>> ((-count
) & mask
)));
26 static inline unsigned char get_bitmask(uint32_t pos
)
28 return ((unsigned char)1) << (pos
& (BITS_PER_WORD
- 1));
31 static int check_bloom_offset(struct commit_graph
*g
, uint32_t pos
,
35 * Note that we allow offsets equal to the data size, which would set
36 * our pointers at one past the end of the chunk memory. This is
37 * necessary because the on-disk index points to the end of the
38 * entries (so we can compute size by comparing adjacent ones). And
39 * naturally the final entry's end is one-past-the-end of the chunk.
41 if (offset
<= g
->chunk_bloom_data_size
- BLOOMDATA_CHUNK_HEADER_SIZE
)
44 warning("ignoring out-of-range offset (%"PRIuMAX
") for changed-path"
45 " filter at pos %"PRIuMAX
" of %s (chunk size: %"PRIuMAX
")",
46 (uintmax_t)offset
, (uintmax_t)pos
,
47 g
->filename
, (uintmax_t)g
->chunk_bloom_data_size
);
51 static int load_bloom_filter_from_graph(struct commit_graph
*g
,
52 struct bloom_filter
*filter
,
55 uint32_t lex_pos
, start_index
, end_index
;
57 while (graph_pos
< g
->num_commits_in_base
)
60 /* The commit graph commit 'c' lives in doesn't carry Bloom filters. */
61 if (!g
->chunk_bloom_indexes
)
64 lex_pos
= graph_pos
- g
->num_commits_in_base
;
66 end_index
= get_be32(g
->chunk_bloom_indexes
+ 4 * lex_pos
);
69 start_index
= get_be32(g
->chunk_bloom_indexes
+ 4 * (lex_pos
- 1));
73 if (check_bloom_offset(g
, lex_pos
, end_index
) < 0 ||
74 check_bloom_offset(g
, lex_pos
- 1, start_index
) < 0)
77 if (end_index
< start_index
) {
78 warning("ignoring decreasing changed-path index offsets"
79 " (%"PRIuMAX
" > %"PRIuMAX
") for positions"
80 " %"PRIuMAX
" and %"PRIuMAX
" of %s",
81 (uintmax_t)start_index
, (uintmax_t)end_index
,
82 (uintmax_t)(lex_pos
-1), (uintmax_t)lex_pos
,
87 filter
->len
= end_index
- start_index
;
88 filter
->data
= (unsigned char *)(g
->chunk_bloom_data
+
89 sizeof(unsigned char) * start_index
+
90 BLOOMDATA_CHUNK_HEADER_SIZE
);
96 * Calculate the murmur3 32-bit hash value for the given data
97 * using the given seed.
98 * Produces a uniformly distributed hash value.
99 * Not considered to be cryptographically secure.
100 * Implemented as described in https://en.wikipedia.org/wiki/MurmurHash#Algorithm
102 uint32_t murmur3_seeded(uint32_t seed
, const char *data
, size_t len
)
104 const uint32_t c1
= 0xcc9e2d51;
105 const uint32_t c2
= 0x1b873593;
106 const uint32_t r1
= 15;
107 const uint32_t r2
= 13;
108 const uint32_t m
= 5;
109 const uint32_t n
= 0xe6546b64;
114 int len4
= len
/ sizeof(uint32_t);
117 for (i
= 0; i
< len4
; i
++) {
118 uint32_t byte1
= (uint32_t)data
[4*i
];
119 uint32_t byte2
= ((uint32_t)data
[4*i
+ 1]) << 8;
120 uint32_t byte3
= ((uint32_t)data
[4*i
+ 2]) << 16;
121 uint32_t byte4
= ((uint32_t)data
[4*i
+ 3]) << 24;
122 k
= byte1
| byte2
| byte3
| byte4
;
124 k
= rotate_left(k
, r1
);
128 seed
= rotate_left(seed
, r2
) * m
+ n
;
131 tail
= (data
+ len4
* sizeof(uint32_t));
133 switch (len
& (sizeof(uint32_t) - 1)) {
135 k1
^= ((uint32_t)tail
[2]) << 16;
138 k1
^= ((uint32_t)tail
[1]) << 8;
141 k1
^= ((uint32_t)tail
[0]) << 0;
143 k1
= rotate_left(k1
, r1
);
149 seed
^= (uint32_t)len
;
150 seed
^= (seed
>> 16);
152 seed
^= (seed
>> 13);
154 seed
^= (seed
>> 16);
159 void fill_bloom_key(const char *data
,
161 struct bloom_key
*key
,
162 const struct bloom_filter_settings
*settings
)
165 const uint32_t seed0
= 0x293ae76f;
166 const uint32_t seed1
= 0x7e646e2c;
167 const uint32_t hash0
= murmur3_seeded(seed0
, data
, len
);
168 const uint32_t hash1
= murmur3_seeded(seed1
, data
, len
);
170 key
->hashes
= (uint32_t *)xcalloc(settings
->num_hashes
, sizeof(uint32_t));
171 for (i
= 0; i
< settings
->num_hashes
; i
++)
172 key
->hashes
[i
] = hash0
+ i
* hash1
;
175 void clear_bloom_key(struct bloom_key
*key
)
177 FREE_AND_NULL(key
->hashes
);
180 void add_key_to_filter(const struct bloom_key
*key
,
181 struct bloom_filter
*filter
,
182 const struct bloom_filter_settings
*settings
)
185 uint64_t mod
= filter
->len
* BITS_PER_WORD
;
187 for (i
= 0; i
< settings
->num_hashes
; i
++) {
188 uint64_t hash_mod
= key
->hashes
[i
] % mod
;
189 uint64_t block_pos
= hash_mod
/ BITS_PER_WORD
;
191 filter
->data
[block_pos
] |= get_bitmask(hash_mod
);
195 void init_bloom_filters(void)
197 init_bloom_filter_slab(&bloom_filters
);
200 static int pathmap_cmp(const void *hashmap_cmp_fn_data UNUSED
,
201 const struct hashmap_entry
*eptr
,
202 const struct hashmap_entry
*entry_or_key
,
203 const void *keydata UNUSED
)
205 const struct pathmap_hash_entry
*e1
, *e2
;
207 e1
= container_of(eptr
, const struct pathmap_hash_entry
, entry
);
208 e2
= container_of(entry_or_key
, const struct pathmap_hash_entry
, entry
);
210 return strcmp(e1
->path
, e2
->path
);
213 static void init_truncated_large_filter(struct bloom_filter
*filter
)
215 filter
->data
= xmalloc(1);
216 filter
->data
[0] = 0xFF;
220 struct bloom_filter
*get_or_compute_bloom_filter(struct repository
*r
,
222 int compute_if_not_present
,
223 const struct bloom_filter_settings
*settings
,
224 enum bloom_filter_computed
*computed
)
226 struct bloom_filter
*filter
;
228 struct diff_options diffopt
;
231 *computed
= BLOOM_NOT_COMPUTED
;
233 if (!bloom_filters
.slab_size
)
236 filter
= bloom_filter_slab_at(&bloom_filters
, c
);
240 if (repo_find_commit_pos_in_graph(r
, c
, &graph_pos
))
241 load_bloom_filter_from_graph(r
->objects
->commit_graph
,
245 if (filter
->data
&& filter
->len
)
247 if (!compute_if_not_present
)
250 repo_diff_setup(r
, &diffopt
);
251 diffopt
.flags
.recursive
= 1;
252 diffopt
.detect_rename
= 0;
253 diffopt
.max_changes
= settings
->max_changed_paths
;
254 diff_setup_done(&diffopt
);
256 /* ensure commit is parsed so we have parent information */
257 repo_parse_commit(r
, c
);
260 diff_tree_oid(&c
->parents
->item
->object
.oid
, &c
->object
.oid
, "", &diffopt
);
262 diff_tree_oid(NULL
, &c
->object
.oid
, "", &diffopt
);
263 diffcore_std(&diffopt
);
265 if (diff_queued_diff
.nr
<= settings
->max_changed_paths
) {
266 struct hashmap pathmap
= HASHMAP_INIT(pathmap_cmp
, NULL
);
267 struct pathmap_hash_entry
*e
;
268 struct hashmap_iter iter
;
270 for (i
= 0; i
< diff_queued_diff
.nr
; i
++) {
271 const char *path
= diff_queued_diff
.queue
[i
]->two
->path
;
274 * Add each leading directory of the changed file, i.e. for
275 * 'dir/subdir/file' add 'dir' and 'dir/subdir' as well, so
276 * the Bloom filter could be used to speed up commands like
277 * 'git log dir/subdir', too.
279 * Note that directories are added without the trailing '/'.
282 char *last_slash
= strrchr(path
, '/');
284 FLEX_ALLOC_STR(e
, path
, path
);
285 hashmap_entry_init(&e
->entry
, strhash(path
));
287 if (!hashmap_get(&pathmap
, &e
->entry
, NULL
))
288 hashmap_add(&pathmap
, &e
->entry
);
293 last_slash
= (char*)path
;
298 diff_free_filepair(diff_queued_diff
.queue
[i
]);
301 if (hashmap_get_size(&pathmap
) > settings
->max_changed_paths
) {
302 init_truncated_large_filter(filter
);
304 *computed
|= BLOOM_TRUNC_LARGE
;
308 filter
->len
= (hashmap_get_size(&pathmap
) * settings
->bits_per_entry
+ BITS_PER_WORD
- 1) / BITS_PER_WORD
;
311 *computed
|= BLOOM_TRUNC_EMPTY
;
314 CALLOC_ARRAY(filter
->data
, filter
->len
);
316 hashmap_for_each_entry(&pathmap
, &iter
, e
, entry
) {
317 struct bloom_key key
;
318 fill_bloom_key(e
->path
, strlen(e
->path
), &key
, settings
);
319 add_key_to_filter(&key
, filter
, settings
);
320 clear_bloom_key(&key
);
324 hashmap_clear_and_free(&pathmap
, struct pathmap_hash_entry
, entry
);
326 for (i
= 0; i
< diff_queued_diff
.nr
; i
++)
327 diff_free_filepair(diff_queued_diff
.queue
[i
]);
328 init_truncated_large_filter(filter
);
331 *computed
|= BLOOM_TRUNC_LARGE
;
335 *computed
|= BLOOM_COMPUTED
;
337 free(diff_queued_diff
.queue
);
338 DIFF_QUEUE_CLEAR(&diff_queued_diff
);
343 int bloom_filter_contains(const struct bloom_filter
*filter
,
344 const struct bloom_key
*key
,
345 const struct bloom_filter_settings
*settings
)
348 uint64_t mod
= filter
->len
* BITS_PER_WORD
;
353 for (i
= 0; i
< settings
->num_hashes
; i
++) {
354 uint64_t hash_mod
= key
->hashes
[i
] % mod
;
355 uint64_t block_pos
= hash_mod
/ BITS_PER_WORD
;
356 if (!(filter
->data
[block_pos
] & get_bitmask(hash_mod
)))