diff options
author | Duy Nguyen <pclouds@gmail.com> | 2013-01-16 13:02:38 +0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2013-01-16 11:08:55 -0800 |
commit | 9db9eecfe5c2490d17c0d4bd5452e4cb1d0948c5 (patch) | |
tree | f6dd00f2d7a013f2ae3474a635611b12388f8678 /attr.c | |
parent | 711536bd4ba791adfd506583927a8f6c8f821e24 (diff) | |
download | git-9db9eecfe5c2490d17c0d4bd5452e4cb1d0948c5.tar.gz |
attr: avoid calling find_basename() twice per path
find_basename() is only used inside collect_all_attrs(), called once
in prepare_attr_stack, then again after prepare_attr_stack()
returns. Both calls return exact same value. Reorder the code to do
the same task once. Also avoid strlen() because we knows the length
after finding basename.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'attr.c')
-rw-r--r-- | attr.c | 45 |
1 files changed, 18 insertions, 27 deletions
@@ -564,32 +564,12 @@ static void bootstrap_attr_stack(void) attr_stack = elem; } -static const char *find_basename(const char *path) -{ - const char *cp, *last_slash = NULL; - - for (cp = path; *cp; cp++) { - if (*cp == '/' && cp[1]) - last_slash = cp; - } - return last_slash ? last_slash + 1 : path; -} - -static void prepare_attr_stack(const char *path) +static void prepare_attr_stack(const char *path, int dirlen) { struct attr_stack *elem, *info; - int dirlen, len; + int len; const char *cp; - dirlen = find_basename(path) - path; - - /* - * find_basename() includes the trailing slash, but we do - * _not_ want it. - */ - if (dirlen) - dirlen--; - /* * At the bottom of the attribute stack is the built-in * set of attribute definitions, followed by the contents @@ -769,15 +749,26 @@ static int macroexpand_one(int attr_nr, int rem) static void collect_all_attrs(const char *path) { struct attr_stack *stk; - int i, pathlen, rem; - const char *basename; + int i, pathlen, rem, dirlen; + const char *basename, *cp, *last_slash = NULL; + + for (cp = path; *cp; cp++) { + if (*cp == '/' && cp[1]) + last_slash = cp; + } + pathlen = cp - path; + if (last_slash) { + basename = last_slash + 1; + dirlen = last_slash - path; + } else { + basename = path; + dirlen = 0; + } - prepare_attr_stack(path); + prepare_attr_stack(path, dirlen); for (i = 0; i < attr_nr; i++) check_all_attr[i].value = ATTR__UNKNOWN; - basename = find_basename(path); - pathlen = strlen(path); rem = attr_nr; for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) rem = fill(path, pathlen, basename, stk, rem); |