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

Re: [PATCH] Optimize prefixcmp()

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 30, 2007, 00:44 UTC
Message-ID
<7vir2h9fkh.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<Pine.LNX.4.64.0712292019450.14355@wbgn129.biozentrum.uni-wuerzburg.de>
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 37 quoted lines
> Certain codepaths (notably "git log --pretty=format...") use
> prefixcmp() extensively, with very short prefixes.  In those cases,
> calling strlen() is a wasteful operation, so avoid it.
>
> Initial patch by Marco Costalba.
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>
> 	On Sat, 29 Dec 2007, Marco Costalba wrote:
>
> 	> In case the prefix string is a single char avoid a costly call 
> 	> to strlen() + strncmp()
>
> 	Could you test this patch, please?
>
> 	Not only does it avoid the strlen() call also for longer prefixes; 
> 	it also avoids a C++ comment.
>
>  git-compat-util.h |    6 +++++-
>  1 files changed, 5 insertions(+), 1 deletions(-)
>
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 79eb10e..7059cbd 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -398,7 +398,11 @@ static inline int sane_case(int x, int high)
>  
>  static inline int prefixcmp(const char *str, const char *prefix)
>  {
> -	return strncmp(str, prefix, strlen(prefix));
> +	for (; ; str++, prefix++)
> +		if (!*prefix)
> +			return 0;
> +		else if (*str != *prefix)
> +			return (unsigned char)*prefix - (unsigned char)*str;
>  }

Losing the unnecessary check for !str || !prefix is a good change.

While I think, for the readability's sake, Marco's original without the unnecessary check would be the way to go, a profile from your totally inlined version would also be interesting, as it may or may not beat the underlying strncmp(), which could be highly optimized.

Previous: Andy ParkinsNext: René Scharfe
Message 13 of 20 in “Speedup prefixcmp() common case”
  1. Speedup prefixcmp() common caseMarco Costalba, Dec 29, 2007
  2. Optimize prefixcmp()Johannes Schindelin, Dec 29, 2007
  3. Marco CostalbaDec 29, 2007
  4. Johannes SchindelinDec 29, 2007
  5. Marco CostalbaDec 29, 2007
  6. Marco CostalbaDec 30, 2007
  7. Pierre HabouzitDec 30, 2007
  8. Pierre HabouzitDec 30, 2007
  9. Marco CostalbaDec 30, 2007
  10. Marco CostalbaDec 30, 2007
  11. Johannes SchindelinDec 30, 2007
  12. Andy ParkinsDec 29, 2007
  13. Junio C HamanoDec 30, 2007
  14. René ScharfeJan 2, 2008
  15. Junio C HamanoJan 2, 2008
  16. René ScharfeJan 3, 2008
  17. Junio C HamanoDec 29, 2007
  18. Marco CostalbaDec 29, 2007
  19. Junio C HamanoDec 30, 2007
  20. Marco CostalbaDec 29, 2007

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.