@matheusbsbs / matheus-bagliano / commits / 8b6087fb25
Fork 0

Remove preemptive allocations.

Careful profiling shows that we spend more time guessing what pattern allocation will have, whereas we can delay it only at the point where add_rfc2047 will be used and don't allocate huge memory area for the many cases where it's not. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 16, 2007 at 10:19 UTC 8b6087fb25068d6af927f112a93dc056930f3108
1 file changed +5 -30
commit.c
+5 -30
@@ -501,6 +501,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
501 return;
502
503 needquote:
504 + strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
505 strbuf_addf(sb, "=?%s?q?", encoding);
506 for (i = last = 0; i < len; i++) {
507 unsigned ch = line[i] & 0xFF;
@@ -520,14 +521,6 @@ needquote:
521 strbuf_addstr(sb, "?=");
522 }
523
523 -static unsigned long bound_rfc2047(unsigned long len, const char *encoding)
524 -{
525 - /* upper bound of q encoded string of length 'len' */
526 - unsigned long elen = strlen(encoding);
527 -
528 - return len * 3 + elen + 100;
529 -}
530 -
524 static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
525 const char *line, enum date_mode dmode,
526 const char *encoding)
@@ -560,8 +553,7 @@ static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb
553 add_rfc2047(sb, line, display_name_length, encoding);
554 strbuf_add(sb, name_tail, namelen - display_name_length);
555 strbuf_addch(sb, '\n');
563 - }
564 - else {
556 + } else {
557 strbuf_addf(sb, "%s: %.*s%.*s\n", what,
558 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
559 filler, namelen, line);
@@ -955,19 +947,12 @@ static void pp_header(enum cmit_fmt fmt,
947 * FULLER shows both authors and dates.
948 */
949 if (!memcmp(line, "author ", 7)) {
958 - unsigned long len = linelen;
959 - if (fmt == CMIT_FMT_EMAIL)
960 - len = bound_rfc2047(linelen, encoding);
961 - strbuf_grow(sb, len + 80);
950 + strbuf_grow(sb, linelen + 80);
951 add_user_info("Author", fmt, sb, line + 7, dmode, encoding);
952 }
964 -
953 if (!memcmp(line, "committer ", 10) &&
954 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
967 - unsigned long len = linelen;
968 - if (fmt == CMIT_FMT_EMAIL)
969 - len = bound_rfc2047(linelen, encoding);
970 - strbuf_grow(sb, len + 80);
955 + strbuf_grow(sb, linelen + 80);
956 add_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
957 }
958 }
@@ -982,7 +967,6 @@ static void pp_title_line(enum cmit_fmt fmt,
967 int plain_non_ascii)
968 {
969 struct strbuf title;
985 - unsigned long len;
970
971 strbuf_init(&title, 80);
972
@@ -1004,16 +988,7 @@ static void pp_title_line(enum cmit_fmt fmt,
988 strbuf_add(&title, line, linelen);
989 }
990
1007 - /* Enough slop for the MIME header and rfc2047 */
1008 - len = bound_rfc2047(title.len, encoding) + 1000;
1009 - if (subject)
1010 - len += strlen(subject);
1011 - if (after_subject)
1012 - len += strlen(after_subject);
1013 - if (encoding)
1014 - len += strlen(encoding);
1015 -
1016 - strbuf_grow(sb, title.len + len);
991 + strbuf_grow(sb, title.len + 1024);
992 if (subject) {
993 strbuf_addstr(sb, subject);
994 add_rfc2047(sb, title.buf, title.len, encoding);