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 <sunshine@sunshineco.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Dec 28, 2014 at 06:39 UTC
fad32bcd83ec4e4f88013e4a0b05f42e32e2c6f5
1 file changed
+7
-3
attr.c
+7
-3
@@ -32,6 +32,7 @@ struct git_attr {
32
struct git_attr *next;
33
unsigned h;
34
int attr_nr;
35
+ int maybe_macro;
36
char name[FLEX_ARRAY];
37
};
38
static int attr_nr;
@@ -95,6 +96,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
96
a->h = hval;
97
a->next = git_attr_hash[pos];
98
a->attr_nr = attr_nr++;
99
+ a->maybe_macro = 0;
100
git_attr_hash[pos] = a;
101
102
REALLOC_ARRAY(check_all_attr, attr_nr);
@@ -244,9 +246,10 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
246
sizeof(*res) +
247
sizeof(struct attr_state) * num_attr +
248
(is_macro ? 0 : namelen + 1));
247
- if (is_macro)
249
+ if (is_macro) {
250
res->u.attr = git_attr_internal(name, namelen);
249
- else {
251
+ res->u.attr->maybe_macro = 1;
252
+ } else {
253
char *p = (char *)&(res->state[num_attr]);
254
memcpy(p, name, namelen);
255
res->u.pat.pattern = p;
@@ -687,7 +690,8 @@ static int macroexpand_one(int nr, int rem)
690
struct match_attr *a = NULL;
691
int i;
692
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)
695
return rem;
696
697
for (stk = attr_stack; !a && stk; stk = stk->prev)