H
Hendrik Schober
Hi,
consider the following small program created to test
the optimizer:
#include <iostream>
struct X {
X(int i) : i_(i) {}
int i_;
};
class Singleton {
private:
Singleton() : b(true) {};
public:
static Singleton& get()
{
static Singleton s;
return s;
}
bool b;
};
inline void f(const X&) {};
inline void g(const X& x)
{
if(Singleton::get().b)
std::cout << x.i_ << std::endl;
}
int main(int, char**)
{
f( X(42) );
std::cout << "Output? (0/1): ";
int i;
std::cin >> i;
Singleton::get().b = i!=0;
g( X(42) );
return 0;
}
When I look at the code generated in a release build
(/O2), I see that the optimizer completely eliminates
the call to 'f()'. In the call to 'g()' the code first
checks the bool and doesn't do anything if it is false.
(BTW, the decision is done by user input in order to
prevent the optimizer from finding out about it during
compilation.)
So far so good. In reality, I have code that is quite
similar, but much more complicated. Particularly, the
compiler seems to be unable to inline the ctor of 'X'.
Unfortunately, in the call to 'g()', it creates an 'X'
before it checks the bool. I suspect this it needs to
do because if it won't inline 'X's ctors it likely
also cannot proof that the ctor doesn't have any side
effects?
Or is there anything else that might prevent it from
skipping the initalization of arguments that are to
be thrown away?
Schobi
--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org
"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
consider the following small program created to test
the optimizer:
#include <iostream>
struct X {
X(int i) : i_(i) {}
int i_;
};
class Singleton {
private:
Singleton() : b(true) {};
public:
static Singleton& get()
{
static Singleton s;
return s;
}
bool b;
};
inline void f(const X&) {};
inline void g(const X& x)
{
if(Singleton::get().b)
std::cout << x.i_ << std::endl;
}
int main(int, char**)
{
f( X(42) );
std::cout << "Output? (0/1): ";
int i;
std::cin >> i;
Singleton::get().b = i!=0;
g( X(42) );
return 0;
}
When I look at the code generated in a release build
(/O2), I see that the optimizer completely eliminates
the call to 'f()'. In the call to 'g()' the code first
checks the bool and doesn't do anything if it is false.
(BTW, the decision is done by user input in order to
prevent the optimizer from finding out about it during
compilation.)
So far so good. In reality, I have code that is quite
similar, but much more complicated. Particularly, the
compiler seems to be unable to inline the ctor of 'X'.
Unfortunately, in the call to 'g()', it creates an 'X'
before it checks the bool. I suspect this it needs to
do because if it won't inline 'X's ctors it likely
also cannot proof that the ctor doesn't have any side
effects?
Or is there anything else that might prevent it from
skipping the initalization of arguments that are to
be thrown away?
Schobi
--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org
"The presence of those seeking the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett