Skip to content

Commit 9db9eec

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 711536b commit 9db9eec

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

attr.c

+18-27
Original file line numberDiff line numberDiff line change
@@ -564,32 +564,12 @@ static void bootstrap_attr_stack(void)
564564
attr_stack = elem;
565565
}
566566

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)
579568
{
580569
struct attr_stack *elem, *info;
581-
int dirlen, len;
570+
int len;
582571
const char *cp;
583572

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-
593573
/*
594574
* At the bottom of the attribute stack is the built-in
595575
* set of attribute definitions, followed by the contents
@@ -769,15 +749,26 @@ static int macroexpand_one(int attr_nr, int rem)
769749
static void collect_all_attrs(const char *path)
770750
{
771751
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+
}
774767

775-
prepare_attr_stack(path);
768+
prepare_attr_stack(path, dirlen);
776769
for (i = 0; i < attr_nr; i++)
777770
check_all_attr[i].value = ATTR__UNKNOWN;
778771

779-
basename = find_basename(path);
780-
pathlen = strlen(path);
781772
rem = attr_nr;
782773
for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
783774
rem = fill(path, pathlen, basename, stk, rem);

0 commit comments

Comments
 (0)