Skip to content

Commit 9951332

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 2636f29 commit 9951332

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)
@@ -1390,6 +1394,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
13901394
return 0;
13911395
}
13921396

1397+
#ifndef NO_PTHREADS
1398+
/*
1399+
* Require index file to be larger than this threshold before
1400+
* we bother using a thread to verify the SHA.
1401+
* This value was arbitrarily chosen.
1402+
*/
1403+
#define VERIFY_HDR_THRESHOLD 10*1024*1024
1404+
1405+
struct verify_hdr_thread_data
1406+
{
1407+
pthread_t thread_id;
1408+
struct cache_header *hdr;
1409+
size_t size;
1410+
int result;
1411+
};
1412+
1413+
/*
1414+
* A thread proc to run the verify_hdr() computation
1415+
* in a background thread.
1416+
*/
1417+
static void *verify_hdr_thread(void *_data)
1418+
{
1419+
struct verify_hdr_thread_data *p = _data;
1420+
p->result = verify_hdr(p->hdr, (unsigned long)p->size);
1421+
return NULL;
1422+
}
1423+
#endif
1424+
13931425
static int read_index_extension(struct index_state *istate,
13941426
const char *ext, void *data, unsigned long sz)
13951427
{
@@ -1591,6 +1623,9 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
15911623
void *mmap;
15921624
size_t mmap_size;
15931625
struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
1626+
#ifndef NO_PTHREADS
1627+
struct verify_hdr_thread_data verify_hdr_thread_data;
1628+
#endif
15941629

15951630
if (istate->initialized)
15961631
return istate->cache_nr;
@@ -1617,8 +1652,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16171652
close(fd);
16181653

16191654
hdr = mmap;
1655+
#ifdef NO_PTHREADS
16201656
if (verify_hdr(hdr, mmap_size) < 0)
16211657
goto unmap;
1658+
#else
1659+
if (mmap_size < VERIFY_HDR_THRESHOLD) {
1660+
if (verify_hdr(hdr, mmap_size) < 0)
1661+
goto unmap;
1662+
} else {
1663+
verify_hdr_thread_data.hdr = hdr;
1664+
verify_hdr_thread_data.size = mmap_size;
1665+
verify_hdr_thread_data.result = -1;
1666+
if (pthread_create(
1667+
&verify_hdr_thread_data.thread_id, NULL,
1668+
verify_hdr_thread, &verify_hdr_thread_data))
1669+
die_errno("unable to start verify_hdr_thread");
1670+
}
1671+
#endif
16221672

16231673
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
16241674
istate->version = ntohl(hdr->hdr_version);
@@ -1666,6 +1716,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16661716
src_offset += 8;
16671717
src_offset += extsize;
16681718
}
1719+
1720+
#ifndef NO_PTHREADS
1721+
if (mmap_size >= VERIFY_HDR_THRESHOLD) {
1722+
if (pthread_join(verify_hdr_thread_data.thread_id, NULL))
1723+
die_errno("unable to join verify_hdr_thread");
1724+
if (verify_hdr_thread_data.result < 0)
1725+
goto unmap;
1726+
}
1727+
#endif
1728+
16691729
munmap(mmap, mmap_size);
16701730
return istate->cache_nr;
16711731

0 commit comments

Comments
 (0)