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>

Duy Nguyen committed Jan 16, 2013 at 13:02 UTC 9db9eecfe5c2490d17c0d4bd5452e4cb1d0948c5
1 file changed +18 -27
attr.c
+18 -27
@@ -564,32 +564,12 @@ static void bootstrap_attr_stack(void)
564 attr_stack = elem;
565 }
566
567 -static const char *find_basename(const char *path)
568 -{
569 - const char *cp, *last_slash = NULL;
570 -
571 - for (cp = path; *cp; cp++) {
572 - if (*cp == '/' && cp[1])
573 - last_slash = cp;
574 - }
575 - return last_slash ? last_slash + 1 : path;
576 -}
577 -
578 -static void prepare_attr_stack(const char *path)
567 +static void prepare_attr_stack(const char *path, int dirlen)
568 {
569 struct attr_stack *elem, *info;
581 - int dirlen, len;
570 + int len;
571 const char *cp;
572
584 - dirlen = find_basename(path) - path;
585 -
586 - /*
587 - * find_basename() includes the trailing slash, but we do
588 - * _not_ want it.
589 - */
590 - if (dirlen)
591 - dirlen--;
592 -
573 /*
574 * At the bottom of the attribute stack is the built-in
575 * set of attribute definitions, followed by the contents
@@ -769,15 +749,26 @@ static int macroexpand_one(int attr_nr, int rem)
749 static void collect_all_attrs(const char *path)
750 {
751 struct attr_stack *stk;
772 - int i, pathlen, rem;
773 - const char *basename;
752 + int i, pathlen, rem, dirlen;
753 + const char *basename, *cp, *last_slash = NULL;
754 +
755 + for (cp = path; *cp; cp++) {
756 + if (*cp == '/' && cp[1])
757 + last_slash = cp;
758 + }
759 + pathlen = cp - path;
760 + if (last_slash) {
761 + basename = last_slash + 1;
762 + dirlen = last_slash - path;
763 + } else {
764 + basename = path;
765 + dirlen = 0;
766 + }
767
775 - prepare_attr_stack(path);
768 + prepare_attr_stack(path, dirlen);
769 for (i = 0; i < attr_nr; i++)
770 check_all_attr[i].value = ATTR__UNKNOWN;
771
779 - basename = find_basename(path);
780 - pathlen = strlen(path);
772 rem = attr_nr;
773 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
774 rem = fill(path, pathlen, basename, stk, rem);