The third batch
[alt-git.git] / name-hash.c
blobd66de1cdfd563336f468817306664dea9a4a9e7a
1 /*
2 * name-hash.c
4 * Hashing names in the index state
6 * Copyright (C) 2008 Linus Torvalds
7 */
9 #define USE_THE_REPOSITORY_VARIABLE
10 #define DISABLE_SIGN_COMPARE_WARNINGS
12 #include "git-compat-util.h"
13 #include "environment.h"
14 #include "gettext.h"
15 #include "name-hash.h"
16 #include "object.h"
17 #include "read-cache-ll.h"
18 #include "thread-utils.h"
19 #include "trace.h"
20 #include "trace2.h"
21 #include "sparse-index.h"
23 struct dir_entry {
24 struct hashmap_entry ent;
25 struct dir_entry *parent;
26 int nr;
27 unsigned int namelen;
28 char name[FLEX_ARRAY];
31 static int dir_entry_cmp(const void *cmp_data UNUSED,
32 const struct hashmap_entry *eptr,
33 const struct hashmap_entry *entry_or_key,
34 const void *keydata)
36 const struct dir_entry *e1, *e2;
37 const char *name = keydata;
39 e1 = container_of(eptr, const struct dir_entry, ent);
40 e2 = container_of(entry_or_key, const struct dir_entry, ent);
42 return e1->namelen != e2->namelen || strncasecmp(e1->name,
43 name ? name : e2->name, e1->namelen);
46 static struct dir_entry *find_dir_entry__hash(struct index_state *istate,
47 const char *name, unsigned int namelen, unsigned int hash)
49 struct dir_entry key;
50 hashmap_entry_init(&key.ent, hash);
51 key.namelen = namelen;
52 return hashmap_get_entry(&istate->dir_hash, &key, ent, name);
55 static struct dir_entry *find_dir_entry(struct index_state *istate,
56 const char *name, unsigned int namelen)
58 return find_dir_entry__hash(istate, name, namelen, memihash(name, namelen));
61 static struct dir_entry *hash_dir_entry(struct index_state *istate,
62 struct cache_entry *ce, int namelen)
65 * Throw each directory component in the hash for quick lookup
66 * during a git status. Directory components are stored without their
67 * closing slash. Despite submodules being a directory, they never
68 * reach this point, because they are stored
69 * in index_state.name_hash (as ordinary cache_entries).
71 struct dir_entry *dir;
73 /* get length of parent directory */
74 while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1]))
75 namelen--;
76 if (namelen <= 0)
77 return NULL;
78 namelen--;
80 /* lookup existing entry for that directory */
81 dir = find_dir_entry(istate, ce->name, namelen);
82 if (!dir) {
83 /* not found, create it and add to hash table */
84 FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
85 hashmap_entry_init(&dir->ent, memihash(ce->name, namelen));
86 dir->namelen = namelen;
87 hashmap_add(&istate->dir_hash, &dir->ent);
89 /* recursively add missing parent directories */
90 dir->parent = hash_dir_entry(istate, ce, namelen);
92 return dir;
95 static void add_dir_entry(struct index_state *istate, struct cache_entry *ce)
97 /* Add reference to the directory entry (and parents if 0). */
98 struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
99 while (dir && !(dir->nr++))
100 dir = dir->parent;
103 static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
106 * Release reference to the directory entry. If 0, remove and continue
107 * with parent directory.
109 struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
110 while (dir && !(--dir->nr)) {
111 struct dir_entry *parent = dir->parent;
112 hashmap_remove(&istate->dir_hash, &dir->ent, NULL);
113 free(dir);
114 dir = parent;
118 static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
120 if (ce->ce_flags & CE_HASHED)
121 return;
122 ce->ce_flags |= CE_HASHED;
124 if (!S_ISSPARSEDIR(ce->ce_mode)) {
125 hashmap_entry_init(&ce->ent, memihash(ce->name, ce_namelen(ce)));
126 hashmap_add(&istate->name_hash, &ce->ent);
129 if (ignore_case)
130 add_dir_entry(istate, ce);
133 static int cache_entry_cmp(const void *cmp_data UNUSED,
134 const struct hashmap_entry *eptr,
135 const struct hashmap_entry *entry_or_key,
136 const void *remove)
138 const struct cache_entry *ce1, *ce2;
140 ce1 = container_of(eptr, const struct cache_entry, ent);
141 ce2 = container_of(entry_or_key, const struct cache_entry, ent);
144 * For remove_name_hash, find the exact entry (pointer equality); for
145 * index_file_exists, find all entries with matching hash code and
146 * decide whether the entry matches in same_name.
148 return remove ? !(ce1 == ce2) : 0;
151 static int lazy_try_threaded = 1;
152 static int lazy_nr_dir_threads;
155 * Set a minimum number of cache_entries that we will handle per
156 * thread and use that to decide how many threads to run (up to
157 * the number on the system).
159 * For guidance setting the lower per-thread bound, see:
160 * t/helper/test-lazy-init-name-hash --analyze
162 #define LAZY_THREAD_COST (2000)
165 * We use n mutexes to guard n partitions of the "istate->dir_hash"
166 * hashtable. Since "find" and "insert" operations will hash to a
167 * particular bucket and modify/search a single chain, we can say
168 * that "all chains mod n" are guarded by the same mutex -- rather
169 * than having a single mutex to guard the entire table. (This does
170 * require that we disable "rehashing" on the hashtable.)
172 * So, a larger value here decreases the probability of a collision
173 * and the time that each thread must wait for the mutex.
175 #define LAZY_MAX_MUTEX (32)
177 static pthread_mutex_t *lazy_dir_mutex_array;
180 * An array of lazy_entry items is used by the n threads in
181 * the directory parse (first) phase to (lock-free) store the
182 * intermediate results. These values are then referenced by
183 * the 2 threads in the second phase.
185 struct lazy_entry {
186 struct dir_entry *dir;
187 unsigned int hash_dir;
188 unsigned int hash_name;
192 * Decide if we want to use threads (if available) to load
193 * the hash tables. We set "lazy_nr_dir_threads" to zero when
194 * it is not worth it.
196 static int lookup_lazy_params(struct index_state *istate)
198 int nr_cpus;
200 lazy_nr_dir_threads = 0;
202 if (!lazy_try_threaded)
203 return 0;
206 * If we are respecting case, just use the original
207 * code to build the "istate->name_hash". We don't
208 * need the complexity here.
210 if (!ignore_case)
211 return 0;
213 nr_cpus = online_cpus();
214 if (nr_cpus < 2)
215 return 0;
217 if (istate->cache_nr < 2 * LAZY_THREAD_COST)
218 return 0;
220 if (istate->cache_nr < nr_cpus * LAZY_THREAD_COST)
221 nr_cpus = istate->cache_nr / LAZY_THREAD_COST;
222 lazy_nr_dir_threads = nr_cpus;
223 return lazy_nr_dir_threads;
227 * Initialize n mutexes for use when searching and inserting
228 * into "istate->dir_hash". All "dir" threads are trying
229 * to insert partial pathnames into the hash as they iterate
230 * over their portions of the index, so lock contention is
231 * high.
233 * However, the hashmap is going to put items into bucket
234 * chains based on their hash values. Use that to create n
235 * mutexes and lock on mutex[bucket(hash) % n]. This will
236 * decrease the collision rate by (hopefully) a factor of n.
238 static void init_dir_mutex(void)
240 int j;
242 CALLOC_ARRAY(lazy_dir_mutex_array, LAZY_MAX_MUTEX);
244 for (j = 0; j < LAZY_MAX_MUTEX; j++)
245 init_recursive_mutex(&lazy_dir_mutex_array[j]);
248 static void cleanup_dir_mutex(void)
250 int j;
252 for (j = 0; j < LAZY_MAX_MUTEX; j++)
253 pthread_mutex_destroy(&lazy_dir_mutex_array[j]);
255 free(lazy_dir_mutex_array);
258 static void lock_dir_mutex(int j)
260 pthread_mutex_lock(&lazy_dir_mutex_array[j]);
263 static void unlock_dir_mutex(int j)
265 pthread_mutex_unlock(&lazy_dir_mutex_array[j]);
268 static inline int compute_dir_lock_nr(
269 const struct hashmap *map,
270 unsigned int hash)
272 return hashmap_bucket(map, hash) % LAZY_MAX_MUTEX;
275 static struct dir_entry *hash_dir_entry_with_parent_and_prefix(
276 struct index_state *istate,
277 struct dir_entry *parent,
278 struct strbuf *prefix)
280 struct dir_entry *dir;
281 unsigned int hash;
282 int lock_nr;
285 * Either we have a parent directory and path with slash(es)
286 * or the directory is an immediate child of the root directory.
288 assert((parent != NULL) ^ (strchr(prefix->buf, '/') == NULL));
290 if (parent)
291 hash = memihash_cont(parent->ent.hash,
292 prefix->buf + parent->namelen,
293 prefix->len - parent->namelen);
294 else
295 hash = memihash(prefix->buf, prefix->len);
297 lock_nr = compute_dir_lock_nr(&istate->dir_hash, hash);
298 lock_dir_mutex(lock_nr);
300 dir = find_dir_entry__hash(istate, prefix->buf, prefix->len, hash);
301 if (!dir) {
302 FLEX_ALLOC_MEM(dir, name, prefix->buf, prefix->len);
303 hashmap_entry_init(&dir->ent, hash);
304 dir->namelen = prefix->len;
305 dir->parent = parent;
306 hashmap_add(&istate->dir_hash, &dir->ent);
308 if (parent) {
309 unlock_dir_mutex(lock_nr);
311 /* All I really need here is an InterlockedIncrement(&(parent->nr)) */
312 lock_nr = compute_dir_lock_nr(&istate->dir_hash, parent->ent.hash);
313 lock_dir_mutex(lock_nr);
314 parent->nr++;
318 unlock_dir_mutex(lock_nr);
320 return dir;
324 * handle_range_1() and handle_range_dir() are derived from
325 * clear_ce_flags_1() and clear_ce_flags_dir() in unpack-trees.c
326 * and handle the iteration over the entire array of index entries.
327 * They use recursion for adjacent entries in the same parent
328 * directory.
330 static int handle_range_1(
331 struct index_state *istate,
332 int k_start,
333 int k_end,
334 struct dir_entry *parent,
335 struct strbuf *prefix,
336 struct lazy_entry *lazy_entries);
338 static int handle_range_dir(
339 struct index_state *istate,
340 int k_start,
341 int k_end,
342 struct dir_entry *parent,
343 struct strbuf *prefix,
344 struct lazy_entry *lazy_entries,
345 struct dir_entry **dir_new_out)
347 int rc, k;
348 int input_prefix_len = prefix->len;
349 struct dir_entry *dir_new;
351 dir_new = hash_dir_entry_with_parent_and_prefix(istate, parent, prefix);
353 strbuf_addch(prefix, '/');
356 * Scan forward in the index array for index entries having the same
357 * path prefix (that are also in this directory).
359 if (k_start + 1 >= k_end)
360 k = k_end;
361 else if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
362 k = k_start + 1;
363 else if (strncmp(istate->cache[k_end - 1]->name, prefix->buf, prefix->len) == 0)
364 k = k_end;
365 else {
366 int begin = k_start;
367 int end = k_end;
368 assert(begin >= 0);
369 while (begin < end) {
370 int mid = begin + ((end - begin) >> 1);
371 int cmp = strncmp(istate->cache[mid]->name, prefix->buf, prefix->len);
372 if (cmp == 0) /* mid has same prefix; look in second part */
373 begin = mid + 1;
374 else if (cmp > 0) /* mid is past group; look in first part */
375 end = mid;
376 else
377 die("cache entry out of order");
379 k = begin;
383 * Recurse and process what we can of this subset [k_start, k).
385 rc = handle_range_1(istate, k_start, k, dir_new, prefix, lazy_entries);
387 strbuf_setlen(prefix, input_prefix_len);
389 *dir_new_out = dir_new;
390 return rc;
393 static int handle_range_1(
394 struct index_state *istate,
395 int k_start,
396 int k_end,
397 struct dir_entry *parent,
398 struct strbuf *prefix,
399 struct lazy_entry *lazy_entries)
401 int input_prefix_len = prefix->len;
402 int k = k_start;
404 while (k < k_end) {
405 struct cache_entry *ce_k = istate->cache[k];
406 const char *name, *slash;
408 if (prefix->len && strncmp(ce_k->name, prefix->buf, prefix->len))
409 break;
411 name = ce_k->name + prefix->len;
412 slash = strchr(name, '/');
414 if (slash) {
415 int len = slash - name;
416 int processed;
417 struct dir_entry *dir_new;
419 strbuf_add(prefix, name, len);
420 processed = handle_range_dir(istate, k, k_end, parent, prefix, lazy_entries, &dir_new);
421 if (processed) {
422 k += processed;
423 strbuf_setlen(prefix, input_prefix_len);
424 continue;
427 strbuf_addch(prefix, '/');
428 processed = handle_range_1(istate, k, k_end, dir_new, prefix, lazy_entries);
429 k += processed;
430 strbuf_setlen(prefix, input_prefix_len);
431 continue;
435 * It is too expensive to take a lock to insert "ce_k"
436 * into "istate->name_hash" and increment the ref-count
437 * on the "parent" dir. So we defer actually updating
438 * permanent data structures until phase 2 (where we
439 * can change the locking requirements) and simply
440 * accumulate our current results into the lazy_entries
441 * data array).
443 * We do not need to lock the lazy_entries array because
444 * we have exclusive access to the cells in the range
445 * [k_start,k_end) that this thread was given.
447 lazy_entries[k].dir = parent;
448 if (parent) {
449 lazy_entries[k].hash_name = memihash_cont(
450 parent->ent.hash,
451 ce_k->name + parent->namelen,
452 ce_namelen(ce_k) - parent->namelen);
453 lazy_entries[k].hash_dir = parent->ent.hash;
454 } else {
455 lazy_entries[k].hash_name = memihash(ce_k->name, ce_namelen(ce_k));
458 k++;
461 return k - k_start;
464 struct lazy_dir_thread_data {
465 pthread_t pthread;
466 struct index_state *istate;
467 struct lazy_entry *lazy_entries;
468 int k_start;
469 int k_end;
472 static void *lazy_dir_thread_proc(void *_data)
474 struct lazy_dir_thread_data *d = _data;
475 struct strbuf prefix = STRBUF_INIT;
476 handle_range_1(d->istate, d->k_start, d->k_end, NULL, &prefix, d->lazy_entries);
477 strbuf_release(&prefix);
478 return NULL;
481 struct lazy_name_thread_data {
482 pthread_t pthread;
483 struct index_state *istate;
484 struct lazy_entry *lazy_entries;
487 static void *lazy_name_thread_proc(void *_data)
489 struct lazy_name_thread_data *d = _data;
490 int k;
492 for (k = 0; k < d->istate->cache_nr; k++) {
493 struct cache_entry *ce_k = d->istate->cache[k];
494 ce_k->ce_flags |= CE_HASHED;
495 hashmap_entry_init(&ce_k->ent, d->lazy_entries[k].hash_name);
496 hashmap_add(&d->istate->name_hash, &ce_k->ent);
499 return NULL;
502 static inline void lazy_update_dir_ref_counts(
503 struct index_state *istate,
504 struct lazy_entry *lazy_entries)
506 int k;
508 for (k = 0; k < istate->cache_nr; k++) {
509 if (lazy_entries[k].dir)
510 lazy_entries[k].dir->nr++;
514 static void threaded_lazy_init_name_hash(
515 struct index_state *istate)
517 int err;
518 int nr_each;
519 int k_start;
520 int t;
521 struct lazy_entry *lazy_entries;
522 struct lazy_dir_thread_data *td_dir;
523 struct lazy_name_thread_data *td_name;
525 if (!HAVE_THREADS)
526 return;
528 k_start = 0;
529 nr_each = DIV_ROUND_UP(istate->cache_nr, lazy_nr_dir_threads);
531 CALLOC_ARRAY(lazy_entries, istate->cache_nr);
532 CALLOC_ARRAY(td_dir, lazy_nr_dir_threads);
533 CALLOC_ARRAY(td_name, 1);
535 init_dir_mutex();
538 * Phase 1:
539 * Build "istate->dir_hash" using n "dir" threads (and a read-only index).
541 for (t = 0; t < lazy_nr_dir_threads; t++) {
542 struct lazy_dir_thread_data *td_dir_t = td_dir + t;
543 td_dir_t->istate = istate;
544 td_dir_t->lazy_entries = lazy_entries;
545 td_dir_t->k_start = k_start;
546 k_start += nr_each;
547 if (k_start > istate->cache_nr)
548 k_start = istate->cache_nr;
549 td_dir_t->k_end = k_start;
550 err = pthread_create(&td_dir_t->pthread, NULL, lazy_dir_thread_proc, td_dir_t);
551 if (err)
552 die(_("unable to create lazy_dir thread: %s"), strerror(err));
554 for (t = 0; t < lazy_nr_dir_threads; t++) {
555 struct lazy_dir_thread_data *td_dir_t = td_dir + t;
556 if (pthread_join(td_dir_t->pthread, NULL))
557 die("unable to join lazy_dir_thread");
561 * Phase 2:
562 * Iterate over all index entries and add them to the "istate->name_hash"
563 * using a single "name" background thread.
564 * (Testing showed it wasn't worth running more than 1 thread for this.)
566 * Meanwhile, finish updating the parent directory ref-counts for each
567 * index entry using the current thread. (This step is very fast and
568 * doesn't need threading.)
570 td_name->istate = istate;
571 td_name->lazy_entries = lazy_entries;
572 err = pthread_create(&td_name->pthread, NULL, lazy_name_thread_proc, td_name);
573 if (err)
574 die(_("unable to create lazy_name thread: %s"), strerror(err));
576 lazy_update_dir_ref_counts(istate, lazy_entries);
578 err = pthread_join(td_name->pthread, NULL);
579 if (err)
580 die(_("unable to join lazy_name thread: %s"), strerror(err));
582 cleanup_dir_mutex();
584 free(td_name);
585 free(td_dir);
586 free(lazy_entries);
589 static void lazy_init_name_hash(struct index_state *istate)
592 if (istate->name_hash_initialized)
593 return;
594 trace_performance_enter();
595 trace2_region_enter("index", "name-hash-init", istate->repo);
596 hashmap_init(&istate->name_hash, cache_entry_cmp, NULL, istate->cache_nr);
597 hashmap_init(&istate->dir_hash, dir_entry_cmp, NULL, istate->cache_nr);
599 if (lookup_lazy_params(istate)) {
601 * Disable item counting and automatic rehashing because
602 * we do per-chain (mod n) locking rather than whole hashmap
603 * locking and we need to prevent the table-size from changing
604 * and bucket items from being redistributed.
606 hashmap_disable_item_counting(&istate->dir_hash);
607 threaded_lazy_init_name_hash(istate);
608 hashmap_enable_item_counting(&istate->dir_hash);
609 } else {
610 int nr;
611 for (nr = 0; nr < istate->cache_nr; nr++)
612 hash_index_entry(istate, istate->cache[nr]);
615 istate->name_hash_initialized = 1;
616 trace2_region_leave("index", "name-hash-init", istate->repo);
617 trace_performance_leave("initialize name hash");
621 * A test routine for t/helper/ sources.
623 * Returns the number of threads used or 0 when
624 * the non-threaded code path was used.
626 * Requesting threading WILL NOT override guards
627 * in lookup_lazy_params().
629 int test_lazy_init_name_hash(struct index_state *istate, int try_threaded)
631 lazy_nr_dir_threads = 0;
632 lazy_try_threaded = try_threaded;
634 lazy_init_name_hash(istate);
636 return lazy_nr_dir_threads;
639 void add_name_hash(struct index_state *istate, struct cache_entry *ce)
641 if (istate->name_hash_initialized)
642 hash_index_entry(istate, ce);
645 void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
647 if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED))
648 return;
649 ce->ce_flags &= ~CE_HASHED;
650 hashmap_remove(&istate->name_hash, &ce->ent, ce);
652 if (ignore_case)
653 remove_dir_entry(istate, ce);
656 static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
658 if (len1 != len2)
659 return 0;
661 while (len1) {
662 unsigned char c1 = *name1++;
663 unsigned char c2 = *name2++;
664 len1--;
665 if (c1 != c2) {
666 c1 = toupper(c1);
667 c2 = toupper(c2);
668 if (c1 != c2)
669 return 0;
672 return 1;
675 static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase)
677 int len = ce_namelen(ce);
680 * Always do exact compare, even if we want a case-ignoring comparison;
681 * we do the quick exact one first, because it will be the common case.
683 if (len == namelen && !memcmp(name, ce->name, len))
684 return 1;
686 if (!icase)
687 return 0;
689 return slow_same_name(name, namelen, ce->name, len);
692 int index_dir_find(struct index_state *istate, const char *name, int namelen,
693 struct strbuf *canonical_path)
695 struct dir_entry *dir;
697 lazy_init_name_hash(istate);
698 expand_to_path(istate, name, namelen, 0);
699 dir = find_dir_entry(istate, name, namelen);
701 if (canonical_path && dir && dir->nr) {
702 strbuf_reset(canonical_path);
703 strbuf_add(canonical_path, dir->name, dir->namelen);
706 return dir && dir->nr;
709 void adjust_dirname_case(struct index_state *istate, char *name)
711 const char *startPtr = name;
712 const char *ptr = startPtr;
714 lazy_init_name_hash(istate);
715 expand_to_path(istate, name, strlen(name), 0);
716 while (*ptr) {
717 while (*ptr && *ptr != '/')
718 ptr++;
720 if (*ptr == '/') {
721 struct dir_entry *dir;
723 dir = find_dir_entry(istate, name, ptr - name);
724 if (dir) {
725 memcpy((void *)startPtr, dir->name + (startPtr - name), ptr - startPtr);
726 startPtr = ptr + 1;
728 ptr++;
733 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
735 struct cache_entry *ce;
736 unsigned int hash = memihash(name, namelen);
738 lazy_init_name_hash(istate);
739 expand_to_path(istate, name, namelen, icase);
741 ce = hashmap_get_entry_from_hash(&istate->name_hash, hash, NULL,
742 struct cache_entry, ent);
743 hashmap_for_each_entry_from(&istate->name_hash, ce, ent) {
744 if (same_name(ce, name, namelen, icase))
745 return ce;
747 return NULL;
750 void free_name_hash(struct index_state *istate)
752 if (!istate->name_hash_initialized)
753 return;
754 istate->name_hash_initialized = 0;
756 hashmap_clear(&istate->name_hash);
757 hashmap_clear_and_free(&istate->dir_hash, struct dir_entry, ent);