C++ inlining of references

Thomas Schatzl thomas.schatzl at oracle.com
Tue Mar 21 09:36:24 UTC 2017


Hi Ioi,

On Tue, 2017-03-21 at 17:25 +0800, Ioi Lam wrote:
> How good are C++ compilers with optimizing references in inline 
> functions? I have 2 functions that have a large common header:
> 
> foo() {
>      int x = xxx();
>      int y = yyy();
>      dofoo(x, y);
> }
> bar() {
>      int x = xxx();
>      int y = yyy();
>      dobar(x, y);
> }
> 
> So I want to refactor it to
> 
> inline void common(int&x, int &y) {
>      x = xxx();
>      y = yyy();
> }
> 
> foo() {
>      int x, y;
>      common(x, y);
>      dofoo(x, y);
> }
> bar() {
>      int x, y;
>      common(x, y);
>      dobar(x, y);
> }
> 
> Because the common header modifies 2 variables, there's no easy way
> to pass them back easily. So I pass them as references to the inline
> function.
> 
> Can I expect modern C++ compilers to always generate equally optimal 
> code for the before/after cases?

  try it yourselves: https://godbolt.org/ :)

Thanks,
  Thomas



More information about the hotspot-dev mailing list