Optimize color_parse_mem
Commit 5ef8d77a
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>
This commit is contained in:
parent
c002922adc
commit
2c2dc7c82c
38
color.c
38
color.c
@ -40,30 +40,41 @@ static int parse_attr(const char *name, int len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void color_parse(const char *value, const char *var, char *dst)
|
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;
|
const char *ptr = value;
|
||||||
|
int len = value_len;
|
||||||
int attr = -1;
|
int attr = -1;
|
||||||
int fg = -2;
|
int fg = -2;
|
||||||
int bg = -2;
|
int bg = -2;
|
||||||
|
|
||||||
if (!strcasecmp(value, "reset")) {
|
if (!strncasecmp(value, "reset", len)) {
|
||||||
strcpy(dst, "\033[m");
|
strcpy(dst, "\033[m");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* [fg [bg]] [attr] */
|
/* [fg [bg]] [attr] */
|
||||||
while (*ptr) {
|
while (len > 0) {
|
||||||
const char *word = ptr;
|
const char *word = ptr;
|
||||||
int val, len = 0;
|
int val, wordlen = 0;
|
||||||
|
|
||||||
while (word[len] && !isspace(word[len]))
|
while (len > 0 && !isspace(word[wordlen])) {
|
||||||
len++;
|
wordlen++;
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
|
||||||
ptr = word + len;
|
ptr = word + wordlen;
|
||||||
while (*ptr && isspace(*ptr))
|
while (len > 0 && isspace(*ptr)) {
|
||||||
ptr++;
|
ptr++;
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
|
||||||
val = parse_color(word, len);
|
val = parse_color(word, wordlen);
|
||||||
if (val >= -1) {
|
if (val >= -1) {
|
||||||
if (fg == -2) {
|
if (fg == -2) {
|
||||||
fg = val;
|
fg = val;
|
||||||
@ -75,7 +86,7 @@ void color_parse(const char *value, const char *var, char *dst)
|
|||||||
}
|
}
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
}
|
||||||
val = parse_attr(word, len);
|
val = parse_attr(word, wordlen);
|
||||||
if (val < 0 || attr != -1)
|
if (val < 0 || attr != -1)
|
||||||
goto bad;
|
goto bad;
|
||||||
attr = val;
|
attr = val;
|
||||||
@ -115,7 +126,7 @@ void color_parse(const char *value, const char *var, char *dst)
|
|||||||
*dst = 0;
|
*dst = 0;
|
||||||
return;
|
return;
|
||||||
bad:
|
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)
|
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
|
||||||
@ -191,10 +202,3 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)
|
|||||||
va_end(args);
|
va_end(args);
|
||||||
return r;
|
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);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user