gitweb: make gitweb_check_feature a boolean wrapper
[git/jnareb-git.git] / preload-index.c
bloba6855837a4b85e475d556527335d89c8b0e3a02c
1 /*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4 #include "cache.h"
5 #include <pthread.h>
7 /*
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)
16 struct thread_data {
17 pthread_t pthread;
18 struct index_state *index;
19 const char **pathspec;
20 int offset, nr;
23 static void *preload_thread(void *_data)
25 int nr;
26 struct thread_data *p = _data;
27 struct index_state *index = p->index;
28 struct cache_entry **cep = index->cache + p->offset;
30 nr = p->nr;
31 if (nr + p->offset > index->cache_nr)
32 nr = index->cache_nr - p->offset;
34 do {
35 struct cache_entry *ce = *cep++;
36 struct stat st;
38 if (ce_stage(ce))
39 continue;
40 if (ce_uptodate(ce))
41 continue;
42 if (!ce_path_match(ce, p->pathspec))
43 continue;
44 if (lstat(ce->name, &st))
45 continue;
46 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY))
47 continue;
48 ce_mark_uptodate(ce);
49 } while (--nr > 0);
50 return NULL;
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)
59 return;
61 threads = index->cache_nr / THREAD_COST;
62 if (threads < 2)
63 return;
64 if (threads > MAX_PARALLEL)
65 threads = MAX_PARALLEL;
66 offset = 0;
67 work = (index->cache_nr + threads - 1) / threads;
68 for (i = 0; i < threads; i++) {
69 struct thread_data *p = data+i;
70 p->index = index;
71 p->pathspec = pathspec;
72 p->offset = offset;
73 p->nr = work;
74 offset += work;
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);
90 return retval;