Skip to content

Commit 271da9e

Browse files
jeffhostetlerdscho
authored andcommitted
read-cache: run verify_hdr() in background thread
This is a performance optimization. Teach do_read_index() to call verify_hdr() using a thread and allow SHA1 verification to run concurrently with the parsing of index-entries and extensions. For large index files, this cuts the startup time in half. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent d5251c7 commit 271da9e

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

read-cache.c

Lines changed: 60 additions & 0 deletions
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)
@@ -1397,6 +1401,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
13971401
return 0;
13981402
}
13991403

1404+
#ifndef NO_PTHREADS
1405+
/*
1406+
* Require index file to be larger than this threshold before
1407+
* we bother using a thread to verify the SHA.
1408+
* This value was arbitrarily chosen.
1409+
*/
1410+
#define VERIFY_HDR_THRESHOLD 10*1024*1024
1411+
1412+
struct verify_hdr_thread_data
1413+
{
1414+
pthread_t thread_id;
1415+
struct cache_header *hdr;
1416+
size_t size;
1417+
int result;
1418+
};
1419+
1420+
/*
1421+
* A thread proc to run the verify_hdr() computation
1422+
* in a background thread.
1423+
*/
1424+
static void *verify_hdr_thread(void *_data)
1425+
{
1426+
struct verify_hdr_thread_data *p = _data;
1427+
p->result = verify_hdr(p->hdr, (unsigned long)p->size);
1428+
return NULL;
1429+
}
1430+
#endif
1431+
14001432
static int read_index_extension(struct index_state *istate,
14011433
const char *ext, void *data, unsigned long sz)
14021434
{
@@ -1584,6 +1616,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
15841616
void *mmap;
15851617
size_t mmap_size;
15861618
struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
1619+
#ifndef NO_PTHREADS
1620+
struct verify_hdr_thread_data verify_hdr_thread_data;
1621+
#endif
15871622

15881623
if (istate->initialized)
15891624
return istate->cache_nr;
@@ -1610,8 +1645,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16101645
close(fd);
16111646

16121647
hdr = mmap;
1648+
#ifdef NO_PTHREADS
16131649
if (verify_hdr(hdr, mmap_size) < 0)
16141650
goto unmap;
1651+
#else
1652+
if (mmap_size < VERIFY_HDR_THRESHOLD) {
1653+
if (verify_hdr(hdr, mmap_size) < 0)
1654+
goto unmap;
1655+
} else {
1656+
verify_hdr_thread_data.hdr = hdr;
1657+
verify_hdr_thread_data.size = mmap_size;
1658+
verify_hdr_thread_data.result = -1;
1659+
if (pthread_create(
1660+
&verify_hdr_thread_data.thread_id, NULL,
1661+
verify_hdr_thread, &verify_hdr_thread_data))
1662+
die_errno("unable to start verify_hdr_thread");
1663+
}
1664+
#endif
16151665

16161666
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
16171667
istate->version = ntohl(hdr->hdr_version);
@@ -1659,6 +1709,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16591709
src_offset += 8;
16601710
src_offset += extsize;
16611711
}
1712+
1713+
#ifndef NO_PTHREADS
1714+
if (mmap_size >= VERIFY_HDR_THRESHOLD) {
1715+
if (pthread_join(verify_hdr_thread_data.thread_id, NULL))
1716+
die_errno("unable to join verify_hdr_thread");
1717+
if (verify_hdr_thread_data.result < 0)
1718+
goto unmap;
1719+
}
1720+
#endif
1721+
16621722
munmap(mmap, mmap_size);
16631723
return istate->cache_nr;
16641724

0 commit comments

Comments
 (0)