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

Re: [PATCH v2 3/3] find_unique_abbrev: early out without a memcpy

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 21, 2010, 20:23 UTC
Message-ID
<7v39ztmnjp.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1269111329-5896-4-git-send-email-kusmabite@gmail.com>
Erik Faye-Lund <kusmabite@googlemail.com> writes:
Show 19 quoted lines
> Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
> ---
>  sha1_name.c |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/sha1_name.c b/sha1_name.c
> index bf92417..2b1be58 100644
> --- a/sha1_name.c
> +++ b/sha1_name.c
> @@ -196,10 +196,10 @@ const char *find_unique_abbrev(const unsigned char *sha1, int len)
>  	int status, exists;
>  	static char hex[41];
>  
> +	if (len == 40 || !len)
> +		return sha1_to_hex(sha1);
>  	exists = has_sha1_file(sha1);
>  	memcpy(hex, sha1_to_hex(sha1), 40);
> -	if (len == 40 || !len)
> -		return hex;

This is somewhat iffy. hex[] being static means there can only be one outstanding return value from f-u-a being used, iow

	printf("%s %s", f-u-a(a, 0), f-u-a(b, 0))

is a no-no. But at the same time, it means that you can use one more recycled buffer than sha1_to_hex() gives us, so this may be safe:

	char *ua = f-u-a(a, 0);
        printf("%s %s %s %s %s", ua,
            sha1_to_hex(b), sha1_to_hex(c),  sha1_to_hex(d), sha1_to_hex(e));
but with the above it probably is not anymore, no?

As an optimization patch, I would buy that delaying the "exists" check until the "no abbreviation" check returned early would make sense, though.

Previous: Erik Faye-LundNext: Erik Faye-Lund
Message 5 of 6 in “cherry-abbrev”
  1. 0/3 cherry-abbrevErik Faye-Lund, Mar 20, 2010
  2. 1/3 cherry: support --abbrev optionErik Faye-Lund, Mar 20, 2010
  3. 2/3 ls: remove redundant logicErik Faye-Lund, Mar 20, 2010
  4. 3/3 find_unique_abbrev: early out without a memcpyErik Faye-Lund, Mar 20, 2010
  5. Junio C HamanoMar 21, 2010
  6. Erik Faye-LundMar 21, 2010

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.