Skip to content

Commit 498e713

Browse files
committed
Merge pull request #978 from jeffhostetler/jeffhostetler/thread_verify_hdr
read-cache: run verify_hdr() in background thread
2 parents 1ef5070 + 68f2d9b commit 498e713

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

read-cache.c

+60
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "split-index.h"
2020
#include "utf8.h"
2121

22+
#ifndef NO_PTHREADS
23+
#include <pthread.h>
24+
#endif
25+
2226
/* Mask for the name length in ce_flags in the on-disk index */
2327

2428
#define CE_NAMEMASK (0x0fff)
@@ -1462,6 +1466,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
14621466
return 0;
14631467
}
14641468

1469+
#ifndef NO_PTHREADS
1470+
/*
1471+
* Require index file to be larger than this threshold before
1472+
* we bother using a thread to verify the SHA.
1473+
* This value was arbitrarily chosen.
1474+
*/
1475+
#define VERIFY_HDR_THRESHOLD 10*1024*1024
1476+
1477+
struct verify_hdr_thread_data
1478+
{
1479+
pthread_t thread_id;
1480+
struct cache_header *hdr;
1481+
size_t size;
1482+
int result;
1483+
};
1484+
1485+
/*
1486+
* A thread proc to run the verify_hdr() computation
1487+
* in a background thread.
1488+
*/
1489+
static void *verify_hdr_thread(void *_data)
1490+
{
1491+
struct verify_hdr_thread_data *p = _data;
1492+
p->result = verify_hdr(p->hdr, (unsigned long)p->size);
1493+
return NULL;
1494+
}
1495+
#endif
1496+
14651497
static int read_index_extension(struct index_state *istate,
14661498
const char *ext, void *data, unsigned long sz)
14671499
{
@@ -1664,6 +1696,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16641696
void *mmap;
16651697
size_t mmap_size;
16661698
struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
1699+
#ifndef NO_PTHREADS
1700+
struct verify_hdr_thread_data verify_hdr_thread_data;
1701+
#endif
16671702

16681703
if (istate->initialized)
16691704
return istate->cache_nr;
@@ -1690,8 +1725,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16901725
close(fd);
16911726

16921727
hdr = mmap;
1728+
#ifdef NO_PTHREADS
16931729
if (verify_hdr(hdr, mmap_size) < 0)
16941730
goto unmap;
1731+
#else
1732+
if (mmap_size < VERIFY_HDR_THRESHOLD) {
1733+
if (verify_hdr(hdr, mmap_size) < 0)
1734+
goto unmap;
1735+
} else {
1736+
verify_hdr_thread_data.hdr = hdr;
1737+
verify_hdr_thread_data.size = mmap_size;
1738+
verify_hdr_thread_data.result = -1;
1739+
if (pthread_create(
1740+
&verify_hdr_thread_data.thread_id, NULL,
1741+
verify_hdr_thread, &verify_hdr_thread_data))
1742+
die_errno("unable to start verify_hdr_thread");
1743+
}
1744+
#endif
16951745

16961746
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
16971747
istate->version = ntohl(hdr->hdr_version);
@@ -1739,6 +1789,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
17391789
src_offset += 8;
17401790
src_offset += extsize;
17411791
}
1792+
1793+
#ifndef NO_PTHREADS
1794+
if (mmap_size >= VERIFY_HDR_THRESHOLD) {
1795+
if (pthread_join(verify_hdr_thread_data.thread_id, NULL))
1796+
die_errno("unable to join verify_hdr_thread");
1797+
if (verify_hdr_thread_data.result < 0)
1798+
goto unmap;
1799+
}
1800+
#endif
1801+
17421802
munmap(mmap, mmap_size);
17431803
return istate->cache_nr;
17441804

0 commit comments

Comments
 (0)