read-cache.c: reduce branching based on HAVE_THREADS

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Nov 3, 2018 at 09:48 UTC 88168b9b43a17d5f7712d15cf5ccfed517eb4470
1 file changed +9 -10
read-cache.c
+9 -10
@@ -2172,7 +2172,6 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
2172
2173 src_offset = sizeof(*hdr);
2174
2175 - if (HAVE_THREADS) {
2175 nr_threads = git_config_get_index_threads();
2176
2177 /* TODO: does creating more threads than cores help? */
@@ -2183,6 +2182,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
2182 nr_threads = cpus;
2183 }
2184
2185 + if (!HAVE_THREADS)
2186 + nr_threads = 1;
2187 +
2188 if (nr_threads > 1) {
2189 extension_offset = read_eoie_extension(mmap, mmap_size);
2190 if (extension_offset) {
@@ -2210,20 +2212,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
2212 } else {
2213 src_offset += load_all_cache_entries(istate, mmap, mmap_size, src_offset);
2214 }
2213 - } else {
2214 - src_offset += load_all_cache_entries(istate, mmap, mmap_size, src_offset);
2215 - }
2215
2216 istate->timestamp.sec = st.st_mtime;
2217 istate->timestamp.nsec = ST_MTIME_NSEC(st);
2218
2219 /* if we created a thread, join it otherwise load the extensions on the primary thread */
2221 - if (HAVE_THREADS && extension_offset) {
2220 + if (extension_offset) {
2221 int ret = pthread_join(p.pthread, NULL);
2222 if (ret)
2223 die(_("unable to join load_index_extensions thread: %s"), strerror(ret));
2225 - }
2226 - if (!extension_offset) {
2224 + } else {
2225 p.src_offset = src_offset;
2226 load_index_extensions(&p);
2227 }
@@ -2745,8 +2743,10 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2743 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
2744 return -1;
2745
2748 - if (HAVE_THREADS) {
2746 + if (HAVE_THREADS)
2747 nr_threads = git_config_get_index_threads();
2748 + else
2749 + nr_threads = 1;
2750
2751 if (nr_threads != 1) {
2752 int ieot_blocks, cpus;
@@ -2777,7 +2777,6 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2777 ieot_entries = DIV_ROUND_UP(entries, ieot_blocks);
2778 }
2779 }
2780 - }
2780
2781 offset = lseek(newfd, 0, SEEK_CUR);
2782 if (offset < 0) {
@@ -2861,7 +2860,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2860 * strip_extensions parameter as we need it when loading the shared
2861 * index.
2862 */
2864 - if (HAVE_THREADS && ieot) {
2863 + if (ieot) {
2864 struct strbuf sb = STRBUF_INIT;
2865
2866 write_ieot_extension(&sb, ieot);