read_packed_refs(): use mmap to read the `packed-refs` file

It's still done in a pretty stupid way, involving more data copying than necessary. That will improve in future commits. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 13, 2017 at 19:15 UTC 49a03ef46667ad5074f1e602e392b7763c686205
1 file changed +32 -10
refs/packed-backend.c
+32 -10
@@ -215,8 +215,12 @@ static NORETURN void die_invalid_line(const char *path,
215 */
216 static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
217 {
218 - FILE *f;
218 struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
219 + int fd;
220 + struct stat st;
221 + size_t size;
222 + char *buf;
223 + const char *pos, *eol, *eof;
224 struct ref_entry *last = NULL;
225 struct strbuf line = STRBUF_INIT;
226 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
@@ -227,8 +231,8 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
231 packed_refs->cache = create_ref_cache(NULL, NULL);
232 packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
233
230 - f = fopen(refs->path, "r");
231 - if (!f) {
234 + fd = open(refs->path, O_RDONLY);
235 + if (fd < 0) {
236 if (errno == ENOENT) {
237 /*
238 * This is OK; it just means that no
@@ -241,16 +245,27 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
245 }
246 }
247
244 - stat_validity_update(&packed_refs->validity, fileno(f));
248 + stat_validity_update(&packed_refs->validity, fd);
249 +
250 + if (fstat(fd, &st) < 0)
251 + die_errno("couldn't stat %s", refs->path);
252 +
253 + size = xsize_t(st.st_size);
254 + buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
255 + pos = buf;
256 + eof = buf + size;
257
258 dir = get_ref_dir(packed_refs->cache->root);
247 - while (strbuf_getwholeline(&line, f, '\n') != EOF) {
259 + while (pos < eof) {
260 struct object_id oid;
261 const char *refname;
262 const char *traits;
263
252 - if (!line.len || line.buf[line.len - 1] != '\n')
253 - die_unterminated_line(refs->path, line.buf, line.len);
264 + eol = memchr(pos, '\n', eof - pos);
265 + if (!eol)
266 + die_unterminated_line(refs->path, pos, eof - pos);
267 +
268 + strbuf_add(&line, pos, eol + 1 - pos);
269
270 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
271 if (strstr(traits, " fully-peeled "))
@@ -258,7 +273,7 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
273 else if (strstr(traits, " peeled "))
274 peeled = PEELED_TAGS;
275 /* perhaps other traits later as well */
261 - continue;
276 + goto next_line;
277 }
278
279 refname = parse_ref_line(&line, &oid);
@@ -291,11 +306,18 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
306 } else {
307 die_invalid_line(refs->path, line.buf, line.len);
308 }
309 +
310 + next_line:
311 + /* The "+ 1" is for the LF character. */
312 + pos = eol + 1;
313 + strbuf_reset(&line);
314 }
315
296 - fclose(f);
297 - strbuf_release(&line);
316 + if (munmap(buf, size))
317 + die_errno("error ummapping packed-refs file");
318 + close(fd);
319
320 + strbuf_release(&line);
321 return packed_refs;
322 }
323