2 * Copyright (C) 2008 Linus Torvalds
7 static void preload_index(struct index_state
*index
, const char **pathspec
)
16 * Mostly randomly chosen maximum thread counts: we
17 * cap the parallelism to 20 threads, and we want
18 * to have at least 500 lstat's per thread for it to
19 * be worth starting a thread.
21 #define MAX_PARALLEL (20)
22 #define THREAD_COST (500)
26 struct index_state
*index
;
27 const char **pathspec
;
31 static void *preload_thread(void *_data
)
34 struct thread_data
*p
= _data
;
35 struct index_state
*index
= p
->index
;
36 struct cache_entry
**cep
= index
->cache
+ p
->offset
;
37 struct cache_def cache
;
39 memset(&cache
, 0, sizeof(cache
));
41 if (nr
+ p
->offset
> index
->cache_nr
)
42 nr
= index
->cache_nr
- p
->offset
;
45 struct cache_entry
*ce
= *cep
++;
50 if (S_ISGITLINK(ce
->ce_mode
))
54 if (!ce_path_match(ce
, p
->pathspec
))
56 if (threaded_has_symlink_leading_path(&cache
, ce
->name
, ce_namelen(ce
)))
58 if (lstat(ce
->name
, &st
))
60 if (ie_match_stat(index
, ce
, &st
, CE_MATCH_RACY_IS_DIRTY
))
67 static void preload_index(struct index_state
*index
, const char **pathspec
)
69 int threads
, i
, work
, offset
;
70 struct thread_data data
[MAX_PARALLEL
];
72 if (!core_preload_index
)
75 threads
= index
->cache_nr
/ THREAD_COST
;
78 if (threads
> MAX_PARALLEL
)
79 threads
= MAX_PARALLEL
;
81 work
= DIV_ROUND_UP(index
->cache_nr
, threads
);
82 for (i
= 0; i
< threads
; i
++) {
83 struct thread_data
*p
= data
+i
;
85 p
->pathspec
= pathspec
;
89 if (pthread_create(&p
->pthread
, NULL
, preload_thread
, p
))
90 die("unable to create threaded lstat");
92 for (i
= 0; i
< threads
; i
++) {
93 struct thread_data
*p
= data
+i
;
94 if (pthread_join(p
->pthread
, NULL
))
95 die("unable to join threaded lstat");
100 int read_index_preload(struct index_state
*index
, const char **pathspec
)
102 int retval
= read_index(index
);
104 preload_index(index
, pathspec
);