Skip to content

Commit 8b6087f

Browse files
MadCodergitster
authored andcommitted
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>
1 parent a08f23a commit 8b6087f

File tree

1 file changed

+5
-30
lines changed

1 file changed

+5
-30
lines changed

commit.c

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
501501
return;
502502

503503
needquote:
504+
strbuf_grow(sb, len * 3 + strlen(encoding) + 100);
504505
strbuf_addf(sb, "=?%s?q?", encoding);
505506
for (i = last = 0; i < len; i++) {
506507
unsigned ch = line[i] & 0xFF;
@@ -520,14 +521,6 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
520521
strbuf_addstr(sb, "?=");
521522
}
522523

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-
531524
static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
532525
const char *line, enum date_mode dmode,
533526
const char *encoding)
@@ -560,8 +553,7 @@ static void add_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb
560553
add_rfc2047(sb, line, display_name_length, encoding);
561554
strbuf_add(sb, name_tail, namelen - display_name_length);
562555
strbuf_addch(sb, '\n');
563-
}
564-
else {
556+
} else {
565557
strbuf_addf(sb, "%s: %.*s%.*s\n", what,
566558
(fmt == CMIT_FMT_FULLER) ? 4 : 0,
567559
filler, namelen, line);
@@ -955,19 +947,12 @@ static void pp_header(enum cmit_fmt fmt,
955947
* FULLER shows both authors and dates.
956948
*/
957949
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);
962951
add_user_info("Author", fmt, sb, line + 7, dmode, encoding);
963952
}
964-
965953
if (!memcmp(line, "committer ", 10) &&
966954
(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);
971956
add_user_info("Commit", fmt, sb, line + 10, dmode, encoding);
972957
}
973958
}
@@ -982,7 +967,6 @@ static void pp_title_line(enum cmit_fmt fmt,
982967
int plain_non_ascii)
983968
{
984969
struct strbuf title;
985-
unsigned long len;
986970

987971
strbuf_init(&title, 80);
988972

@@ -1004,16 +988,7 @@ static void pp_title_line(enum cmit_fmt fmt,
1004988
strbuf_add(&title, line, linelen);
1005989
}
1006990

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);
1017992
if (subject) {
1018993
strbuf_addstr(sb, subject);
1019994
add_rfc2047(sb, title.buf, title.len, encoding);

0 commit comments

Comments
 (0)