@matheusbsbs / matheus-bagliano / commits / ea68b0ce9f
Fork 0

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>

Dmitry Potapov committed Feb 21, 2010 at 09:32 UTC ea68b0ce9f8ce8da3e360aed3cbd6720159ffbee
1 file changed +10
sha1_file.c
+10
@@ -2434,6 +2434,8 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
2434 return ret;
2435 }
2436
2437 +#define SMALL_FILE_SIZE (32*1024)
2438 +
2439 int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2440 enum object_type type, const char *path)
2441 {
@@ -2448,6 +2450,14 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2450 else
2451 ret = -1;
2452 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);
2461 } else if (size) {
2462 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2463 ret = index_mem(sha1, buf, size, write_object, type, path);