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

Re: [PATCH] Optimize prefixcmp()

From
MCMarco Costalba <mcostalba@gmail.com>
Date
Dec 30, 2007, 14:50 UTC
Message-ID
<e5bfff550712300650j2ea70032jaca893b734592184@mail.gmail.com>
In-Reply-To
<20071230135820.GB25917@artemis.madism.org>
On Dec 30, 2007 2:58 PM, Pierre Habouzit <madcoder@debian.org> wrote:
Show 12 quoted lines
> >
> >   This code doesn't work if prefix is "". You want something like:
> >
> >     for (; *prefix; prefix++, str++) {
> >         if (*str != *prefix)
> >             return *(unsigned const char *)prefix - *(unsigned const char *)str;
> >     }
> >     return 0;
>
>   Which happens to be basically the same than what Dscho wrote, though I
> suppose the compiler can compile that more efficiently than his code.
>

Yes, your version covers the *prefix == "" case too. If this case is important for us we could use something as

static inline int prefixcmp(const char *str, const char *prefix)
{
	do {
		if (*str != *prefix)
			return (!*prefix ? 0 : *(unsigned const char *)prefix - *(unsigned
const char *)str);
		if (!*(++prefix))
			return 0;
		str++;
	} while (1);
}

But your code is *surely* nicer then this one. But, for unknown reasons, this code happens to be faster, probably as you say the compiler optimizes away the second check in the return statement so that this version is slightly faster then the 'for' loop one, but admitelly we are going to much in the academic now.

If *prefix == "" case is to be considered I vote for your/Johannes version because it's "better code" (tm).

Marco
Previous: Pierre HabouzitNext: Marco Costalba
Message 9 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.