@matheusbsbs / matheus-bagliano / commits / 2c2dc7c82c
Fork 0

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>

René Scharfe committed Jan 19, 2009 at 23:30 UTC 2c2dc7c82cea66c5030583233c73593e0f5a8fdb
1 file changed +21 -17
color.c
+21 -17
@@ -40,30 +40,41 @@ static int parse_attr(const char *name, int len)
40 }
41
42 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)
49 {
50 const char *ptr = value;
51 + int len = value_len;
52 int attr = -1;
53 int fg = -2;
54 int bg = -2;
55
49 - if (!strcasecmp(value, "reset")) {
56 + if (!strncasecmp(value, "reset", len)) {
57 strcpy(dst, "\033[m");
58 return;
59 }
60
61 /* [fg [bg]] [attr] */
55 - while (*ptr) {
62 + while (len > 0) {
63 const char *word = ptr;
57 - int val, len = 0;
64 + int val, wordlen = 0;
65
59 - while (word[len] && !isspace(word[len]))
60 - len++;
66 + while (len > 0 && !isspace(word[wordlen])) {
67 + wordlen++;
68 + len--;
69 + }
70
62 - ptr = word + len;
63 - while (*ptr && isspace(*ptr))
71 + ptr = word + wordlen;
72 + while (len > 0 && isspace(*ptr)) {
73 ptr++;
74 + len--;
75 + }
76
66 - val = parse_color(word, len);
77 + val = parse_color(word, wordlen);
78 if (val >= -1) {
79 if (fg == -2) {
80 fg = val;
@@ -75,7 +86,7 @@ void color_parse(const char *value, const char *var, char *dst)
86 }
87 goto bad;
88 }
78 - val = parse_attr(word, len);
89 + val = parse_attr(word, wordlen);
90 if (val < 0 || attr != -1)
91 goto bad;
92 attr = val;
@@ -115,7 +126,7 @@ void color_parse(const char *value, const char *var, char *dst)
126 *dst = 0;
127 return;
128 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);
130 }
131
132 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, ...)
202 va_end(args);
203 return r;
204 }
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 -}