Skip to content

Commit

Permalink
Optimize color_parse_mem
Browse files Browse the repository at this point in the history
Commit 5ef8d77 implemented color_parse_mem, a function for
parsing colors from a non-NUL-terminated string, by simply
allocating a new NUL-terminated string and calling
color_parse. This had a small but measurable speed impact on
a user format that used the advanced color parsing. E.g.,

  # uses quick parsing
  $ time ./git log --pretty=tformat:'%Credfoo%Creset' >/dev/null
  real    0m0.673s
  user    0m0.652s
  sys     0m0.016s

  # uses color_parse_mem
  $ time ./git log --pretty=tformat:'%C(red)foo%C(reset)' >/dev/null
  real    0m0.692s
  user    0m0.660s
  sys     0m0.032s

This patch implements color_parse_mem as the primary
function, with color_parse as a wrapper for strings. This
gives comparable timings to the first case above.

Original patch by René. Commit message and debugging by Jeff
King.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
René Scharfe authored and gitster committed Jan 20, 2009
1 parent c002922 commit 2c2dc7c
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions color.c
Expand Up @@ -40,30 +40,41 @@ static int parse_attr(const char *name, int len)
}

void color_parse(const char *value, const char *var, char *dst)
{
color_parse_mem(value, strlen(value), var, dst);
}

void color_parse_mem(const char *value, int value_len, const char *var,
char *dst)
{
const char *ptr = value;
int len = value_len;
int attr = -1;
int fg = -2;
int bg = -2;

if (!strcasecmp(value, "reset")) {
if (!strncasecmp(value, "reset", len)) {
strcpy(dst, "\033[m");
return;
}

/* [fg [bg]] [attr] */
while (*ptr) {
while (len > 0) {
const char *word = ptr;
int val, len = 0;
int val, wordlen = 0;

while (word[len] && !isspace(word[len]))
len++;
while (len > 0 && !isspace(word[wordlen])) {
wordlen++;
len--;
}

ptr = word + len;
while (*ptr && isspace(*ptr))
ptr = word + wordlen;
while (len > 0 && isspace(*ptr)) {
ptr++;
len--;
}

val = parse_color(word, len);
val = parse_color(word, wordlen);
if (val >= -1) {
if (fg == -2) {
fg = val;
Expand All @@ -75,7 +86,7 @@ void color_parse(const char *value, const char *var, char *dst)
}
goto bad;
}
val = parse_attr(word, len);
val = parse_attr(word, wordlen);
if (val < 0 || attr != -1)
goto bad;
attr = val;
Expand Down Expand Up @@ -115,7 +126,7 @@ void color_parse(const char *value, const char *var, char *dst)
*dst = 0;
return;
bad:
die("bad color value '%s' for variable '%s'", value, var);
die("bad color value '%.*s' for variable '%s'", value_len, value, var);
}

int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
Expand Down Expand Up @@ -191,10 +202,3 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
va_end(args);
return r;
}

void color_parse_mem(const char *value, int len, const char *var, char *dst)
{
char *tmp = xmemdupz(value, len);
color_parse(tmp, var, dst);
free(tmp);
}

0 comments on commit 2c2dc7c

Please sign in to comment.