]> git.ipfire.org Git - thirdparty/git.git/blame - preload-index.c
line-log: convert to use parse_pathspec
[thirdparty/git.git] / preload-index.c
CommitLineData
671c9b7e
LT
1/*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4#include "cache.h"
64acde94 5#include "pathspec.h"
46059cc6
JH
6
7#ifdef NO_PTHREADS
8static void preload_index(struct index_state *index, const char **pathspec)
9{
10 ; /* nothing */
11}
12#else
13
671c9b7e
LT
14#include <pthread.h>
15
16/*
17 * Mostly randomly chosen maximum thread counts: we
18 * cap the parallelism to 20 threads, and we want
19 * to have at least 500 lstat's per thread for it to
20 * be worth starting a thread.
21 */
22#define MAX_PARALLEL (20)
23#define THREAD_COST (500)
24
25struct thread_data {
26 pthread_t pthread;
27 struct index_state *index;
28 const char **pathspec;
29 int offset, nr;
30};
31
32static void *preload_thread(void *_data)
33{
34 int nr;
35 struct thread_data *p = _data;
36 struct index_state *index = p->index;
37 struct cache_entry **cep = index->cache + p->offset;
f62ce3de 38 struct cache_def cache;
eb9cb55b 39 struct pathspec pathspec;
671c9b7e 40
eb9cb55b 41 init_pathspec(&pathspec, p->pathspec);
f62ce3de 42 memset(&cache, 0, sizeof(cache));
671c9b7e
LT
43 nr = p->nr;
44 if (nr + p->offset > index->cache_nr)
45 nr = index->cache_nr - p->offset;
46
47 do {
48 struct cache_entry *ce = *cep++;
49 struct stat st;
50
51 if (ce_stage(ce))
52 continue;
125fd984
JH
53 if (S_ISGITLINK(ce->ce_mode))
54 continue;
671c9b7e
LT
55 if (ce_uptodate(ce))
56 continue;
eb9cb55b 57 if (!ce_path_match(ce, &pathspec))
671c9b7e 58 continue;
f62ce3de
LT
59 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
60 continue;
671c9b7e
LT
61 if (lstat(ce->name, &st))
62 continue;
7c4ea599 63 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY))
671c9b7e
LT
64 continue;
65 ce_mark_uptodate(ce);
66 } while (--nr > 0);
eb9cb55b 67 free_pathspec(&pathspec);
671c9b7e
LT
68 return NULL;
69}
70
71static void preload_index(struct index_state *index, const char **pathspec)
72{
73 int threads, i, work, offset;
74 struct thread_data data[MAX_PARALLEL];
75
76 if (!core_preload_index)
77 return;
78
79 threads = index->cache_nr / THREAD_COST;
80 if (threads < 2)
81 return;
82 if (threads > MAX_PARALLEL)
83 threads = MAX_PARALLEL;
84 offset = 0;
98cb6f30 85 work = DIV_ROUND_UP(index->cache_nr, threads);
671c9b7e
LT
86 for (i = 0; i < threads; i++) {
87 struct thread_data *p = data+i;
88 p->index = index;
89 p->pathspec = pathspec;
90 p->offset = offset;
91 p->nr = work;
92 offset += work;
93 if (pthread_create(&p->pthread, NULL, preload_thread, p))
94 die("unable to create threaded lstat");
95 }
96 for (i = 0; i < threads; i++) {
97 struct thread_data *p = data+i;
98 if (pthread_join(p->pthread, NULL))
99 die("unable to join threaded lstat");
100 }
101}
46059cc6 102#endif
671c9b7e
LT
103
104int read_index_preload(struct index_state *index, const char **pathspec)
105{
106 int retval = read_index(index);
107
108 preload_index(index, pathspec);
109 return retval;
110}