Skip to content

Commit 312cc27

Browse files
jeffhostetlerGit for Windows Build Agent
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 ed5f577 commit 312cc27

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

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

15791614
if (istate->initialized)
15801615
return istate->cache_nr;
@@ -1601,8 +1636,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16011636
close(fd);
16021637

16031638
hdr = mmap;
1639+
#ifdef NO_PTHREADS
16041640
if (verify_hdr(hdr, mmap_size) < 0)
16051641
goto unmap;
1642+
#else
1643+
if (mmap_size < VERIFY_HDR_THRESHOLD) {
1644+
if (verify_hdr(hdr, mmap_size) < 0)
1645+
goto unmap;
1646+
} else {
1647+
verify_hdr_thread_data.hdr = hdr;
1648+
verify_hdr_thread_data.size = mmap_size;
1649+
verify_hdr_thread_data.result = -1;
1650+
if (pthread_create(
1651+
&verify_hdr_thread_data.thread_id, NULL,
1652+
verify_hdr_thread, &verify_hdr_thread_data))
1653+
die_errno("unable to start verify_hdr_thread");
1654+
}
1655+
#endif
16061656

16071657
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
16081658
istate->version = ntohl(hdr->hdr_version);
@@ -1650,6 +1700,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16501700
src_offset += 8;
16511701
src_offset += extsize;
16521702
}
1703+
1704+
#ifndef NO_PTHREADS
1705+
if (mmap_size >= VERIFY_HDR_THRESHOLD) {
1706+
if (pthread_join(verify_hdr_thread_data.thread_id, NULL))
1707+
die_errno("unable to join verify_hdr_thread");
1708+
if (verify_hdr_thread_data.result < 0)
1709+
goto unmap;
1710+
}
1711+
#endif
1712+
16531713
munmap(mmap, mmap_size);
16541714
return istate->cache_nr;
16551715

0 commit comments

Comments
 (0)