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

Re: [PATCH] Make strbuf_cmp inline, constify its arguments and optimize it a bit

From
Pierre Habouzit <madcoder@debian.org>
Date
Oct 8, 2007, 07:23 UTC
Message-ID
<20071008072312.GA22552@artemis.corp>
In-Reply-To
<87odfapefc.fsf@catnip.gol.com>
On Mon, Oct 08, 2007 at 01:45:27AM +0000, Miles Bader wrote:
Show 27 quoted lines
> Alex Riesen <raa.lkml@gmail.com> writes:
> > int strbuf_cmp2(struct strbuf *a, struct strbuf *b)
> > {
> > 	int len = a->len < b->len ? a->len: b->len;
> > 	int cmp = memcmp(a->buf, b->buf, len);
> > 	if (cmp)
> > 		return cmp;
> > 	return a->len < b->len ? -1: a->len != b->len;
> > }
> 
> BTW, why are you making such effort to return only -1, 0, or 1 in the
> last line?  memcmp/strcmp make no such guarantee; e.g. glibc says:
> 
>      The `strcmp' function compares the string S1 against S2, returning
>      a value that has the same sign as the difference between the first
>      differing pair of characters (interpreted as `unsigned char'
>      objects, then promoted to `int').
> 
>      If the two strings are equal, `strcmp' returns `0'.
> 
>      A consequence of the ordering used by `strcmp' is that if S1 is an
>      initial substring of S2, then S1 is considered to be "less than"
>      S2.
> 
> So I think the last line can just be:
> 
>    return a->len - b->len;
  Won't work because ->len are size_t and return value is int, so on 64
bits platform, this has chances to overflow.
  FWIW I believe we are doing micro-benchs in a function that is used in
2 places in git right now.
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Previous: Miles BaderNext: Florian Weimer
Message 14 of 25 in “mini-refactor in rerere.c”
  1. Pierre HabouzitSep 24, 2007
  2. Johannes SchindelinSep 24, 2007
  3. Junio C HamanoSep 26, 2007
  4. Pierre HabouzitSep 26, 2007
  5. Make strbuf_cmp inline, constify its arguments and optimize it a bitAlex Riesen, Oct 7, 2007
  6. Timo HirvonenOct 7, 2007
  7. Pierre HabouzitOct 7, 2007
  8. Miles BaderOct 7, 2007
  9. David KastrupOct 7, 2007
  10. Alex RiesenOct 7, 2007
  11. Wincent ColaiutaOct 7, 2007
  12. Alex RiesenOct 7, 2007
  13. Miles BaderOct 8, 2007
  14. Pierre HabouzitOct 8, 2007
  15. Florian WeimerOct 8, 2007
  16. Alex RiesenOct 8, 2007
  17. Johannes SchindelinOct 7, 2007
  18. Timo HirvonenOct 7, 2007
  19. Johannes SchindelinOct 7, 2007
  20. Pierre HabouzitOct 7, 2007
  21. David KastrupOct 7, 2007
  22. Alex RiesenOct 7, 2007
  23. David KastrupOct 7, 2007
  24. Alex RiesenOct 7, 2007
  25. Jeff KingOct 8, 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.