bloom.c 8.1 KB

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