Skip to content

Commit 2c2dc7c

Browse files
René Scharfegitster
authored andcommitted
Optimize color_parse_mem
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c002922 commit 2c2dc7c

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

color.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,41 @@ static int parse_attr(const char *name, int len)
4040
}
4141

4242
void color_parse(const char *value, const char *var, char *dst)
43+
{
44+
color_parse_mem(value, strlen(value), var, dst);
45+
}
46+
47+
void color_parse_mem(const char *value, int value_len, const char *var,
48+
char *dst)
4349
{
4450
const char *ptr = value;
51+
int len = value_len;
4552
int attr = -1;
4653
int fg = -2;
4754
int bg = -2;
4855

49-
if (!strcasecmp(value, "reset")) {
56+
if (!strncasecmp(value, "reset", len)) {
5057
strcpy(dst, "\033[m");
5158
return;
5259
}
5360

5461
/* [fg [bg]] [attr] */
55-
while (*ptr) {
62+
while (len > 0) {
5663
const char *word = ptr;
57-
int val, len = 0;
64+
int val, wordlen = 0;
5865

59-
while (word[len] && !isspace(word[len]))
60-
len++;
66+
while (len > 0 && !isspace(word[wordlen])) {
67+
wordlen++;
68+
len--;
69+
}
6170

62-
ptr = word + len;
63-
while (*ptr && isspace(*ptr))
71+
ptr = word + wordlen;
72+
while (len > 0 && isspace(*ptr)) {
6473
ptr++;
74+
len--;
75+
}
6576

66-
val = parse_color(word, len);
77+
val = parse_color(word, wordlen);
6778
if (val >= -1) {
6879
if (fg == -2) {
6980
fg = val;
@@ -75,7 +86,7 @@ void color_parse(const char *value, const char *var, char *dst)
7586
}
7687
goto bad;
7788
}
78-
val = parse_attr(word, len);
89+
val = parse_attr(word, wordlen);
7990
if (val < 0 || attr != -1)
8091
goto bad;
8192
attr = val;
@@ -115,7 +126,7 @@ void color_parse(const char *value, const char *var, char *dst)
115126
*dst = 0;
116127
return;
117128
bad:
118-
die("bad color value '%s' for variable '%s'", value, var);
129+
die("bad color value '%.*s' for variable '%s'", value_len, value, var);
119130
}
120131

121132
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, ...)
191202
va_end(args);
192203
return r;
193204
}
194-
195-
void color_parse_mem(const char *value, int len, const char *var, char *dst)
196-
{
197-
char *tmp = xmemdupz(value, len);
198-
color_parse(tmp, var, dst);
199-
free(tmp);
200-
}

0 commit comments

Comments
 (0)