Browse Source

Merge branch 'nd/attr-optim'

Optimize attribute look-up, mostly useful in "git grep" on a
project that does not use many attributes, by avoiding it when we
(should) know that the attributes are not defined in the first
place.

* nd/attr-optim:
  attr: avoid heavy work when we know the specified attr is not defined
  attr: do not attempt to expand when we know it's not a macro
  attr.c: rename arg name attr_nr to avoid shadowing the global one
maint
Junio C Hamano 10 years ago
parent
commit
50e1ba5050
  1. 47
      attr.c

47
attr.c

@ -32,9 +32,12 @@ struct git_attr {
struct git_attr *next; struct git_attr *next;
unsigned h; unsigned h;
int attr_nr; int attr_nr;
int maybe_macro;
int maybe_real;
char name[FLEX_ARRAY]; char name[FLEX_ARRAY];
}; };
static int attr_nr; static int attr_nr;
static int cannot_trust_maybe_real;


static struct git_attr_check *check_all_attr; static struct git_attr_check *check_all_attr;
static struct git_attr *(git_attr_hash[HASHSIZE]); static struct git_attr *(git_attr_hash[HASHSIZE]);
@ -95,6 +98,8 @@ static struct git_attr *git_attr_internal(const char *name, int len)
a->h = hval; a->h = hval;
a->next = git_attr_hash[pos]; a->next = git_attr_hash[pos];
a->attr_nr = attr_nr++; a->attr_nr = attr_nr++;
a->maybe_macro = 0;
a->maybe_real = 0;
git_attr_hash[pos] = a; git_attr_hash[pos] = a;


REALLOC_ARRAY(check_all_attr, attr_nr); REALLOC_ARRAY(check_all_attr, attr_nr);
@ -244,9 +249,10 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
sizeof(*res) + sizeof(*res) +
sizeof(struct attr_state) * num_attr + sizeof(struct attr_state) * num_attr +
(is_macro ? 0 : namelen + 1)); (is_macro ? 0 : namelen + 1));
if (is_macro) if (is_macro) {
res->u.attr = git_attr_internal(name, namelen); res->u.attr = git_attr_internal(name, namelen);
else { res->u.attr->maybe_macro = 1;
} else {
char *p = (char *)&(res->state[num_attr]); char *p = (char *)&(res->state[num_attr]);
memcpy(p, name, namelen); memcpy(p, name, namelen);
res->u.pat.pattern = p; res->u.pat.pattern = p;
@ -266,6 +272,10 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
/* Second pass to fill the attr_states */ /* Second pass to fill the attr_states */
for (cp = states, i = 0; *cp; i++) { for (cp = states, i = 0; *cp; i++) {
cp = parse_attr(src, lineno, cp, &(res->state[i])); cp = parse_attr(src, lineno, cp, &(res->state[i]));
if (!is_macro)
res->state[i].attr->maybe_real = 1;
if (res->state[i].attr->maybe_macro)
cannot_trust_maybe_real = 1;
} }


return res; return res;
@ -681,13 +691,14 @@ static int fill(const char *path, int pathlen, int basename_offset,
return rem; return rem;
} }


static int macroexpand_one(int attr_nr, int rem) static int macroexpand_one(int nr, int rem)
{ {
struct attr_stack *stk; struct attr_stack *stk;
struct match_attr *a = NULL; struct match_attr *a = NULL;
int i; int i;


if (check_all_attr[attr_nr].value != ATTR__TRUE) if (check_all_attr[nr].value != ATTR__TRUE ||
!check_all_attr[nr].attr->maybe_macro)
return rem; return rem;


for (stk = attr_stack; !a && stk; stk = stk->prev) for (stk = attr_stack; !a && stk; stk = stk->prev)
@ -695,7 +706,7 @@ static int macroexpand_one(int attr_nr, int rem)
struct match_attr *ma = stk->attrs[i]; struct match_attr *ma = stk->attrs[i];
if (!ma->is_macro) if (!ma->is_macro)
continue; continue;
if (ma->u.attr->attr_nr == attr_nr) if (ma->u.attr->attr_nr == nr)
a = ma; a = ma;
} }


@ -706,10 +717,13 @@ static int macroexpand_one(int attr_nr, int rem)
} }


/* /*
* Collect all attributes for path into the array pointed to by * Collect attributes for path into the array pointed to by
* check_all_attr. * check_all_attr. If num is non-zero, only attributes in check[] are
* collected. Otherwise all attributes are collected.
*/ */
static void collect_all_attrs(const char *path) static void collect_some_attrs(const char *path, int num,
struct git_attr_check *check)

{ {
struct attr_stack *stk; struct attr_stack *stk;
int i, pathlen, rem, dirlen; int i, pathlen, rem, dirlen;
@ -732,6 +746,19 @@ static void collect_all_attrs(const char *path)
prepare_attr_stack(path, dirlen); prepare_attr_stack(path, dirlen);
for (i = 0; i < attr_nr; i++) for (i = 0; i < attr_nr; i++)
check_all_attr[i].value = ATTR__UNKNOWN; check_all_attr[i].value = ATTR__UNKNOWN;
if (num && !cannot_trust_maybe_real) {
rem = 0;
for (i = 0; i < num; i++) {
if (!check[i].attr->maybe_real) {
struct git_attr_check *c;
c = check_all_attr + check[i].attr->attr_nr;
c->value = ATTR__UNSET;
rem++;
}
}
if (rem == num)
return;
}


rem = attr_nr; rem = attr_nr;
for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
@ -742,7 +769,7 @@ int git_check_attr(const char *path, int num, struct git_attr_check *check)
{ {
int i; int i;


collect_all_attrs(path); collect_some_attrs(path, num, check);


for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
const char *value = check_all_attr[check[i].attr->attr_nr].value; const char *value = check_all_attr[check[i].attr->attr_nr].value;
@ -758,7 +785,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
{ {
int i, count, j; int i, count, j;


collect_all_attrs(path); collect_some_attrs(path, 0, NULL);


/* Count the number of attributes that are set. */ /* Count the number of attributes that are set. */
count = 0; count = 0;

Loading…
Cancel
Save