Skip to content

Commit ea68b0c

Browse files
dmpotgitster
authored andcommitted
hash-object: don't use mmap() for small files
Using read() instead of mmap() can be 39% speed up for 1Kb files and is 1% speed up 1Mb files. For larger files, it is better to use mmap(), because the difference between is not significant, and when there is not enough memory, mmap() performs much better, because it avoids swapping. Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e923eae commit ea68b0c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

sha1_file.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,8 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
24342434
return ret;
24352435
}
24362436

2437+
#define SMALL_FILE_SIZE (32*1024)
2438+
24372439
int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
24382440
enum object_type type, const char *path)
24392441
{
@@ -2448,6 +2450,14 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
24482450
else
24492451
ret = -1;
24502452
strbuf_release(&sbuf);
2453+
} else if (size <= SMALL_FILE_SIZE) {
2454+
char *buf = xmalloc(size);
2455+
if (size == read_in_full(fd, buf, size))
2456+
ret = index_mem(sha1, buf, size, write_object, type,
2457+
path);
2458+
else
2459+
ret = error("short read %s", strerror(errno));
2460+
free(buf);
24512461
} else if (size) {
24522462
void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
24532463
ret = index_mem(sha1, buf, size, write_object, type, path);

0 commit comments

Comments
 (0)