Skip to content

Commit fad32bc

Browse files
pcloudsgitster
authored andcommitted
attr: do not attempt to expand when we know it's not a macro
Keep track of all recognized macros in the new "maybe_macro" field. If this field is true, it _may_ be a macro (depending on what's in the current attr stack). But if the field is false, it's definitely not a macro, no need to go through the whole attr stack in macroexpand_one() to search for one. Without this, "git grep abcdefghi" on git.git hits the inner loop in macroexpand_one() 2481 times. With this, it's 66 times. Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aa7710e commit fad32bc

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

attr.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct git_attr {
3232
struct git_attr *next;
3333
unsigned h;
3434
int attr_nr;
35+
int maybe_macro;
3536
char name[FLEX_ARRAY];
3637
};
3738
static int attr_nr;
@@ -95,6 +96,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
9596
a->h = hval;
9697
a->next = git_attr_hash[pos];
9798
a->attr_nr = attr_nr++;
99+
a->maybe_macro = 0;
98100
git_attr_hash[pos] = a;
99101

100102
REALLOC_ARRAY(check_all_attr, attr_nr);
@@ -244,9 +246,10 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
244246
sizeof(*res) +
245247
sizeof(struct attr_state) * num_attr +
246248
(is_macro ? 0 : namelen + 1));
247-
if (is_macro)
249+
if (is_macro) {
248250
res->u.attr = git_attr_internal(name, namelen);
249-
else {
251+
res->u.attr->maybe_macro = 1;
252+
} else {
250253
char *p = (char *)&(res->state[num_attr]);
251254
memcpy(p, name, namelen);
252255
res->u.pat.pattern = p;
@@ -687,7 +690,8 @@ static int macroexpand_one(int nr, int rem)
687690
struct match_attr *a = NULL;
688691
int i;
689692

690-
if (check_all_attr[nr].value != ATTR__TRUE)
693+
if (check_all_attr[nr].value != ATTR__TRUE ||
694+
!check_all_attr[nr].attr->maybe_macro)
691695
return rem;
692696

693697
for (stk = attr_stack; !a && stk; stk = stk->prev)

0 commit comments

Comments
 (0)