git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Duy Nguyen <pclouds@gmail.com>
Cc: "Jean-Noël AVILA" <avila.jn@gmail.com>, git@vger.kernel.org
Subject: Re: [PATCH] attr: fix off-by-one directory component length calculation
Date: Wed, 16 Jan 2013 11:07:44 -0800	[thread overview]
Message-ID: <7vzk09vtdr.fsf@alter.siamese.dyndns.org> (raw)
In-Reply-To: <20130116060238.GA29523@duynguyen-vnpc.dek-tpc.internal> (Duy Nguyen's message of "Wed, 16 Jan 2013 13:02:38 +0700")

Duy Nguyen <pclouds@gmail.com> writes:

> OK I get your point now. Something like this?
>
> -- 8< --
> Subject: [PATCH] attr: avoid calling find_basename() twice per path
>
> find_basename() is only used inside collect_all_attrs(), called once
> in prepare_attr_stack, then again after prepare_attr_stack()
> returns. Both calls return exact same value. Reorder the code to do
> the same task once. Also avoid strlen() because we knows the length
> after finding basename.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>

Yeah, I think this is a nice code reduction, readability improvement
and micro optimization rolled into one.

>  attr.c | 45 ++++++++++++++++++---------------------------
>  1 file changed, 18 insertions(+), 27 deletions(-)
>
> diff --git a/attr.c b/attr.c
> index cfc6748..880f862 100644
> --- a/attr.c
> +++ b/attr.c
> @@ -564,32 +564,12 @@ static void bootstrap_attr_stack(void)
>  	attr_stack = elem;
>  }
>  
> -static const char *find_basename(const char *path)
> -{
> -	const char *cp, *last_slash = NULL;
> -
> -	for (cp = path; *cp; cp++) {
> -		if (*cp == '/' && cp[1])
> -			last_slash = cp;
> -	}
> -	return last_slash ? last_slash + 1 : path;
> -}
> -
> -static void prepare_attr_stack(const char *path)
> +static void prepare_attr_stack(const char *path, int dirlen)
>  {
>  	struct attr_stack *elem, *info;
> -	int dirlen, len;
> +	int len;
>  	const char *cp;
>  
> -	dirlen = find_basename(path) - path;
> -
> -	/*
> -	 * find_basename() includes the trailing slash, but we do
> -	 * _not_ want it.
> -	 */
> -	if (dirlen)
> -		dirlen--;
> -
>  	/*
>  	 * At the bottom of the attribute stack is the built-in
>  	 * set of attribute definitions, followed by the contents
> @@ -769,15 +749,26 @@ static int macroexpand_one(int attr_nr, int rem)
>  static void collect_all_attrs(const char *path)
>  {
>  	struct attr_stack *stk;
> -	int i, pathlen, rem;
> -	const char *basename;
> +	int i, pathlen, rem, dirlen;
> +	const char *basename, *cp, *last_slash = NULL;
> +
> +	for (cp = path; *cp; cp++) {
> +		if (*cp == '/' && cp[1])
> +			last_slash = cp;
> +	}
> +	pathlen = cp - path;
> +	if (last_slash) {
> +		basename = last_slash + 1;
> +		dirlen = last_slash - path;
> +	} else {
> +		basename = path;
> +		dirlen = 0;
> +	}
>  
> -	prepare_attr_stack(path);
> +	prepare_attr_stack(path, dirlen);
>  	for (i = 0; i < attr_nr; i++)
>  		check_all_attr[i].value = ATTR__UNKNOWN;
>  
> -	basename = find_basename(path);
> -	pathlen = strlen(path);
>  	rem = attr_nr;
>  	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
>  		rem = fill(path, pathlen, basename, stk, rem);

  reply	other threads:[~2013-01-16 19:08 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-15 13:35 [PATCH] attr: fix off-by-one directory component length calculation Nguyễn Thái Ngọc Duy
2013-01-15 16:17 ` Junio C Hamano
2013-01-15 19:14 ` Jean-Noël AVILA
2013-01-15 19:29   ` Junio C Hamano
2013-01-15 19:53     ` Jean-Noël AVILA
2013-01-15 20:49       ` Junio C Hamano
2013-01-15 23:24         ` Jeff King
2013-01-16  6:15           ` t9902 fails (Was: [PATCH] attr: fix off-by-one directory component length calculation) Torsten Bögershausen
2013-01-17 22:47             ` Jean-Noël AVILA
2013-01-18  0:04               ` Jonathan Nieder
2013-01-18  0:12                 ` t9902 fails Junio C Hamano
2013-01-18  5:20                 ` t9902 fails (Was: [PATCH] attr: fix off-by-one directory component length calculation) Torsten Bögershausen
2013-01-18 16:49                 ` t9902 fails Junio C Hamano
2013-01-18 20:15                   ` Junio C Hamano
2013-01-18 22:23                     ` Jean-Noël AVILA
2013-01-18 22:37                       ` Junio C Hamano
2013-01-18 22:57                         ` Junio C Hamano
2013-01-19  5:38                       ` Torsten Bögershausen
2013-01-19  7:52                         ` Re* " Junio C Hamano
2013-01-19  8:00                           ` [PATCH 1/2] help: include <common-cmds.h> only in one file Junio C Hamano
2013-01-19  8:02                             ` [PATCH 2/2] help --standard: list standard commands Junio C Hamano
2013-01-19 10:32                               ` Jean-Noël AVILA
2013-01-19 13:43                           ` Re* t9902 fails Jean-Noël AVILA
2013-01-19 10:23                         ` Jean-Noël AVILA
2013-01-16  1:08     ` [PATCH] attr: fix off-by-one directory component length calculation Duy Nguyen
2013-01-16  2:09       ` Duy Nguyen
2013-01-16  2:33         ` Junio C Hamano
2013-01-16  3:12           ` Duy Nguyen
2013-01-16  5:35             ` Junio C Hamano
2013-01-16  6:02               ` Duy Nguyen
2013-01-16 19:07                 ` Junio C Hamano [this message]
2013-01-16  1:03   ` Duy Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vzk09vtdr.fsf@alter.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=avila.jn@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).