C++ inlining of references

Ioi Lam ioi.lam at oracle.com
Tue Mar 21 09:25:02 UTC 2017


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?

Thanks
- Ioi


More information about the hotspot-dev mailing list