index-pack usage of mmap() is unacceptably slower on many OSes other than Linux
It was reported by Randal L. Schwartz <merlyn@stonehenge.com> that indexing the Linux repository ~150MB pack takes about an hour on OS x while it's a minute on Linux. It seems that the OS X mmap() implementation is more than 2 orders of magnitude slower than the Linux one. Linus proposed a patch replacing mmap() with pread() bringing index-pack performance on OS X in line with the Linux one. The performances on Linux also improved by a small margin. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Dec 19, 2006 at 10:53 UTC
6d2fa7f1b489c65e677c18eda5c144dbc5d614ab
1 file changed
+14
-13
index-pack.c
+14
-13
@@ -1,3 +1,8 @@
1
+#define _XOPEN_SOURCE 600
2
+#include <unistd.h>
3
+#include <sys/time.h>
4
+#include <signal.h>
5
+
6
#include "cache.h"
7
#include "delta.h"
8
#include "pack.h"
@@ -6,8 +11,6 @@
11
#include "commit.h"
12
#include "tag.h"
13
#include "tree.h"
9
-#include <sys/time.h>
10
-#include <signal.h>
14
15
static const char index_pack_usage[] =
16
"git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
@@ -87,7 +90,7 @@ static unsigned display_progress(unsigned n, unsigned total, unsigned last_pc)
90
static unsigned char input_buffer[4096];
91
static unsigned long input_offset, input_len, consumed_bytes;
92
static SHA_CTX input_ctx;
90
-static int input_fd, output_fd, mmap_fd;
93
+static int input_fd, output_fd, pack_fd;
94
95
/* Discard current buffer used content. */
96
static void flush(void)
@@ -148,14 +151,14 @@ static const char *open_pack_file(const char *pack_name)
151
output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
152
if (output_fd < 0)
153
die("unable to create %s: %s\n", pack_name, strerror(errno));
151
- mmap_fd = output_fd;
154
+ pack_fd = output_fd;
155
} else {
156
input_fd = open(pack_name, O_RDONLY);
157
if (input_fd < 0)
158
die("cannot open packfile '%s': %s",
159
pack_name, strerror(errno));
160
output_fd = -1;
158
- mmap_fd = input_fd;
161
+ pack_fd = input_fd;
162
}
163
SHA1_Init(&input_ctx);
164
return pack_name;
@@ -279,27 +282,25 @@ static void *get_data_from_pack(struct object_entry *obj)
282
{
283
unsigned long from = obj[0].offset + obj[0].hdr_size;
284
unsigned long len = obj[1].offset - from;
282
- unsigned pg_offset = from % getpagesize();
283
- unsigned char *map, *data;
285
+ unsigned char *src, *data;
286
z_stream stream;
287
int st;
288
287
- map = mmap(NULL, len + pg_offset, PROT_READ, MAP_PRIVATE,
288
- mmap_fd, from - pg_offset);
289
- if (map == MAP_FAILED)
290
- die("cannot mmap pack file: %s", strerror(errno));
289
+ src = xmalloc(len);
290
+ if (pread(pack_fd, src, len, from) != len)
291
+ die("cannot pread pack file: %s", strerror(errno));
292
data = xmalloc(obj->size);
293
memset(&stream, 0, sizeof(stream));
294
stream.next_out = data;
295
stream.avail_out = obj->size;
295
- stream.next_in = map + pg_offset;
296
+ stream.next_in = src;
297
stream.avail_in = len;
298
inflateInit(&stream);
299
while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
300
inflateEnd(&stream);
301
if (st != Z_STREAM_END || stream.total_out != obj->size)
302
die("serious inflate inconsistency");
302
- munmap(map, len + pg_offset);
303
+ free(src);
304
return data;
305
}
306