2 * Copyright (C) 2008 Linus Torvalds
10 static void preload_index(struct index_state *index,
11 const struct pathspec *pathspec)
20 * Mostly randomly chosen maximum thread counts: we
21 * cap the parallelism to 20 threads, and we want
22 * to have at least 500 lstat's per thread for it to
23 * be worth starting a thread.
25 #define MAX_PARALLEL (20)
26 #define THREAD_COST (500)
30 struct index_state *index;
31 struct pathspec pathspec;
35 static void *preload_thread(void *_data)
38 struct thread_data *p = _data;
39 struct index_state *index = p->index;
40 struct cache_entry **cep = index->cache + p->offset;
41 struct cache_def cache = CACHE_DEF_INIT;
44 if (nr + p->offset > index->cache_nr)
45 nr = index->cache_nr - p->offset;
48 struct cache_entry *ce = *cep++;
53 if (S_ISGITLINK(ce->ce_mode))
57 if (ce_skip_worktree(ce))
59 if (ce->ce_flags & CE_FSMONITOR_VALID)
61 if (!ce_path_match(index, ce, &p->pathspec, NULL))
63 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
65 if (lstat(ce->name, &st))
67 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
70 mark_fsmonitor_valid(ce);
72 cache_def_clear(&cache);
76 static void preload_index(struct index_state *index,
77 const struct pathspec *pathspec)
79 int threads, i, work, offset;
80 struct thread_data data[MAX_PARALLEL];
82 if (!core_preload_index)
85 threads = index->cache_nr / THREAD_COST;
86 if ((index->cache_nr > 1) && (threads < 2) && getenv("GIT_FORCE_PRELOAD_TEST"))
90 trace_performance_enter();
91 if (threads > MAX_PARALLEL)
92 threads = MAX_PARALLEL;
94 work = DIV_ROUND_UP(index->cache_nr, threads);
95 memset(&data, 0, sizeof(data));
96 for (i = 0; i < threads; i++) {
97 struct thread_data *p = data+i;
100 copy_pathspec(&p->pathspec, pathspec);
104 if (pthread_create(&p->pthread, NULL, preload_thread, p))
105 die("unable to create threaded lstat");
107 for (i = 0; i < threads; i++) {
108 struct thread_data *p = data+i;
109 if (pthread_join(p->pthread, NULL))
110 die("unable to join threaded lstat");
112 trace_performance_leave("preload index");
116 int read_index_preload(struct index_state *index,
117 const struct pathspec *pathspec)
119 int retval = read_index(index);
121 preload_index(index, pathspec);