git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH/RFC 4/4] attr: avoid heavy work when we know the specified attr is not defined

From
Duy Nguyen <pclouds@gmail.com>
Date
Dec 15, 2014, 00:50 UTC
Message-ID
<20141215005018.GA31006@lanh>
In-Reply-To
<xmqqsigojr0u.fsf@gitster.dls.corp.google.com>
On Tue, Dec 09, 2014 at 04:18:57PM -0800, Junio C Hamano wrote:
Show 34 quoted lines
> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
> 
> > +static void collect_selected_attrs(const char *path, int num,
> > +				   struct git_attr_check *check)
> > +{
> > +	struct attr_stack *stk;
> > +	int i, pathlen, rem, dirlen;
> > +	int basename_offset;
> > +
> > +	pathlen = split_path(path, &dirlen, &basename_offset);
> > +	prepare_attr_stack(path, dirlen);
> > +	if (cannot_trust_maybe_real) {
> > +		for (i = 0; i < git_attr_nr; i++)
> > +			check_all_attr[i].value = ATTR__UNKNOWN;
> 
> Judging from the fact that
> 
>  (1) the only caller calls this function in this fashion based on the
>      setting of "cannot-trust" bit,
> 
>  (2) this and the other function the only caller calls share the
>      same code in their beginning part, and
> 
>  (3) the body of the if() statement here duplicates the code from
>      collect_all_attrs(),
> 
> I smell that a much better split is possible.
> 
> Why isn't this all inside a single function collect_all_attrs()?
> That single function may no longer be collect_ALL_attrs, so renaming
> it to collect_attrs() is fine, but then that function may have this
> if () to initialize all of them to ATTR__UNKNOWN or do the else part
> we see below, and when organized that way we do not need to have
> duplicated code (or split_path() helper function), no?
Something like this? Definitely looks better.
-- 8< --
diff --git a/attr.c b/attr.c
index b80e52b..0f828e3 100644
--- a/attr.c
+++ b/attr.c
@@ -33,9 +33,11 @@ struct git_attr {
 	unsigned h;
 	int attr_nr;
 	int maybe_macro;
+	int maybe_real;
 	char name[FLEX_ARRAY];
 };
 static int attr_nr;
+static int cannot_trust_maybe_real;
 
 static struct git_attr_check *check_all_attr;
 static struct git_attr *(git_attr_hash[HASHSIZE]);
@@ -97,6 +99,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
 	a->next = git_attr_hash[pos];
 	a->attr_nr = attr_nr++;
 	a->maybe_macro = 0;
+	a->maybe_real = 0;
 	git_attr_hash[pos] = a;
 
 	REALLOC_ARRAY(check_all_attr, attr_nr);
@@ -269,6 +272,10 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 	/* Second pass to fill the attr_states */
 	for (cp = states, i = 0; *cp; 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;
@@ -713,7 +720,9 @@ static int macroexpand_one(int nr, int rem)
  * Collect all attributes for path into the array pointed to by
  * check_all_attr.
  */
-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;
 	int i, pathlen, rem, dirlen;
@@ -736,6 +745,19 @@ static void collect_all_attrs(const char *path)
 	prepare_attr_stack(path, dirlen);
 	for (i = 0; i < attr_nr; i++)
 		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;
 	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
@@ -746,7 +768,7 @@ int git_check_attr(const char *path, int num, struct git_attr_check *check)
 {
 	int i;
 
-	collect_all_attrs(path);
+	collect_some_attrs(path, num, check);
 
 	for (i = 0; i < num; i++) {
 		const char *value = check_all_attr[check[i].attr->attr_nr].value;
@@ -762,7 +784,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
 {
 	int i, count, j;
 
-	collect_all_attrs(path);
+	collect_some_attrs(path, 0, NULL);
 
 	/* Count the number of attributes that are set. */
 	count = 0;
-- 8< --
Previous: Junio C HamanoNext: Junio C Hamano
Message 10 of 16 in “some attr optimizations”
  1. 0/4 some attr optimizationsNguyễn Thái Ngọc Duy, Dec 9, 2014
  2. 1/4 attr.c: rename global var attr_nr to git_attr_nrNguyễn Thái Ngọc Duy, Dec 9, 2014
  3. Junio C HamanoDec 9, 2014
  4. 2/4 attr.c: split path processing code out of collect_all_attrs()Nguyễn Thái Ngọc Duy, Dec 9, 2014
  5. 3/4 attr: do not attempt to expand when we know it's not a macroNguyễn Thái Ngọc Duy, Dec 9, 2014
  6. Eric SunshineDec 9, 2014
  7. Junio C HamanoDec 9, 2014
  8. 4/4 attr: avoid heavy work when we know the specified attr is not definedNguyễn Thái Ngọc Duy, Dec 9, 2014
  9. Junio C HamanoDec 10, 2014
  10. Duy NguyenDec 15, 2014
  11. Junio C HamanoDec 15, 2014
  12. 0/3 some attr optimizationsNguyễn Thái Ngọc Duy, Dec 27, 2014
  13. 1/3 attr.c: rename arg name attr_nr to avoid shadowing the global oneNguyễn Thái Ngọc Duy, Dec 27, 2014
  14. 2/3 attr: do not attempt to expand when we know it's not a macroNguyễn Thái Ngọc Duy, Dec 27, 2014
  15. Eric SunshineDec 27, 2014
  16. 3/3 attr: avoid heavy work when we know the specified attr is not definedNguyễn Thái Ngọc Duy, Dec 27, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.