From: Junio C Hamano <junkio@cox.net>
To: Linus Torvalds <torvalds@osdl.org>
Cc: git@vger.kernel.org, Nguyen Thai Ngoc Duy <pclouds@gmail.com>
Subject: Re: git-diff opens too many files?
Date: Mon, 20 Nov 2006 13:02:24 -0800 [thread overview]
Message-ID: <7vfycd25nj.fsf@assigned-by-dhcp.cox.net> (raw)
In-Reply-To: <Pine.LNX.4.64.0611200832450.3692@woody.osdl.org> (Linus Torvalds's message of "Mon, 20 Nov 2006 09:00:55 -0800 (PST)")
Linus Torvalds <torvalds@osdl.org> writes:
> Junio? This is your speciality, I'm not sure how painful it would be to
> unmap and remap on demand.. (or switch it to some kind of "keep the last
> <n> mmaps active" kind of thing to avoid having thousands and thousands of
> mmaps active).
>
> One simple thing that might be worth it is to simply _not_ use mmap() at
> all for small files. If a file is less than 1kB, it might be better to do
> a malloc() and a read() - partly because it avoids having tons of file
> descriptors, but partly because it's also more efficient from a virtual
> memory usage perspective (not that you're probably very likely to ever
> really hit that problem in practice).
>
> Nguyen - that "use malloc+read" thing might be a quick workaround, but
> only if you have tons of _small_ files (and if you can't easily just
> increase file-max).
So here is a lunch-time hack to get Nguyen unstuck.
-- >8 --
[PATCH] diff.c: avoid mmap() of small files in populate_filespec()
This would hopefully behave better in VM usage. It is not a
real fix if you are dealing with truly huge diff, in which case
we would have to LRU out the data in memory for filespecs that
are not used in immediate future.
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
diff --git a/diff.c b/diff.c
index 3315378..af50b6f 100644
--- a/diff.c
+++ b/diff.c
@@ -1294,11 +1294,22 @@ int diff_populate_filespec(struct diff_f
fd = open(s->path, O_RDONLY);
if (fd < 0)
goto err_empty;
- s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (s->size < MINIMUM_MMAP) {
+ s->data = xmalloc(s->size);
+ s->should_free = 1;
+ if (xread(fd, s->data, s->size) != s->size) {
+ free(s->data);
+ goto err_empty;
+ }
+ }
+ else {
+ s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE,
+ fd, 0);
+ s->should_munmap = 1;
+ }
close(fd);
if (s->data == MAP_FAILED)
goto err_empty;
- s->should_munmap = 1;
}
else {
char type[20];
diff --git a/diffcore.h b/diffcore.h
index 2249bc2..f129aa0 100644
--- a/diffcore.h
+++ b/diffcore.h
@@ -21,6 +21,7 @@
#define DEFAULT_MERGE_SCORE 36000 /* maximum for break-merge to happen 60%) */
#define MINIMUM_BREAK_SIZE 400 /* do not break a file smaller than this */
+#define MINIMUM_MMAP 4096 /* do not mmap a file smaller than this */
struct diff_filespec {
unsigned char sha1[20];
prev parent reply other threads:[~2006-11-20 21:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-11-20 10:12 git-diff opens too many files? Nguyen Thai Ngoc Duy
2006-11-20 15:20 ` Johannes Schindelin
2006-11-20 15:32 ` Nguyen Thai Ngoc Duy
2006-11-20 15:48 ` Johannes Schindelin
2006-11-20 17:00 ` Linus Torvalds
2006-11-20 19:51 ` Junio C Hamano
2006-11-20 21:02 ` Junio C Hamano [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7vfycd25nj.fsf@assigned-by-dhcp.cox.net \
--to=junkio@cox.net \
--cc=git@vger.kernel.org \
--cc=pclouds@gmail.com \
--cc=torvalds@osdl.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox