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

Re: [PATCH 2/2] hashcmp: use memcmp instead of open-coded loop

From
René Scharfe <l.s.r@web.de>
Date
Aug 9, 2017, 14:55 UTC
Message-ID
<2b111a78-dea4-85ff-3d8f-94d2eee0ca26@web.de>
In-Reply-To
<20170809101645.7wv4mcsmnejxzhvs@sigill.intra.peff.net>
Am 09.08.2017 um 12:16 schrieb Jeff King:
Show 20 quoted lines
> In 1a812f3a70 (hashcmp(): inline memcmp() by hand to
> optimize, 2011-04-28), it was reported that an open-coded
> loop outperformed memcmp() for comparing sha1s.
> 
> Discussion[1] a few years later in 2013 showed that this
> depends on your libc's version of memcmp(). In particular,
> glibc 2.13 optimized their memcmp around 2011. Here are
> current timings with glibc 2.24 (best-of-five, on
> linux.git):
> 
>    [before this patch, open-coded]
>    $ time git rev-list --objects --all
>    real	0m35.357s
>    user	0m35.016s
>    sys	0m0.340s
> 
>    [after this patch, memcmp]
>    real	0m32.930s
>    user	0m32.630s
>    sys	0m0.300s
Nice.  And here's the size of the git executable in my build:
         unstripped stripped
  before    8048176  2082416
  after     8006064  2037360
Show 5 quoted lines
> I also wondered if using memcmp() could be a hint to the compiler to use
> an intrinsic or some other trick, especially because the "len" here is a
> constant. But in a toy function compiled with "gcc -S", it looks like we
> do keep the call to memcmp (so the speedup really is glibc, and not some
> compiler magic).
GCC 7 inlines memcmp() if we only need a binary result:
	https://godbolt.org/g/iZ11Ne
René
Previous: Jeff KingNext: Jeff King
Message 13 of 14 in “sha1_file: avoid comparison if no packed hash matches the first byte”
  1. sha1_file: avoid comparison if no packed hash matches the first byteRené Scharfe, Aug 8, 2017
  2. Jonathan NiederAug 8, 2017
  3. Junio C HamanoAug 8, 2017
  4. Jeff KingAug 8, 2017
  5. Jeff KingAug 8, 2017
  6. Junio C HamanoAug 9, 2017
  7. Jeff KingAug 9, 2017
  8. Jeff KingAug 9, 2017
  9. 1/2 sha1_file: drop experimental GIT_USE_LOOKUP searchJeff King, Aug 9, 2017
  10. Junio C HamanoAug 9, 2017
  11. Jeff KingAug 9, 2017
  12. 2/2 hashcmp: use memcmp instead of open-coded loopJeff King, Aug 9, 2017
  13. René ScharfeAug 9, 2017
  14. Jeff KingAug 9, 2017

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.