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

Re: [PATCH 1/5] add SWAP macro

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jan 30, 2017, 20:48 UTC
Message-ID
<alpine.DEB.2.20.1701301806591.3469@virtualbox>
In-Reply-To
<9bcae427-d654-a671-4368-030150168102@web.de>
Hi René,
On Mon, 30 Jan 2017, René Scharfe wrote:
Show 17 quoted lines
> Am 30.01.2017 um 16:39 schrieb Johannes Schindelin:
>
> > On Sat, 28 Jan 2017, René Scharfe wrote:
> >
> > > Add a macro for exchanging the values of variables.  It allows users
> > > to avoid repetition and takes care of the temporary variable for
> > > them.  It also makes sure that the storage sizes of its two
> > > parameters are the same.  Its memcpy(1) calls are optimized away by
> > > current compilers.
> >
> > How certain are we that "current compilers" optimize that away? And
> > about which "current compilers" are we talking? GCC? GCC 6? Clang?
> > Clang 3?  Clang 3.8.*? Visual C 2005+?
> 
> GCC 4.4.7 and clang 3.2 on x86-64 compile the macro to the same object
> code as a manual swap ; see https://godbolt.org/g/F4b9M9.  Both were
> released 2012.
Good. Thank you.
> That website doesn't offer Visual C(++), but since you added the
> original macro in e5a94313c0 ("Teach git-apply about '-R'", 2006) I
> guess we have that part covered. ;)

Back then, I was not able to build Git using Visual C *at all*. It required a Herculean effort to make that happen, and it did not happen before the Git for Windows project was started in 2007.

So I tried to verify that Visual C optimizes this well, and oh my deity, this was not easy. In Debug mode, it does not optimize, i.e. the memcpy() will be called, even for simple 32-bit integers. In Release mode, Visual Studio's defaults turn on "whole-program optimization" which means that the only swapping that is going on in the end is that the meaning of two registers is swapped, i.e. the SWAP() macro is expanded to... no assembler code at all.

After trying this and that and something else, I finally ended up with the file-scope optimized SWAP() resulting in this assembly code:

00007FF791311000 mov eax,dword ptr [rcx] 00007FF791311002 mov r8d,dword ptr [rdx] 00007FF791311005 mov dword ptr [rcx],r8d 00007FF791311008 mov dword ptr [rdx],eax

However, recent events (including some discussions on this mailing list which had unfortunate consequences) made me trust my instincts more. And my instincts tell me that it would be unwise to replace code that swaps primitive types in the straight-forward way by code that relies on advanced compiler optimization to generate efficient code, otherwise producing very suboptimal code.

The commit you quoted embarrasses me, and I have no excuse for it. I would love to see that myswap() ugliness fixed by replacing it with a construct that is simpler, and generates good code even without any smart compiler.

Ciao, Dscho

Previous: René ScharfeNext: René Scharfe
Message 5 of 50 in “introduce SWAP macro”
  1. 0/5 introduce SWAP macroRené Scharfe, Jan 28, 2017
  2. 1/5 add SWAP macroRené Scharfe, Jan 28, 2017
  3. Johannes SchindelinJan 30, 2017
  4. René ScharfeJan 30, 2017
  5. Johannes SchindelinJan 30, 2017
  6. René ScharfeJan 30, 2017
  7. Johannes SchindelinJan 31, 2017
  8. René ScharfeJan 31, 2017
  9. Ramsay JonesFeb 1, 2017
  10. Johannes SchindelinFeb 1, 2017
  11. Johannes SchindelinJan 30, 2017
  12. René ScharfeJan 30, 2017
  13. Johannes SixtJan 30, 2017
  14. Johannes SchindelinJan 30, 2017
  15. René ScharfeJan 30, 2017
  16. Brandon WilliamsJan 30, 2017
  17. René ScharfeJan 31, 2017
  18. Jeff KingJan 31, 2017
  19. Junio C HamanoJan 31, 2017
  20. Jeff KingJan 31, 2017
  21. Johannes SchindelinFeb 1, 2017
  22. Jeff KingFeb 1, 2017
  23. René ScharfeFeb 1, 2017
  24. Junio C HamanoFeb 1, 2017
  25. René ScharfeFeb 7, 2017
  26. Junio C HamanoFeb 7, 2017
  27. Johannes SchindelinFeb 8, 2017
  28. Johannes SchindelinJan 31, 2017
  29. Jeff KingApr 24, 2017
  30. Jeff KingApr 24, 2017
  31. Duy NguyenApr 24, 2017
  32. René ScharfeApr 28, 2017
  33. Jeff KingApr 28, 2017
  34. René ScharfeApr 29, 2017
  35. Jeff KingApr 30, 2017
  36. René ScharfeMay 2, 2017
  37. 2/5 apply: use SWAP macroRené Scharfe, Jan 28, 2017
  38. 3/5 use SWAP macroRené Scharfe, Jan 28, 2017
  39. Johannes SchindelinJan 30, 2017
  40. René ScharfeJan 30, 2017
  41. Junio C HamanoJan 30, 2017
  42. René ScharfeJan 31, 2017
  43. 4/5 diff: use SWAP macroRené Scharfe, Jan 28, 2017
  44. Johannes SchindelinJan 30, 2017
  45. René ScharfeJan 30, 2017
  46. Junio C HamanoJan 30, 2017
  47. 5/5 graph: use SWAP macroRené Scharfe, Jan 28, 2017
  48. Johannes SchindelinJan 30, 2017
  49. René ScharfeJan 30, 2017
  50. Junio C HamanoJan 30, 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.