From 2c2dc7c82cea66c5030583233c73593e0f5a8fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Mon, 19 Jan 2009 23:30:30 -0500 Subject: Optimize color_parse_mem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano diff --git a/color.c b/color.c index 54a3da1..915d7a9 100644 --- a/color.c +++ b/color.c @@ -41,29 +41,40 @@ 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; @@ -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; @@ -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) @@ -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); -} -- cgit v0.10.2-6-g49f6