]>
git.ipfire.org Git - thirdparty/git.git/blob - name-hash.c
4 * Hashing names in the index state
6 * Copyright (C) 2008 Linus Torvalds
9 #include "thread-utils.h"
11 #include "sparse-index.h"
14 struct hashmap_entry ent
;
15 struct dir_entry
*parent
;
18 char name
[FLEX_ARRAY
];
21 static int dir_entry_cmp(const void *unused_cmp_data
,
22 const struct hashmap_entry
*eptr
,
23 const struct hashmap_entry
*entry_or_key
,
26 const struct dir_entry
*e1
, *e2
;
27 const char *name
= keydata
;
29 e1
= container_of(eptr
, const struct dir_entry
, ent
);
30 e2
= container_of(entry_or_key
, const struct dir_entry
, ent
);
32 return e1
->namelen
!= e2
->namelen
|| strncasecmp(e1
->name
,
33 name
? name
: e2
->name
, e1
->namelen
);
36 static struct dir_entry
*find_dir_entry__hash(struct index_state
*istate
,
37 const char *name
, unsigned int namelen
, unsigned int hash
)
40 hashmap_entry_init(&key
.ent
, hash
);
41 key
.namelen
= namelen
;
42 return hashmap_get_entry(&istate
->dir_hash
, &key
, ent
, name
);
45 static struct dir_entry
*find_dir_entry(struct index_state
*istate
,
46 const char *name
, unsigned int namelen
)
48 return find_dir_entry__hash(istate
, name
, namelen
, memihash(name
, namelen
));
51 static struct dir_entry
*hash_dir_entry(struct index_state
*istate
,
52 struct cache_entry
*ce
, int namelen
)
55 * Throw each directory component in the hash for quick lookup
56 * during a git status. Directory components are stored without their
57 * closing slash. Despite submodules being a directory, they never
58 * reach this point, because they are stored
59 * in index_state.name_hash (as ordinary cache_entries).
61 struct dir_entry
*dir
;
63 /* get length of parent directory */
64 while (namelen
> 0 && !is_dir_sep(ce
->name
[namelen
- 1]))
70 /* lookup existing entry for that directory */
71 dir
= find_dir_entry(istate
, ce
->name
, namelen
);
73 /* not found, create it and add to hash table */
74 FLEX_ALLOC_MEM(dir
, name
, ce
->name
, namelen
);
75 hashmap_entry_init(&dir
->ent
, memihash(ce
->name
, namelen
));
76 dir
->namelen
= namelen
;
77 hashmap_add(&istate
->dir_hash
, &dir
->ent
);
79 /* recursively add missing parent directories */
80 dir
->parent
= hash_dir_entry(istate
, ce
, namelen
);
85 static void add_dir_entry(struct index_state
*istate
, struct cache_entry
*ce
)
87 /* Add reference to the directory entry (and parents if 0). */
88 struct dir_entry
*dir
= hash_dir_entry(istate
, ce
, ce_namelen(ce
));
89 while (dir
&& !(dir
->nr
++))
93 static void remove_dir_entry(struct index_state
*istate
, struct cache_entry
*ce
)
96 * Release reference to the directory entry. If 0, remove and continue
97 * with parent directory.
99 struct dir_entry
*dir
= hash_dir_entry(istate
, ce
, ce_namelen(ce
));
100 while (dir
&& !(--dir
->nr
)) {
101 struct dir_entry
*parent
= dir
->parent
;
102 hashmap_remove(&istate
->dir_hash
, &dir
->ent
, NULL
);
108 static void hash_index_entry(struct index_state
*istate
, struct cache_entry
*ce
)
110 if (ce
->ce_flags
& CE_HASHED
)
112 ce
->ce_flags
|= CE_HASHED
;
114 if (!S_ISSPARSEDIR(ce
->ce_mode
)) {
115 hashmap_entry_init(&ce
->ent
, memihash(ce
->name
, ce_namelen(ce
)));
116 hashmap_add(&istate
->name_hash
, &ce
->ent
);
120 add_dir_entry(istate
, ce
);
123 static int cache_entry_cmp(const void *unused_cmp_data
,
124 const struct hashmap_entry
*eptr
,
125 const struct hashmap_entry
*entry_or_key
,
128 const struct cache_entry
*ce1
, *ce2
;
130 ce1
= container_of(eptr
, const struct cache_entry
, ent
);
131 ce2
= container_of(entry_or_key
, const struct cache_entry
, ent
);
134 * For remove_name_hash, find the exact entry (pointer equality); for
135 * index_file_exists, find all entries with matching hash code and
136 * decide whether the entry matches in same_name.
138 return remove
? !(ce1
== ce2
) : 0;
141 static int lazy_try_threaded
= 1;
142 static int lazy_nr_dir_threads
;
145 * Set a minimum number of cache_entries that we will handle per
146 * thread and use that to decide how many threads to run (up to
147 * the number on the system).
149 * For guidance setting the lower per-thread bound, see:
150 * t/helper/test-lazy-init-name-hash --analyze
152 #define LAZY_THREAD_COST (2000)
155 * We use n mutexes to guard n partitions of the "istate->dir_hash"
156 * hashtable. Since "find" and "insert" operations will hash to a
157 * particular bucket and modify/search a single chain, we can say
158 * that "all chains mod n" are guarded by the same mutex -- rather
159 * than having a single mutex to guard the entire table. (This does
160 * require that we disable "rehashing" on the hashtable.)
162 * So, a larger value here decreases the probability of a collision
163 * and the time that each thread must wait for the mutex.
165 #define LAZY_MAX_MUTEX (32)
167 static pthread_mutex_t
*lazy_dir_mutex_array
;
170 * An array of lazy_entry items is used by the n threads in
171 * the directory parse (first) phase to (lock-free) store the
172 * intermediate results. These values are then referenced by
173 * the 2 threads in the second phase.
176 struct dir_entry
*dir
;
177 unsigned int hash_dir
;
178 unsigned int hash_name
;
182 * Decide if we want to use threads (if available) to load
183 * the hash tables. We set "lazy_nr_dir_threads" to zero when
184 * it is not worth it.
186 static int lookup_lazy_params(struct index_state
*istate
)
190 lazy_nr_dir_threads
= 0;
192 if (!lazy_try_threaded
)
196 * If we are respecting case, just use the original
197 * code to build the "istate->name_hash". We don't
198 * need the complexity here.
203 nr_cpus
= online_cpus();
207 if (istate
->cache_nr
< 2 * LAZY_THREAD_COST
)
210 if (istate
->cache_nr
< nr_cpus
* LAZY_THREAD_COST
)
211 nr_cpus
= istate
->cache_nr
/ LAZY_THREAD_COST
;
212 lazy_nr_dir_threads
= nr_cpus
;
213 return lazy_nr_dir_threads
;
217 * Initialize n mutexes for use when searching and inserting
218 * into "istate->dir_hash". All "dir" threads are trying
219 * to insert partial pathnames into the hash as they iterate
220 * over their portions of the index, so lock contention is
223 * However, the hashmap is going to put items into bucket
224 * chains based on their hash values. Use that to create n
225 * mutexes and lock on mutex[bucket(hash) % n]. This will
226 * decrease the collision rate by (hopefully) a factor of n.
228 static void init_dir_mutex(void)
232 CALLOC_ARRAY(lazy_dir_mutex_array
, LAZY_MAX_MUTEX
);
234 for (j
= 0; j
< LAZY_MAX_MUTEX
; j
++)
235 init_recursive_mutex(&lazy_dir_mutex_array
[j
]);
238 static void cleanup_dir_mutex(void)
242 for (j
= 0; j
< LAZY_MAX_MUTEX
; j
++)
243 pthread_mutex_destroy(&lazy_dir_mutex_array
[j
]);
245 free(lazy_dir_mutex_array
);
248 static void lock_dir_mutex(int j
)
250 pthread_mutex_lock(&lazy_dir_mutex_array
[j
]);
253 static void unlock_dir_mutex(int j
)
255 pthread_mutex_unlock(&lazy_dir_mutex_array
[j
]);
258 static inline int compute_dir_lock_nr(
259 const struct hashmap
*map
,
262 return hashmap_bucket(map
, hash
) % LAZY_MAX_MUTEX
;
265 static struct dir_entry
*hash_dir_entry_with_parent_and_prefix(
266 struct index_state
*istate
,
267 struct dir_entry
*parent
,
268 struct strbuf
*prefix
)
270 struct dir_entry
*dir
;
275 * Either we have a parent directory and path with slash(es)
276 * or the directory is an immediate child of the root directory.
278 assert((parent
!= NULL
) ^ (strchr(prefix
->buf
, '/') == NULL
));
281 hash
= memihash_cont(parent
->ent
.hash
,
282 prefix
->buf
+ parent
->namelen
,
283 prefix
->len
- parent
->namelen
);
285 hash
= memihash(prefix
->buf
, prefix
->len
);
287 lock_nr
= compute_dir_lock_nr(&istate
->dir_hash
, hash
);
288 lock_dir_mutex(lock_nr
);
290 dir
= find_dir_entry__hash(istate
, prefix
->buf
, prefix
->len
, hash
);
292 FLEX_ALLOC_MEM(dir
, name
, prefix
->buf
, prefix
->len
);
293 hashmap_entry_init(&dir
->ent
, hash
);
294 dir
->namelen
= prefix
->len
;
295 dir
->parent
= parent
;
296 hashmap_add(&istate
->dir_hash
, &dir
->ent
);
299 unlock_dir_mutex(lock_nr
);
301 /* All I really need here is an InterlockedIncrement(&(parent->nr)) */
302 lock_nr
= compute_dir_lock_nr(&istate
->dir_hash
, parent
->ent
.hash
);
303 lock_dir_mutex(lock_nr
);
308 unlock_dir_mutex(lock_nr
);
314 * handle_range_1() and handle_range_dir() are derived from
315 * clear_ce_flags_1() and clear_ce_flags_dir() in unpack-trees.c
316 * and handle the iteration over the entire array of index entries.
317 * They use recursion for adjacent entries in the same parent
320 static int handle_range_1(
321 struct index_state
*istate
,
324 struct dir_entry
*parent
,
325 struct strbuf
*prefix
,
326 struct lazy_entry
*lazy_entries
);
328 static int handle_range_dir(
329 struct index_state
*istate
,
332 struct dir_entry
*parent
,
333 struct strbuf
*prefix
,
334 struct lazy_entry
*lazy_entries
,
335 struct dir_entry
**dir_new_out
)
338 int input_prefix_len
= prefix
->len
;
339 struct dir_entry
*dir_new
;
341 dir_new
= hash_dir_entry_with_parent_and_prefix(istate
, parent
, prefix
);
343 strbuf_addch(prefix
, '/');
346 * Scan forward in the index array for index entries having the same
347 * path prefix (that are also in this directory).
349 if (k_start
+ 1 >= k_end
)
351 else if (strncmp(istate
->cache
[k_start
+ 1]->name
, prefix
->buf
, prefix
->len
) > 0)
353 else if (strncmp(istate
->cache
[k_end
- 1]->name
, prefix
->buf
, prefix
->len
) == 0)
359 while (begin
< end
) {
360 int mid
= begin
+ ((end
- begin
) >> 1);
361 int cmp
= strncmp(istate
->cache
[mid
]->name
, prefix
->buf
, prefix
->len
);
362 if (cmp
== 0) /* mid has same prefix; look in second part */
364 else if (cmp
> 0) /* mid is past group; look in first part */
367 die("cache entry out of order");
373 * Recurse and process what we can of this subset [k_start, k).
375 rc
= handle_range_1(istate
, k_start
, k
, dir_new
, prefix
, lazy_entries
);
377 strbuf_setlen(prefix
, input_prefix_len
);
379 *dir_new_out
= dir_new
;
383 static int handle_range_1(
384 struct index_state
*istate
,
387 struct dir_entry
*parent
,
388 struct strbuf
*prefix
,
389 struct lazy_entry
*lazy_entries
)
391 int input_prefix_len
= prefix
->len
;
395 struct cache_entry
*ce_k
= istate
->cache
[k
];
396 const char *name
, *slash
;
398 if (prefix
->len
&& strncmp(ce_k
->name
, prefix
->buf
, prefix
->len
))
401 name
= ce_k
->name
+ prefix
->len
;
402 slash
= strchr(name
, '/');
405 int len
= slash
- name
;
407 struct dir_entry
*dir_new
;
409 strbuf_add(prefix
, name
, len
);
410 processed
= handle_range_dir(istate
, k
, k_end
, parent
, prefix
, lazy_entries
, &dir_new
);
413 strbuf_setlen(prefix
, input_prefix_len
);
417 strbuf_addch(prefix
, '/');
418 processed
= handle_range_1(istate
, k
, k_end
, dir_new
, prefix
, lazy_entries
);
420 strbuf_setlen(prefix
, input_prefix_len
);
425 * It is too expensive to take a lock to insert "ce_k"
426 * into "istate->name_hash" and increment the ref-count
427 * on the "parent" dir. So we defer actually updating
428 * permanent data structures until phase 2 (where we
429 * can change the locking requirements) and simply
430 * accumulate our current results into the lazy_entries
433 * We do not need to lock the lazy_entries array because
434 * we have exclusive access to the cells in the range
435 * [k_start,k_end) that this thread was given.
437 lazy_entries
[k
].dir
= parent
;
439 lazy_entries
[k
].hash_name
= memihash_cont(
441 ce_k
->name
+ parent
->namelen
,
442 ce_namelen(ce_k
) - parent
->namelen
);
443 lazy_entries
[k
].hash_dir
= parent
->ent
.hash
;
445 lazy_entries
[k
].hash_name
= memihash(ce_k
->name
, ce_namelen(ce_k
));
454 struct lazy_dir_thread_data
{
456 struct index_state
*istate
;
457 struct lazy_entry
*lazy_entries
;
462 static void *lazy_dir_thread_proc(void *_data
)
464 struct lazy_dir_thread_data
*d
= _data
;
465 struct strbuf prefix
= STRBUF_INIT
;
466 handle_range_1(d
->istate
, d
->k_start
, d
->k_end
, NULL
, &prefix
, d
->lazy_entries
);
467 strbuf_release(&prefix
);
471 struct lazy_name_thread_data
{
473 struct index_state
*istate
;
474 struct lazy_entry
*lazy_entries
;
477 static void *lazy_name_thread_proc(void *_data
)
479 struct lazy_name_thread_data
*d
= _data
;
482 for (k
= 0; k
< d
->istate
->cache_nr
; k
++) {
483 struct cache_entry
*ce_k
= d
->istate
->cache
[k
];
484 ce_k
->ce_flags
|= CE_HASHED
;
485 hashmap_entry_init(&ce_k
->ent
, d
->lazy_entries
[k
].hash_name
);
486 hashmap_add(&d
->istate
->name_hash
, &ce_k
->ent
);
492 static inline void lazy_update_dir_ref_counts(
493 struct index_state
*istate
,
494 struct lazy_entry
*lazy_entries
)
498 for (k
= 0; k
< istate
->cache_nr
; k
++) {
499 if (lazy_entries
[k
].dir
)
500 lazy_entries
[k
].dir
->nr
++;
504 static void threaded_lazy_init_name_hash(
505 struct index_state
*istate
)
511 struct lazy_entry
*lazy_entries
;
512 struct lazy_dir_thread_data
*td_dir
;
513 struct lazy_name_thread_data
*td_name
;
519 nr_each
= DIV_ROUND_UP(istate
->cache_nr
, lazy_nr_dir_threads
);
521 CALLOC_ARRAY(lazy_entries
, istate
->cache_nr
);
522 CALLOC_ARRAY(td_dir
, lazy_nr_dir_threads
);
523 CALLOC_ARRAY(td_name
, 1);
529 * Build "istate->dir_hash" using n "dir" threads (and a read-only index).
531 for (t
= 0; t
< lazy_nr_dir_threads
; t
++) {
532 struct lazy_dir_thread_data
*td_dir_t
= td_dir
+ t
;
533 td_dir_t
->istate
= istate
;
534 td_dir_t
->lazy_entries
= lazy_entries
;
535 td_dir_t
->k_start
= k_start
;
537 if (k_start
> istate
->cache_nr
)
538 k_start
= istate
->cache_nr
;
539 td_dir_t
->k_end
= k_start
;
540 err
= pthread_create(&td_dir_t
->pthread
, NULL
, lazy_dir_thread_proc
, td_dir_t
);
542 die(_("unable to create lazy_dir thread: %s"), strerror(err
));
544 for (t
= 0; t
< lazy_nr_dir_threads
; t
++) {
545 struct lazy_dir_thread_data
*td_dir_t
= td_dir
+ t
;
546 if (pthread_join(td_dir_t
->pthread
, NULL
))
547 die("unable to join lazy_dir_thread");
552 * Iterate over all index entries and add them to the "istate->name_hash"
553 * using a single "name" background thread.
554 * (Testing showed it wasn't worth running more than 1 thread for this.)
556 * Meanwhile, finish updating the parent directory ref-counts for each
557 * index entry using the current thread. (This step is very fast and
558 * doesn't need threading.)
560 td_name
->istate
= istate
;
561 td_name
->lazy_entries
= lazy_entries
;
562 err
= pthread_create(&td_name
->pthread
, NULL
, lazy_name_thread_proc
, td_name
);
564 die(_("unable to create lazy_name thread: %s"), strerror(err
));
566 lazy_update_dir_ref_counts(istate
, lazy_entries
);
568 err
= pthread_join(td_name
->pthread
, NULL
);
570 die(_("unable to join lazy_name thread: %s"), strerror(err
));
579 static void lazy_init_name_hash(struct index_state
*istate
)
582 if (istate
->name_hash_initialized
)
584 trace_performance_enter();
585 trace2_region_enter("index", "name-hash-init", istate
->repo
);
586 hashmap_init(&istate
->name_hash
, cache_entry_cmp
, NULL
, istate
->cache_nr
);
587 hashmap_init(&istate
->dir_hash
, dir_entry_cmp
, NULL
, istate
->cache_nr
);
589 if (lookup_lazy_params(istate
)) {
591 * Disable item counting and automatic rehashing because
592 * we do per-chain (mod n) locking rather than whole hashmap
593 * locking and we need to prevent the table-size from changing
594 * and bucket items from being redistributed.
596 hashmap_disable_item_counting(&istate
->dir_hash
);
597 threaded_lazy_init_name_hash(istate
);
598 hashmap_enable_item_counting(&istate
->dir_hash
);
601 for (nr
= 0; nr
< istate
->cache_nr
; nr
++)
602 hash_index_entry(istate
, istate
->cache
[nr
]);
605 istate
->name_hash_initialized
= 1;
606 trace2_region_leave("index", "name-hash-init", istate
->repo
);
607 trace_performance_leave("initialize name hash");
611 * A test routine for t/helper/ sources.
613 * Returns the number of threads used or 0 when
614 * the non-threaded code path was used.
616 * Requesting threading WILL NOT override guards
617 * in lookup_lazy_params().
619 int test_lazy_init_name_hash(struct index_state
*istate
, int try_threaded
)
621 lazy_nr_dir_threads
= 0;
622 lazy_try_threaded
= try_threaded
;
624 lazy_init_name_hash(istate
);
626 return lazy_nr_dir_threads
;
629 void add_name_hash(struct index_state
*istate
, struct cache_entry
*ce
)
631 if (istate
->name_hash_initialized
)
632 hash_index_entry(istate
, ce
);
635 void remove_name_hash(struct index_state
*istate
, struct cache_entry
*ce
)
637 if (!istate
->name_hash_initialized
|| !(ce
->ce_flags
& CE_HASHED
))
639 ce
->ce_flags
&= ~CE_HASHED
;
640 hashmap_remove(&istate
->name_hash
, &ce
->ent
, ce
);
643 remove_dir_entry(istate
, ce
);
646 static int slow_same_name(const char *name1
, int len1
, const char *name2
, int len2
)
652 unsigned char c1
= *name1
++;
653 unsigned char c2
= *name2
++;
665 static int same_name(const struct cache_entry
*ce
, const char *name
, int namelen
, int icase
)
667 int len
= ce_namelen(ce
);
670 * Always do exact compare, even if we want a case-ignoring comparison;
671 * we do the quick exact one first, because it will be the common case.
673 if (len
== namelen
&& !memcmp(name
, ce
->name
, len
))
679 return slow_same_name(name
, namelen
, ce
->name
, len
);
682 int index_dir_exists(struct index_state
*istate
, const char *name
, int namelen
)
684 struct dir_entry
*dir
;
686 lazy_init_name_hash(istate
);
687 expand_to_path(istate
, name
, namelen
, 0);
688 dir
= find_dir_entry(istate
, name
, namelen
);
689 return dir
&& dir
->nr
;
692 void adjust_dirname_case(struct index_state
*istate
, char *name
)
694 const char *startPtr
= name
;
695 const char *ptr
= startPtr
;
697 lazy_init_name_hash(istate
);
698 expand_to_path(istate
, name
, strlen(name
), 0);
700 while (*ptr
&& *ptr
!= '/')
704 struct dir_entry
*dir
;
706 dir
= find_dir_entry(istate
, name
, ptr
- name
);
708 memcpy((void *)startPtr
, dir
->name
+ (startPtr
- name
), ptr
- startPtr
);
716 struct cache_entry
*index_file_exists(struct index_state
*istate
, const char *name
, int namelen
, int icase
)
718 struct cache_entry
*ce
;
719 unsigned int hash
= memihash(name
, namelen
);
721 lazy_init_name_hash(istate
);
722 expand_to_path(istate
, name
, namelen
, icase
);
724 ce
= hashmap_get_entry_from_hash(&istate
->name_hash
, hash
, NULL
,
725 struct cache_entry
, ent
);
726 hashmap_for_each_entry_from(&istate
->name_hash
, ce
, ent
) {
727 if (same_name(ce
, name
, namelen
, icase
))
733 void free_name_hash(struct index_state
*istate
)
735 if (!istate
->name_hash_initialized
)
737 istate
->name_hash_initialized
= 0;
739 hashmap_clear(&istate
->name_hash
);
740 hashmap_clear_and_free(&istate
->dir_hash
, struct dir_entry
, ent
);