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
Junio C Hamano <gitster@pobox.com>
Date
Dec 10, 2014, 00:18 UTC
Message-ID
<xmqqsigojr0u.fsf@gitster.dls.corp.google.com>
In-Reply-To
<1418133205-18213-5-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 12 quoted lines
> +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?

Show 32 quoted lines
> +	} else {
> +		rem = num;
> +		for (i = 0; i < num; i++) {
> +			struct git_attr_check *c;
> +			c = check_all_attr + check[i].attr->attr_nr;
> +			if (check[i].attr->maybe_real)
> +				c->value = ATTR__UNKNOWN;
> +			else {
> +				c->value = ATTR__UNSET;
> +				rem--;
> +			}
> +		}
> +		if (!rem)
> +			return;
> +	}
> +	rem = git_attr_nr;
> +	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
> +		rem = fill(path, pathlen, basename_offset, stk, rem);
> +}
> +
>  int git_check_attr(const char *path, int num, struct git_attr_check *check)
>  {
>  	int i;
>  
> -	collect_all_attrs(path);
> +	if (cannot_trust_maybe_real)
> +		collect_all_attrs(path);
> +	else
> +		collect_selected_attrs(path, num, check);
>  
>  	for (i = 0; i < num; i++) {
>  		const char *value = check_all_attr[check[i].attr->attr_nr].value;
Previous: Nguyễn Thái Ngọc DuyNext: Duy Nguyen
Message 9 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.