Skip to content

Commit 00c15af

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 0ffaae0 commit 00c15af

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)
@@ -1393,6 +1397,34 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
13931397
return 0;
13941398
}
13951399

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

15851620
if (istate->initialized)
15861621
return istate->cache_nr;
@@ -1607,8 +1642,23 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16071642
close(fd);
16081643

16091644
hdr = mmap;
1645+
#ifdef NO_PTHREADS
16101646
if (verify_hdr(hdr, mmap_size) < 0)
16111647
goto unmap;
1648+
#else
1649+
if (mmap_size < VERIFY_HDR_THRESHOLD) {
1650+
if (verify_hdr(hdr, mmap_size) < 0)
1651+
goto unmap;
1652+
} else {
1653+
verify_hdr_thread_data.hdr = hdr;
1654+
verify_hdr_thread_data.size = mmap_size;
1655+
verify_hdr_thread_data.result = -1;
1656+
if (pthread_create(
1657+
&verify_hdr_thread_data.thread_id, NULL,
1658+
verify_hdr_thread, &verify_hdr_thread_data))
1659+
die_errno("unable to start verify_hdr_thread");
1660+
}
1661+
#endif
16121662

16131663
hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
16141664
istate->version = ntohl(hdr->hdr_version);
@@ -1656,6 +1706,16 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
16561706
src_offset += 8;
16571707
src_offset += extsize;
16581708
}
1709+
1710+
#ifndef NO_PTHREADS
1711+
if (mmap_size >= VERIFY_HDR_THRESHOLD) {
1712+
if (pthread_join(verify_hdr_thread_data.thread_id, NULL))
1713+
die_errno("unable to join verify_hdr_thread");
1714+
if (verify_hdr_thread_data.result < 0)
1715+
goto unmap;
1716+
}
1717+
#endif
1718+
16591719
munmap(mmap, mmap_size);
16601720
return istate->cache_nr;
16611721

0 commit comments

Comments
 (0)