2 * Copyright (C) 2008 Linus Torvalds
8 * Mostly randomly chosen maximum thread counts: we
9 * cap the parallelism to 20 threads, and we want
10 * to have at least 500 lstat's per thread for it to
11 * be worth starting a thread.
13 #define MAX_PARALLEL (20)
14 #define THREAD_COST (500)
18 struct index_state *index;
19 const char **pathspec;
23 static void *preload_thread(void *_data)
26 struct thread_data *p = _data;
27 struct index_state *index = p->index;
28 struct cache_entry **cep = index->cache + p->offset;
31 if (nr + p->offset > index->cache_nr)
32 nr = index->cache_nr - p->offset;
35 struct cache_entry *ce = *cep++;
42 if (!ce_path_match(ce, p->pathspec))
44 if (lstat(ce->name, &st))
46 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY))
53 static void preload_index(struct index_state *index, const char **pathspec)
55 int threads, i, work, offset;
56 struct thread_data data[MAX_PARALLEL];
58 if (!core_preload_index)
61 threads = index->cache_nr / THREAD_COST;
64 if (threads > MAX_PARALLEL)
65 threads = MAX_PARALLEL;
67 work = (index->cache_nr + threads - 1) / threads;
68 for (i = 0; i < threads; i++) {
69 struct thread_data *p = data+i;
71 p->pathspec = pathspec;
75 if (pthread_create(&p->pthread, NULL, preload_thread, p))
76 die("unable to create threaded lstat");
78 for (i = 0; i < threads; i++) {
79 struct thread_data *p = data+i;
80 if (pthread_join(p->pthread, NULL))
81 die("unable to join threaded lstat");
85 int read_index_preload(struct index_state *index, const char **pathspec)
87 int retval = read_index(index);
89 preload_index(index, pathspec);