Merge branch 'add-uk-initial-l10n' of github.com:arkid15r/git-ukrainian-l10n
[alt-git.git] / name-hash.c
blobfb13716e4303ebc7c96caa49934ca67f61ae772e
1 /*
2 * name-hash.c
4 * Hashing names in the index state
6 * Copyright (C) 2008 Linus Torvalds
7 */
8 #include "cache.h"
9 #include "environment.h"
10 #include "gettext.h"
11 #include "thread-utils.h"
12 #include "trace.h"
13 #include "trace2.h"
14 #include "sparse-index.h"
16 struct dir_entry {
17 struct hashmap_entry ent;
18 struct dir_entry *parent;
19 int nr;
20 unsigned int namelen;
21 char name[FLEX_ARRAY];
24 static int dir_entry_cmp(const void *cmp_data UNUSED,
25 const struct hashmap_entry *eptr,
26 const struct hashmap_entry *entry_or_key,
27 const void *keydata)
29 const struct dir_entry *e1, *e2;
30 const char *name = keydata;
32 e1 = container_of(eptr, const struct dir_entry, ent);
33 e2 = container_of(entry_or_key, const struct dir_entry, ent);
35 return e1->namelen != e2->namelen || strncasecmp(e1->name,
36 name ? name : e2->name, e1->namelen);
39 static struct dir_entry *find_dir_entry__hash(struct index_state *istate,
40 const char *name, unsigned int namelen, unsigned int hash)
42 struct dir_entry key;
43 hashmap_entry_init(&key.ent, hash);
44 key.namelen = namelen;
45 return hashmap_get_entry(&istate->dir_hash, &key, ent, name);
48 static struct dir_entry *find_dir_entry(struct index_state *istate,
49 const char *name, unsigned int namelen)
51 return find_dir_entry__hash(istate, name, namelen, memihash(name, namelen));
54 static struct dir_entry *hash_dir_entry(struct index_state *istate,
55 struct cache_entry *ce, int namelen)
58 * Throw each directory component in the hash for quick lookup
59 * during a git status. Directory components are stored without their
60 * closing slash. Despite submodules being a directory, they never
61 * reach this point, because they are stored
62 * in index_state.name_hash (as ordinary cache_entries).
64 struct dir_entry *dir;
66 /* get length of parent directory */
67 while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1]))
68 namelen--;
69 if (namelen <= 0)
70 return NULL;
71 namelen--;
73 /* lookup existing entry for that directory */
74 dir = find_dir_entry(istate, ce->name, namelen);
75 if (!dir) {
76 /* not found, create it and add to hash table */
77 FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
78 hashmap_entry_init(&dir->ent, memihash(ce->name, namelen));
79 dir->namelen = namelen;
80 hashmap_add(&istate->dir_hash, &dir->ent);
82 /* recursively add missing parent directories */
83 dir->parent = hash_dir_entry(istate, ce, namelen);
85 return dir;
88 static void add_dir_entry(struct index_state *istate, struct cache_entry *ce)
90 /* Add reference to the directory entry (and parents if 0). */
91 struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
92 while (dir && !(dir->nr++))
93 dir = dir->parent;
96 static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
99 * Release reference to the directory entry. If 0, remove and continue
100 * with parent directory.
102 struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
103 while (dir && !(--dir->nr)) {
104 struct dir_entry *parent = dir->parent;
105 hashmap_remove(&istate->dir_hash, &dir->ent, NULL);
106 free(dir);
107 dir = parent;
111 static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
113 if (ce->ce_flags & CE_HASHED)
114 return;
115 ce->ce_flags |= CE_HASHED;
117 if (!S_ISSPARSEDIR(ce->ce_mode)) {
118 hashmap_entry_init(&ce->ent, memihash(ce->name, ce_namelen(ce)));
119 hashmap_add(&istate->name_hash, &ce->ent);
122 if (ignore_case)
123 add_dir_entry(istate, ce);
126 static int cache_entry_cmp(const void *cmp_data UNUSED,
127 const struct hashmap_entry *eptr,
128 const struct hashmap_entry *entry_or_key,
129 const void *remove)
131 const struct cache_entry *ce1, *ce2;
133 ce1 = container_of(eptr, const struct cache_entry, ent);
134 ce2 = container_of(entry_or_key, const struct cache_entry, ent);
137 * For remove_name_hash, find the exact entry (pointer equality); for
138 * index_file_exists, find all entries with matching hash code and
139 * decide whether the entry matches in same_name.
141 return remove ? !(ce1 == ce2) : 0;
144 static int lazy_try_threaded = 1;
145 static int lazy_nr_dir_threads;
148 * Set a minimum number of cache_entries that we will handle per
149 * thread and use that to decide how many threads to run (up to
150 * the number on the system).
152 * For guidance setting the lower per-thread bound, see:
153 * t/helper/test-lazy-init-name-hash --analyze
155 #define LAZY_THREAD_COST (2000)
158 * We use n mutexes to guard n partitions of the "istate->dir_hash"
159 * hashtable. Since "find" and "insert" operations will hash to a
160 * particular bucket and modify/search a single chain, we can say
161 * that "all chains mod n" are guarded by the same mutex -- rather
162 * than having a single mutex to guard the entire table. (This does
163 * require that we disable "rehashing" on the hashtable.)
165 * So, a larger value here decreases the probability of a collision
166 * and the time that each thread must wait for the mutex.
168 #define LAZY_MAX_MUTEX (32)
170 static pthread_mutex_t *lazy_dir_mutex_array;
173 * An array of lazy_entry items is used by the n threads in
174 * the directory parse (first) phase to (lock-free) store the
175 * intermediate results. These values are then referenced by
176 * the 2 threads in the second phase.
178 struct lazy_entry {
179 struct dir_entry *dir;
180 unsigned int hash_dir;
181 unsigned int hash_name;
185 * Decide if we want to use threads (if available) to load
186 * the hash tables. We set "lazy_nr_dir_threads" to zero when
187 * it is not worth it.
189 static int lookup_lazy_params(struct index_state *istate)
191 int nr_cpus;
193 lazy_nr_dir_threads = 0;
195 if (!lazy_try_threaded)
196 return 0;
199 * If we are respecting case, just use the original
200 * code to build the "istate->name_hash". We don't
201 * need the complexity here.
203 if (!ignore_case)
204 return 0;
206 nr_cpus = online_cpus();
207 if (nr_cpus < 2)
208 return 0;
210 if (istate->cache_nr < 2 * LAZY_THREAD_COST)
211 return 0;
213 if (istate->cache_nr < nr_cpus * LAZY_THREAD_COST)
214 nr_cpus = istate->cache_nr / LAZY_THREAD_COST;
215 lazy_nr_dir_threads = nr_cpus;
216 return lazy_nr_dir_threads;
220 * Initialize n mutexes for use when searching and inserting
221 * into "istate->dir_hash". All "dir" threads are trying
222 * to insert partial pathnames into the hash as they iterate
223 * over their portions of the index, so lock contention is
224 * high.
226 * However, the hashmap is going to put items into bucket
227 * chains based on their hash values. Use that to create n
228 * mutexes and lock on mutex[bucket(hash) % n]. This will
229 * decrease the collision rate by (hopefully) a factor of n.
231 static void init_dir_mutex(void)
233 int j;
235 CALLOC_ARRAY(lazy_dir_mutex_array, LAZY_MAX_MUTEX);
237 for (j = 0; j < LAZY_MAX_MUTEX; j++)
238 init_recursive_mutex(&lazy_dir_mutex_array[j]);
241 static void cleanup_dir_mutex(void)
243 int j;
245 for (j = 0; j < LAZY_MAX_MUTEX; j++)
246 pthread_mutex_destroy(&lazy_dir_mutex_array[j]);
248 free(lazy_dir_mutex_array);
251 static void lock_dir_mutex(int j)
253 pthread_mutex_lock(&lazy_dir_mutex_array[j]);
256 static void unlock_dir_mutex(int j)
258 pthread_mutex_unlock(&lazy_dir_mutex_array[j]);
261 static inline int compute_dir_lock_nr(
262 const struct hashmap *map,
263 unsigned int hash)
265 return hashmap_bucket(map, hash) % LAZY_MAX_MUTEX;
268 static struct dir_entry *hash_dir_entry_with_parent_and_prefix(
269 struct index_state *istate,
270 struct dir_entry *parent,
271 struct strbuf *prefix)
273 struct dir_entry *dir;
274 unsigned int hash;
275 int lock_nr;
278 * Either we have a parent directory and path with slash(es)
279 * or the directory is an immediate child of the root directory.
281 assert((parent != NULL) ^ (strchr(prefix->buf, '/') == NULL));
283 if (parent)
284 hash = memihash_cont(parent->ent.hash,
285 prefix->buf + parent->namelen,
286 prefix->len - parent->namelen);
287 else
288 hash = memihash(prefix->buf, prefix->len);
290 lock_nr = compute_dir_lock_nr(&istate->dir_hash, hash);
291 lock_dir_mutex(lock_nr);
293 dir = find_dir_entry__hash(istate, prefix->buf, prefix->len, hash);
294 if (!dir) {
295 FLEX_ALLOC_MEM(dir, name, prefix->buf, prefix->len);
296 hashmap_entry_init(&dir->ent, hash);
297 dir->namelen = prefix->len;
298 dir->parent = parent;
299 hashmap_add(&istate->dir_hash, &dir->ent);
301 if (parent) {
302 unlock_dir_mutex(lock_nr);
304 /* All I really need here is an InterlockedIncrement(&(parent->nr)) */
305 lock_nr = compute_dir_lock_nr(&istate->dir_hash, parent->ent.hash);
306 lock_dir_mutex(lock_nr);
307 parent->nr++;
311 unlock_dir_mutex(lock_nr);
313 return dir;
317 * handle_range_1() and handle_range_dir() are derived from
318 * clear_ce_flags_1() and clear_ce_flags_dir() in unpack-trees.c
319 * and handle the iteration over the entire array of index entries.
320 * They use recursion for adjacent entries in the same parent
321 * directory.
323 static int handle_range_1(
324 struct index_state *istate,
325 int k_start,
326 int k_end,
327 struct dir_entry *parent,
328 struct strbuf *prefix,
329 struct lazy_entry *lazy_entries);
331 static int handle_range_dir(
332 struct index_state *istate,
333 int k_start,
334 int k_end,
335 struct dir_entry *parent,
336 struct strbuf *prefix,
337 struct lazy_entry *lazy_entries,
338 struct dir_entry **dir_new_out)
340 int rc, k;
341 int input_prefix_len = prefix->len;
342 struct dir_entry *dir_new;
344 dir_new = hash_dir_entry_with_parent_and_prefix(istate, parent, prefix);
346 strbuf_addch(prefix, '/');
349 * Scan forward in the index array for index entries having the same
350 * path prefix (that are also in this directory).
352 if (k_start + 1 >= k_end)
353 k = k_end;
354 else if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
355 k = k_start + 1;
356 else if (strncmp(istate->cache[k_end - 1]->name, prefix->buf, prefix->len) == 0)
357 k = k_end;
358 else {
359 int begin = k_start;
360 int end = k_end;
361 assert(begin >= 0);
362 while (begin < end) {
363 int mid = begin + ((end - begin) >> 1);
364 int cmp = strncmp(istate->cache[mid]->name, prefix->buf, prefix->len);
365 if (cmp == 0) /* mid has same prefix; look in second part */
366 begin = mid + 1;
367 else if (cmp > 0) /* mid is past group; look in first part */
368 end = mid;
369 else
370 die("cache entry out of order");
372 k = begin;
376 * Recurse and process what we can of this subset [k_start, k).
378 rc = handle_range_1(istate, k_start, k, dir_new, prefix, lazy_entries);
380 strbuf_setlen(prefix, input_prefix_len);
382 *dir_new_out = dir_new;
383 return rc;
386 static int handle_range_1(
387 struct index_state *istate,
388 int k_start,
389 int k_end,
390 struct dir_entry *parent,
391 struct strbuf *prefix,
392 struct lazy_entry *lazy_entries)
394 int input_prefix_len = prefix->len;
395 int k = k_start;
397 while (k < k_end) {
398 struct cache_entry *ce_k = istate->cache[k];
399 const char *name, *slash;
401 if (prefix->len && strncmp(ce_k->name, prefix->buf, prefix->len))
402 break;
404 name = ce_k->name + prefix->len;
405 slash = strchr(name, '/');
407 if (slash) {
408 int len = slash - name;
409 int processed;
410 struct dir_entry *dir_new;
412 strbuf_add(prefix, name, len);
413 processed = handle_range_dir(istate, k, k_end, parent, prefix, lazy_entries, &dir_new);
414 if (processed) {
415 k += processed;
416 strbuf_setlen(prefix, input_prefix_len);
417 continue;
420 strbuf_addch(prefix, '/');
421 processed = handle_range_1(istate, k, k_end, dir_new, prefix, lazy_entries);
422 k += processed;
423 strbuf_setlen(prefix, input_prefix_len);
424 continue;
428 * It is too expensive to take a lock to insert "ce_k"
429 * into "istate->name_hash" and increment the ref-count
430 * on the "parent" dir. So we defer actually updating
431 * permanent data structures until phase 2 (where we
432 * can change the locking requirements) and simply
433 * accumulate our current results into the lazy_entries
434 * data array).
436 * We do not need to lock the lazy_entries array because
437 * we have exclusive access to the cells in the range
438 * [k_start,k_end) that this thread was given.
440 lazy_entries[k].dir = parent;
441 if (parent) {
442 lazy_entries[k].hash_name = memihash_cont(
443 parent->ent.hash,
444 ce_k->name + parent->namelen,
445 ce_namelen(ce_k) - parent->namelen);
446 lazy_entries[k].hash_dir = parent->ent.hash;
447 } else {
448 lazy_entries[k].hash_name = memihash(ce_k->name, ce_namelen(ce_k));
451 k++;
454 return k - k_start;
457 struct lazy_dir_thread_data {
458 pthread_t pthread;
459 struct index_state *istate;
460 struct lazy_entry *lazy_entries;
461 int k_start;
462 int k_end;
465 static void *lazy_dir_thread_proc(void *_data)
467 struct lazy_dir_thread_data *d = _data;
468 struct strbuf prefix = STRBUF_INIT;
469 handle_range_1(d->istate, d->k_start, d->k_end, NULL, &prefix, d->lazy_entries);
470 strbuf_release(&prefix);
471 return NULL;
474 struct lazy_name_thread_data {
475 pthread_t pthread;
476 struct index_state *istate;
477 struct lazy_entry *lazy_entries;
480 static void *lazy_name_thread_proc(void *_data)
482 struct lazy_name_thread_data *d = _data;
483 int k;
485 for (k = 0; k < d->istate->cache_nr; k++) {
486 struct cache_entry *ce_k = d->istate->cache[k];
487 ce_k->ce_flags |= CE_HASHED;
488 hashmap_entry_init(&ce_k->ent, d->lazy_entries[k].hash_name);
489 hashmap_add(&d->istate->name_hash, &ce_k->ent);
492 return NULL;
495 static inline void lazy_update_dir_ref_counts(
496 struct index_state *istate,
497 struct lazy_entry *lazy_entries)
499 int k;
501 for (k = 0; k < istate->cache_nr; k++) {
502 if (lazy_entries[k].dir)
503 lazy_entries[k].dir->nr++;
507 static void threaded_lazy_init_name_hash(
508 struct index_state *istate)
510 int err;
511 int nr_each;
512 int k_start;
513 int t;
514 struct lazy_entry *lazy_entries;
515 struct lazy_dir_thread_data *td_dir;
516 struct lazy_name_thread_data *td_name;
518 if (!HAVE_THREADS)
519 return;
521 k_start = 0;
522 nr_each = DIV_ROUND_UP(istate->cache_nr, lazy_nr_dir_threads);
524 CALLOC_ARRAY(lazy_entries, istate->cache_nr);
525 CALLOC_ARRAY(td_dir, lazy_nr_dir_threads);
526 CALLOC_ARRAY(td_name, 1);
528 init_dir_mutex();
531 * Phase 1:
532 * Build "istate->dir_hash" using n "dir" threads (and a read-only index).
534 for (t = 0; t < lazy_nr_dir_threads; t++) {
535 struct lazy_dir_thread_data *td_dir_t = td_dir + t;
536 td_dir_t->istate = istate;
537 td_dir_t->lazy_entries = lazy_entries;
538 td_dir_t->k_start = k_start;
539 k_start += nr_each;
540 if (k_start > istate->cache_nr)
541 k_start = istate->cache_nr;
542 td_dir_t->k_end = k_start;
543 err = pthread_create(&td_dir_t->pthread, NULL, lazy_dir_thread_proc, td_dir_t);
544 if (err)
545 die(_("unable to create lazy_dir thread: %s"), strerror(err));
547 for (t = 0; t < lazy_nr_dir_threads; t++) {
548 struct lazy_dir_thread_data *td_dir_t = td_dir + t;
549 if (pthread_join(td_dir_t->pthread, NULL))
550 die("unable to join lazy_dir_thread");
554 * Phase 2:
555 * Iterate over all index entries and add them to the "istate->name_hash"
556 * using a single "name" background thread.
557 * (Testing showed it wasn't worth running more than 1 thread for this.)
559 * Meanwhile, finish updating the parent directory ref-counts for each
560 * index entry using the current thread. (This step is very fast and
561 * doesn't need threading.)
563 td_name->istate = istate;
564 td_name->lazy_entries = lazy_entries;
565 err = pthread_create(&td_name->pthread, NULL, lazy_name_thread_proc, td_name);
566 if (err)
567 die(_("unable to create lazy_name thread: %s"), strerror(err));
569 lazy_update_dir_ref_counts(istate, lazy_entries);
571 err = pthread_join(td_name->pthread, NULL);
572 if (err)
573 die(_("unable to join lazy_name thread: %s"), strerror(err));
575 cleanup_dir_mutex();
577 free(td_name);
578 free(td_dir);
579 free(lazy_entries);
582 static void lazy_init_name_hash(struct index_state *istate)
585 if (istate->name_hash_initialized)
586 return;
587 trace_performance_enter();
588 trace2_region_enter("index", "name-hash-init", istate->repo);
589 hashmap_init(&istate->name_hash, cache_entry_cmp, NULL, istate->cache_nr);
590 hashmap_init(&istate->dir_hash, dir_entry_cmp, NULL, istate->cache_nr);
592 if (lookup_lazy_params(istate)) {
594 * Disable item counting and automatic rehashing because
595 * we do per-chain (mod n) locking rather than whole hashmap
596 * locking and we need to prevent the table-size from changing
597 * and bucket items from being redistributed.
599 hashmap_disable_item_counting(&istate->dir_hash);
600 threaded_lazy_init_name_hash(istate);
601 hashmap_enable_item_counting(&istate->dir_hash);
602 } else {
603 int nr;
604 for (nr = 0; nr < istate->cache_nr; nr++)
605 hash_index_entry(istate, istate->cache[nr]);
608 istate->name_hash_initialized = 1;
609 trace2_region_leave("index", "name-hash-init", istate->repo);
610 trace_performance_leave("initialize name hash");
614 * A test routine for t/helper/ sources.
616 * Returns the number of threads used or 0 when
617 * the non-threaded code path was used.
619 * Requesting threading WILL NOT override guards
620 * in lookup_lazy_params().
622 int test_lazy_init_name_hash(struct index_state *istate, int try_threaded)
624 lazy_nr_dir_threads = 0;
625 lazy_try_threaded = try_threaded;
627 lazy_init_name_hash(istate);
629 return lazy_nr_dir_threads;
632 void add_name_hash(struct index_state *istate, struct cache_entry *ce)
634 if (istate->name_hash_initialized)
635 hash_index_entry(istate, ce);
638 void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
640 if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED))
641 return;
642 ce->ce_flags &= ~CE_HASHED;
643 hashmap_remove(&istate->name_hash, &ce->ent, ce);
645 if (ignore_case)
646 remove_dir_entry(istate, ce);
649 static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
651 if (len1 != len2)
652 return 0;
654 while (len1) {
655 unsigned char c1 = *name1++;
656 unsigned char c2 = *name2++;
657 len1--;
658 if (c1 != c2) {
659 c1 = toupper(c1);
660 c2 = toupper(c2);
661 if (c1 != c2)
662 return 0;
665 return 1;
668 static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase)
670 int len = ce_namelen(ce);
673 * Always do exact compare, even if we want a case-ignoring comparison;
674 * we do the quick exact one first, because it will be the common case.
676 if (len == namelen && !memcmp(name, ce->name, len))
677 return 1;
679 if (!icase)
680 return 0;
682 return slow_same_name(name, namelen, ce->name, len);
685 int index_dir_exists(struct index_state *istate, const char *name, int namelen)
687 struct dir_entry *dir;
689 lazy_init_name_hash(istate);
690 expand_to_path(istate, name, namelen, 0);
691 dir = find_dir_entry(istate, name, namelen);
692 return dir && dir->nr;
695 void adjust_dirname_case(struct index_state *istate, char *name)
697 const char *startPtr = name;
698 const char *ptr = startPtr;
700 lazy_init_name_hash(istate);
701 expand_to_path(istate, name, strlen(name), 0);
702 while (*ptr) {
703 while (*ptr && *ptr != '/')
704 ptr++;
706 if (*ptr == '/') {
707 struct dir_entry *dir;
709 dir = find_dir_entry(istate, name, ptr - name);
710 if (dir) {
711 memcpy((void *)startPtr, dir->name + (startPtr - name), ptr - startPtr);
712 startPtr = ptr + 1;
714 ptr++;
719 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
721 struct cache_entry *ce;
722 unsigned int hash = memihash(name, namelen);
724 lazy_init_name_hash(istate);
725 expand_to_path(istate, name, namelen, icase);
727 ce = hashmap_get_entry_from_hash(&istate->name_hash, hash, NULL,
728 struct cache_entry, ent);
729 hashmap_for_each_entry_from(&istate->name_hash, ce, ent) {
730 if (same_name(ce, name, namelen, icase))
731 return ce;
733 return NULL;
736 void free_name_hash(struct index_state *istate)
738 if (!istate->name_hash_initialized)
739 return;
740 istate->name_hash_initialized = 0;
742 hashmap_clear(&istate->name_hash);
743 hashmap_clear_and_free(&istate->dir_hash, struct dir_entry, ent);