]>
Commit | Line | Data |
---|---|---|
f52207a4 GS |
1 | #include "git-compat-util.h" |
2 | #include "bloom.h" | |
ed591feb GS |
3 | #include "diff.h" |
4 | #include "diffcore.h" | |
5 | #include "revision.h" | |
6 | #include "hashmap.h" | |
1217c03e GS |
7 | #include "commit-graph.h" |
8 | #include "commit.h" | |
ed591feb GS |
9 | |
10 | define_commit_slab(bloom_filter_slab, struct bloom_filter); | |
11 | ||
066b70ae | 12 | static struct bloom_filter_slab bloom_filters; |
ed591feb GS |
13 | |
14 | struct pathmap_hash_entry { | |
15 | struct hashmap_entry entry; | |
16 | const char path[FLEX_ARRAY]; | |
17 | }; | |
f52207a4 GS |
18 | |
19 | static uint32_t rotate_left(uint32_t value, int32_t count) | |
20 | { | |
21 | uint32_t mask = 8 * sizeof(uint32_t) - 1; | |
22 | count &= mask; | |
23 | return ((value << count) | (value >> ((-count) & mask))); | |
24 | } | |
25 | ||
f1294eaf GS |
26 | static inline unsigned char get_bitmask(uint32_t pos) |
27 | { | |
28 | return ((unsigned char)1) << (pos & (BITS_PER_WORD - 1)); | |
29 | } | |
30 | ||
1217c03e | 31 | static int load_bloom_filter_from_graph(struct commit_graph *g, |
eb591e42 DS |
32 | struct bloom_filter *filter, |
33 | struct commit *c) | |
1217c03e GS |
34 | { |
35 | uint32_t lex_pos, start_index, end_index; | |
c752ad09 | 36 | uint32_t graph_pos = commit_graph_position(c); |
1217c03e | 37 | |
c752ad09 | 38 | while (graph_pos < g->num_commits_in_base) |
1217c03e GS |
39 | g = g->base_graph; |
40 | ||
41 | /* The commit graph commit 'c' lives in doesn't carry bloom filters. */ | |
42 | if (!g->chunk_bloom_indexes) | |
43 | return 0; | |
44 | ||
c752ad09 | 45 | lex_pos = graph_pos - g->num_commits_in_base; |
1217c03e GS |
46 | |
47 | end_index = get_be32(g->chunk_bloom_indexes + 4 * lex_pos); | |
48 | ||
49 | if (lex_pos > 0) | |
50 | start_index = get_be32(g->chunk_bloom_indexes + 4 * (lex_pos - 1)); | |
51 | else | |
52 | start_index = 0; | |
53 | ||
54 | filter->len = end_index - start_index; | |
55 | filter->data = (unsigned char *)(g->chunk_bloom_data + | |
56 | sizeof(unsigned char) * start_index + | |
57 | BLOOMDATA_CHUNK_HEADER_SIZE); | |
58 | ||
59 | return 1; | |
60 | } | |
61 | ||
f52207a4 GS |
62 | /* |
63 | * Calculate the murmur3 32-bit hash value for the given data | |
64 | * using the given seed. | |
65 | * Produces a uniformly distributed hash value. | |
66 | * Not considered to be cryptographically secure. | |
67 | * Implemented as described in https://en.wikipedia.org/wiki/MurmurHash#Algorithm | |
68 | */ | |
69 | uint32_t murmur3_seeded(uint32_t seed, const char *data, size_t len) | |
70 | { | |
71 | const uint32_t c1 = 0xcc9e2d51; | |
72 | const uint32_t c2 = 0x1b873593; | |
73 | const uint32_t r1 = 15; | |
74 | const uint32_t r2 = 13; | |
75 | const uint32_t m = 5; | |
76 | const uint32_t n = 0xe6546b64; | |
77 | int i; | |
78 | uint32_t k1 = 0; | |
79 | const char *tail; | |
80 | ||
81 | int len4 = len / sizeof(uint32_t); | |
82 | ||
83 | uint32_t k; | |
84 | for (i = 0; i < len4; i++) { | |
85 | uint32_t byte1 = (uint32_t)data[4*i]; | |
86 | uint32_t byte2 = ((uint32_t)data[4*i + 1]) << 8; | |
87 | uint32_t byte3 = ((uint32_t)data[4*i + 2]) << 16; | |
88 | uint32_t byte4 = ((uint32_t)data[4*i + 3]) << 24; | |
89 | k = byte1 | byte2 | byte3 | byte4; | |
90 | k *= c1; | |
91 | k = rotate_left(k, r1); | |
92 | k *= c2; | |
93 | ||
94 | seed ^= k; | |
95 | seed = rotate_left(seed, r2) * m + n; | |
96 | } | |
97 | ||
98 | tail = (data + len4 * sizeof(uint32_t)); | |
99 | ||
100 | switch (len & (sizeof(uint32_t) - 1)) { | |
101 | case 3: | |
102 | k1 ^= ((uint32_t)tail[2]) << 16; | |
103 | /*-fallthrough*/ | |
104 | case 2: | |
105 | k1 ^= ((uint32_t)tail[1]) << 8; | |
106 | /*-fallthrough*/ | |
107 | case 1: | |
108 | k1 ^= ((uint32_t)tail[0]) << 0; | |
109 | k1 *= c1; | |
110 | k1 = rotate_left(k1, r1); | |
111 | k1 *= c2; | |
112 | seed ^= k1; | |
113 | break; | |
114 | } | |
115 | ||
116 | seed ^= (uint32_t)len; | |
117 | seed ^= (seed >> 16); | |
118 | seed *= 0x85ebca6b; | |
119 | seed ^= (seed >> 13); | |
120 | seed *= 0xc2b2ae35; | |
121 | seed ^= (seed >> 16); | |
122 | ||
123 | return seed; | |
f1294eaf GS |
124 | } |
125 | ||
126 | void fill_bloom_key(const char *data, | |
eb591e42 DS |
127 | size_t len, |
128 | struct bloom_key *key, | |
129 | const struct bloom_filter_settings *settings) | |
f1294eaf GS |
130 | { |
131 | int i; | |
132 | const uint32_t seed0 = 0x293ae76f; | |
133 | const uint32_t seed1 = 0x7e646e2c; | |
134 | const uint32_t hash0 = murmur3_seeded(seed0, data, len); | |
135 | const uint32_t hash1 = murmur3_seeded(seed1, data, len); | |
136 | ||
137 | key->hashes = (uint32_t *)xcalloc(settings->num_hashes, sizeof(uint32_t)); | |
138 | for (i = 0; i < settings->num_hashes; i++) | |
139 | key->hashes[i] = hash0 + i * hash1; | |
140 | } | |
141 | ||
f32dde8c DS |
142 | void clear_bloom_key(struct bloom_key *key) |
143 | { | |
144 | FREE_AND_NULL(key->hashes); | |
145 | } | |
146 | ||
f1294eaf | 147 | void add_key_to_filter(const struct bloom_key *key, |
eb591e42 DS |
148 | struct bloom_filter *filter, |
149 | const struct bloom_filter_settings *settings) | |
f1294eaf GS |
150 | { |
151 | int i; | |
152 | uint64_t mod = filter->len * BITS_PER_WORD; | |
153 | ||
154 | for (i = 0; i < settings->num_hashes; i++) { | |
155 | uint64_t hash_mod = key->hashes[i] % mod; | |
156 | uint64_t block_pos = hash_mod / BITS_PER_WORD; | |
157 | ||
158 | filter->data[block_pos] |= get_bitmask(hash_mod); | |
159 | } | |
160 | } | |
ed591feb GS |
161 | |
162 | void init_bloom_filters(void) | |
163 | { | |
164 | init_bloom_filter_slab(&bloom_filters); | |
165 | } | |
166 | ||
65c1a28b DS |
167 | static int pathmap_cmp(const void *hashmap_cmp_fn_data, |
168 | const struct hashmap_entry *eptr, | |
169 | const struct hashmap_entry *entry_or_key, | |
170 | const void *keydata) | |
171 | { | |
172 | const struct pathmap_hash_entry *e1, *e2; | |
173 | ||
174 | e1 = container_of(eptr, const struct pathmap_hash_entry, entry); | |
175 | e2 = container_of(entry_or_key, const struct pathmap_hash_entry, entry); | |
176 | ||
177 | return strcmp(e1->path, e2->path); | |
178 | } | |
179 | ||
ed591feb | 180 | struct bloom_filter *get_bloom_filter(struct repository *r, |
1217c03e | 181 | struct commit *c, |
eb591e42 | 182 | int compute_if_not_present) |
ed591feb GS |
183 | { |
184 | struct bloom_filter *filter; | |
185 | struct bloom_filter_settings settings = DEFAULT_BLOOM_FILTER_SETTINGS; | |
186 | int i; | |
187 | struct diff_options diffopt; | |
e3696980 | 188 | int max_changes = 512; |
ed591feb GS |
189 | |
190 | if (bloom_filters.slab_size == 0) | |
191 | return NULL; | |
192 | ||
193 | filter = bloom_filter_slab_at(&bloom_filters, c); | |
194 | ||
1217c03e GS |
195 | if (!filter->data) { |
196 | load_commit_graph_info(r, c); | |
c49c82aa | 197 | if (commit_graph_position(c) != COMMIT_NOT_FROM_GRAPH && |
1217c03e GS |
198 | r->objects->commit_graph->chunk_bloom_indexes) { |
199 | if (load_bloom_filter_from_graph(r->objects->commit_graph, filter, c)) | |
200 | return filter; | |
201 | else | |
202 | return NULL; | |
203 | } | |
204 | } | |
205 | ||
206 | if (filter->data || !compute_if_not_present) | |
207 | return filter; | |
208 | ||
ed591feb GS |
209 | repo_diff_setup(r, &diffopt); |
210 | diffopt.flags.recursive = 1; | |
caf388ca | 211 | diffopt.detect_rename = 0; |
e3696980 | 212 | diffopt.max_changes = max_changes; |
ed591feb GS |
213 | diff_setup_done(&diffopt); |
214 | ||
891c17c9 DS |
215 | /* ensure commit is parsed so we have parent information */ |
216 | repo_parse_commit(r, c); | |
217 | ||
ed591feb GS |
218 | if (c->parents) |
219 | diff_tree_oid(&c->parents->item->object.oid, &c->object.oid, "", &diffopt); | |
220 | else | |
221 | diff_tree_oid(NULL, &c->object.oid, "", &diffopt); | |
222 | diffcore_std(&diffopt); | |
223 | ||
2f6775f0 | 224 | if (diffopt.num_changes <= max_changes) { |
ed591feb GS |
225 | struct hashmap pathmap; |
226 | struct pathmap_hash_entry *e; | |
227 | struct hashmap_iter iter; | |
65c1a28b | 228 | hashmap_init(&pathmap, pathmap_cmp, NULL, 0); |
ed591feb GS |
229 | |
230 | for (i = 0; i < diff_queued_diff.nr; i++) { | |
231 | const char *path = diff_queued_diff.queue[i]->two->path; | |
232 | ||
233 | /* | |
65c1a28b DS |
234 | * Add each leading directory of the changed file, i.e. for |
235 | * 'dir/subdir/file' add 'dir' and 'dir/subdir' as well, so | |
236 | * the Bloom filter could be used to speed up commands like | |
237 | * 'git log dir/subdir', too. | |
238 | * | |
239 | * Note that directories are added without the trailing '/'. | |
240 | */ | |
ed591feb GS |
241 | do { |
242 | char *last_slash = strrchr(path, '/'); | |
243 | ||
244 | FLEX_ALLOC_STR(e, path, path); | |
245 | hashmap_entry_init(&e->entry, strhash(path)); | |
65c1a28b DS |
246 | |
247 | if (!hashmap_get(&pathmap, &e->entry, NULL)) | |
248 | hashmap_add(&pathmap, &e->entry); | |
249 | else | |
250 | free(e); | |
ed591feb GS |
251 | |
252 | if (!last_slash) | |
253 | last_slash = (char*)path; | |
254 | *last_slash = '\0'; | |
255 | ||
256 | } while (*path); | |
257 | ||
258 | diff_free_filepair(diff_queued_diff.queue[i]); | |
259 | } | |
260 | ||
261 | filter->len = (hashmap_get_size(&pathmap) * settings.bits_per_entry + BITS_PER_WORD - 1) / BITS_PER_WORD; | |
262 | filter->data = xcalloc(filter->len, sizeof(unsigned char)); | |
263 | ||
264 | hashmap_for_each_entry(&pathmap, &iter, e, entry) { | |
265 | struct bloom_key key; | |
266 | fill_bloom_key(e->path, strlen(e->path), &key, &settings); | |
267 | add_key_to_filter(&key, filter, &settings); | |
268 | } | |
269 | ||
270 | hashmap_free_entries(&pathmap, struct pathmap_hash_entry, entry); | |
271 | } else { | |
272 | for (i = 0; i < diff_queued_diff.nr; i++) | |
273 | diff_free_filepair(diff_queued_diff.queue[i]); | |
274 | filter->data = NULL; | |
275 | filter->len = 0; | |
276 | } | |
277 | ||
278 | free(diff_queued_diff.queue); | |
279 | DIFF_QUEUE_CLEAR(&diff_queued_diff); | |
280 | ||
281 | return filter; | |
282 | } | |
a56b9464 GS |
283 | |
284 | int bloom_filter_contains(const struct bloom_filter *filter, | |
285 | const struct bloom_key *key, | |
286 | const struct bloom_filter_settings *settings) | |
287 | { | |
288 | int i; | |
289 | uint64_t mod = filter->len * BITS_PER_WORD; | |
290 | ||
291 | if (!mod) | |
292 | return -1; | |
293 | ||
294 | for (i = 0; i < settings->num_hashes; i++) { | |
295 | uint64_t hash_mod = key->hashes[i] % mod; | |
296 | uint64_t block_pos = hash_mod / BITS_PER_WORD; | |
297 | if (!(filter->data[block_pos] & get_bitmask(hash_mod))) | |
298 | return 0; | |
299 | } | |
300 | ||
301 | return 1; | |
066b70ae | 302 | } |