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

Re: [PATCH 1/2] bswap.h: drop unaligned loads

From
Jeff King <peff@peff.net>
Date
Sep 25, 2020, 04:56 UTC
Message-ID
<20200925045620.GA3110076@coredump.intra.peff.net>
In-Reply-To
<b4b9475f-9c84-1889-835d-9f6e81198e5b@web.de>
On Fri, Sep 25, 2020 at 12:02:38AM +0200, René Scharfe wrote:
Show 12 quoted lines
> Older versions of gcc and clang didn't see through the shifting
> put_be32() implementation.  If you go back further there are also
> versions that didn't optimize the shifting get_be32().  And the latest
> icc still can't do that.
> 
> gcc 10.2 just optimizes all functions to a bswap and a mov.  Can't do
> any better than that, can you?
> 
> But why do we then see a difference in our benchmark results?  Not sure,
> but https://www.godbolt.org/z/7xh8ao shows that gcc is shuffling some
> instructions around depending on the implementation.  Switch to clang if
> you want to see more vigorous shuffling.

We do redefine ntohl(), etc in compat/bswap.h. Looking at them, I'd think the result would end up as a bswap instruction, though. And indeed, trying to feed that to godbolt results in the same output you got.

It does sound like older compilers were benefiting from the unaligned versions. Building with gcc-4.8 (from debian jessie in a docker container on the same machine), I get ~6.25s with the unaligned load versus ~6.6s with the bit-shifting code. So that's the opposite of how the newer compiler behaves.

Benchmarking clang-8 (which your results showed doesn't handle the shifting version well). It likewise is just _slightly_ slower after my patch (11.47s versus 11.57s).

Given that newer compilers behave the opposite way, and the overall small magnitude of the impact, I'm still comfortable with the change. It's nice to have a better understanding of how the compiler is impacting it (even if I am still confused how anything at all changes on the newer compilers). Thanks for digging.

-Peff
Previous: René ScharfeNext: brian m. carlson
Message 4 of 9 in “drop unaligned loads”
  1. 0/2 drop unaligned loadsJeff King, Sep 24, 2020
  2. 1/2 bswap.h: drop unaligned loadsJeff King, Sep 24, 2020
  3. René ScharfeSep 24, 2020
  4. Jeff KingSep 25, 2020
  5. brian m. carlsonSep 25, 2020
  6. Carlo ArenasSep 25, 2020
  7. Jeff KingSep 25, 2020
  8. Thomas GuyotSep 25, 2020
  9. 2/2 Revert "fast-export: use local array to store anonymized oid"Jeff King, Sep 24, 2020

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.