login
Header Space

 
 

Re: [PATCH] mailsplit and mailinfo: gracefully handle NUL characters

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Avery Pennarun <apenwarr@...>
Cc: Tommy Thorn <tommy-git@...>, <git@...>
Date: Friday, May 16, 2008 - 7:59 pm

Hi,

On Fri, 16 May 2008, Avery Pennarun wrote:


Well, my question was more about fgetc() vs fgets().

If you feel like it, you might benchmark this patch:

-- snipsnap --
diff --git a/builtin-mailsplit.c b/builtin-mailsplit.c
index 021dc16..5d8defd 100644
--- a/builtin-mailsplit.c
+++ b/builtin-mailsplit.c
@@ -45,13 +45,32 @@ static int is_from_line(const char *line, int len)
 /* Could be as small as 64, enough to hold a Unix "From " line. */
 static char buf[4096];
 
+/*
+ *  This is an ugly hack to avoid fgetc(), which is slow, as it is locking.
+ *  The argument "in" must be the same for all calls to this function!
+ */
+static int fast_fgetc(FILE *in)
+{
+	static char buf[4096];
+	static int offset = 0, len = 0;
+
+	if (offset >= len) {
+		len = fread(buf, 1, sizeof(buf), in);
+		offset = 0;
+		if (!len && feof(in))
+			return EOF;
+	}
+
+	return buf[offset++];
+}
+
 /* We cannot use fgets() because our lines can contain NULs */
 int read_line_with_nul(char *buf, int size, FILE *in)
 {
 	int len = 0, c;
 
 	for (;;) {
-		c = fgetc(in);
+		c = fast_fgetc(in);
 		buf[len++] = c;
 		if (c == EOF || c == '\n' || len + 1 >= size)
 			break;
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
git bug: rebase fatal failure, Tommy Thorn, (Thu May 15, 3:27 am)
Re: git bug: rebase fatal failure, Johannes Schindelin, (Fri May 16, 6:41 am)
Re: git bug: rebase fatal failure, Johannes Schindelin, (Fri May 16, 7:01 am)
[PATCH] mailsplit and mailinfo: gracefully handle NUL charac..., Johannes Schindelin, (Fri May 16, 9:03 am)
Re: [PATCH] mailsplit and mailinfo: gracefully handle NUL ch..., Johannes Schindelin, (Thu May 22, 6:38 am)
Re: [PATCH] mailsplit and mailinfo: gracefully handle NUL ch..., Johannes Schindelin, (Fri May 23, 7:21 am)
Re: [PATCH] mailsplit and mailinfo: gracefully handle NUL ch..., Johannes Schindelin, (Fri May 16, 10:32 am)
Re: [PATCH] mailsplit and mailinfo: gracefully handle NUL ch..., Johannes Schindelin, (Fri May 16, 7:59 pm)
Re: [PATCH] mailsplit and mailinfo: gracefully handle NUL ch..., Stephen R. van den Berg, (Sat May 17, 6:07 am)
Re: [PATCH] mailsplit and mailinfo: gracefully handle NUL ch..., Johannes Schindelin, (Sat May 17, 6:18 am)
Re: [PATCH] mailsplit and mailinfo: gracefully handle NUL ch..., Johannes Schindelin, (Fri May 16, 8:26 pm)
speck-geostationary