Skip to content

Commit a56c4f9

Browse files
kgybelsdscho
authored andcommitted
packed_ref_cache: don't use mmap() for small files
Take a hint from commit ea68b0c (hash-object: don't use mmap() for small files, 2010-02-21) and use read() instead of mmap() for small packed-refs files. This also fixes #1410 (where xmmap() returns NULL for zero length[1], for which munmap() later fails). Alternatively, we could simply check for NULL before munmap(), or introduce xmunmap() that could be used together with xmmap(). However, always setting snapshot->buf to a valid pointer, by relying on xmalloc(0)'s fallback to 1-byte allocation, makes using snapshots easier. [1] Logic introduced in commit 9130ac1 (Better error messages for corrupt databases, 2007-01-11) This was cherry-picked from upstream's `pu` branch so that the fix is included in Git for Windows v2.16.0. Signed-off-by: Kim Gybels <kgybels@infogroep.be> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 90e90ed commit a56c4f9

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

refs/packed-backend.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,8 @@ static void verify_buffer_safe(struct snapshot *snapshot)
455455
last_line, eof - last_line);
456456
}
457457

458+
#define SMALL_FILE_SIZE (32*1024)
459+
458460
/*
459461
* Depending on `mmap_strategy`, either mmap or read the contents of
460462
* the `packed-refs` file into the snapshot. Return 1 if the file
@@ -489,21 +491,17 @@ static int load_contents(struct snapshot *snapshot)
489491
die_errno("couldn't stat %s", snapshot->refs->path);
490492
size = xsize_t(st.st_size);
491493

492-
switch (mmap_strategy) {
493-
case MMAP_NONE:
494+
if (size <= SMALL_FILE_SIZE || mmap_strategy == MMAP_NONE) {
494495
snapshot->buf = xmalloc(size);
495496
bytes_read = read_in_full(fd, snapshot->buf, size);
496497
if (bytes_read < 0 || bytes_read != size)
497498
die_errno("couldn't read %s", snapshot->refs->path);
498499
snapshot->eof = snapshot->buf + size;
499500
snapshot->mmapped = 0;
500-
break;
501-
case MMAP_TEMPORARY:
502-
case MMAP_OK:
501+
} else {
503502
snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
504503
snapshot->eof = snapshot->buf + size;
505504
snapshot->mmapped = 1;
506-
break;
507505
}
508506
close(fd);
509507

0 commit comments

Comments
 (0)