[PATCH v2] don't use mmap() to hash files
- From
Dmitry Potapov <dpotapov@gmail.com>
- Date
- Feb 14, 2010, 03:05 UTC
- Message-ID
- <20100214030504.GA17952@dpotapov.dyndns.org>
- In-Reply-To
- <20100214011812.GA2175@dpotapov.dyndns.org>
If a mmapped file is changed by another program during git-add, it causes the repository corruption. Disabling mmap() in index_fd() does not have any negative impact on the overall speed of Git. In fact, it makes git hash-object to work slightly faster. Here is the best result before and after patch based on 5 runs on the Linix kernel repository:
Before:
$ git ls-files | time git hash-object --stdin-path > /dev/null 2.15user 0.36system 0:02.52elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+103248minor)pagefaults 0swaps
After:
$ git ls-files | time ../git/git hash-object --stdin-path > /dev/null 2.09user 0.33system 0:02.42elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+1073minor)pagefaults 0swaps
Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> ---
In this version, I have improved the hint value for regular files to avoid useless re-allocation and copy.
sha1_file.c | 27 +++++++++++---------------- 1 files changed, 11 insertions(+), 16 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c index 657825e..26c6231 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -2438,22 +2438,17 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path) { int ret; - size_t size = xsize_t(st->st_size); - - if (!S_ISREG(st->st_mode)) { - struct strbuf sbuf = STRBUF_INIT; - if (strbuf_read(&sbuf, fd, 4096) >= 0) - ret = index_mem(sha1, sbuf.buf, sbuf.len, write_object, - type, path); - else - ret = -1; - strbuf_release(&sbuf); - } else if (size) { - void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); - ret = index_mem(sha1, buf, size, write_object, type, path); - munmap(buf, size); - } else - ret = index_mem(sha1, NULL, size, write_object, type, path); + struct strbuf sbuf = STRBUF_INIT; + /* for regular files, we supply the real file size, otherwise + `size' is just a hint */ + size_t size = S_ISREG(st->st_mode) ? xsize_t(st->st_size) : 4096; + + if (strbuf_read(&sbuf, fd, size) >= 0) + ret = index_mem(sha1, sbuf.buf, sbuf.len, write_object, + type, path); + else + ret = -1; + strbuf_release(&sbuf); close(fd); return ret; }
-- 1.7.0