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
Jeff King <peff@peff.net>
Date
Aug 9, 2017, 15:06 UTC
Message-ID
<20170809150644.ny565zdhki5zgdzr@sigill.intra.peff.net>
In-Reply-To
<2b111a78-dea4-85ff-3d8f-94d2eee0ca26@web.de>
On Wed, Aug 09, 2017 at 04:55:43PM +0200, René Scharfe wrote:
Show 9 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

Cute. It turns it into a series of 8-byte xors. The original open-coded loop doesn't end up nearly as optimized with gcc-7.

I suspect many calls in practice are of the binary-result type. So some of the speedup I saw may have been from compiler improvements and not libc improvements. Still, I think the general argument is the same, replacing "if your libc memcmp is fast" with "if your libc/compiler makes memcmp fast".

-Peff
Previous: René Scharfe
Message 14 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.