Functor not optimized away [modified]
-
Here is what the code looked like:
class Functor
{
public: static bool SomeTest(int pSomeValue)
{
if (pSomeValue > pSomeConst)
return true;
return false;
};class DoesSomething
{
public: template<class T> static void Do(int pSomeParam)
{
DoHelper(pSomeParam);
...
}protected: template<class T> static void DoHelper(int pSomeParam)
{
// Help doing something
if (T::SomeTest(pSomeParam))
// Something
else
// Something else
}
};class IsACaller
{
public: void Call()
{
DoesSomething::Do<Functor>(2);
}
};This code was working, but very slow. So I scanned it and discovered I had missed the following:
DoHelper(pSomeParam);
should be replaced by
DoHelper<T>(pSomeParam);
I corrected the code, and as expected the functor was optimized away and the code ran fast. Still, I think the compiler should have complained about this. -- modified at 1:56 Monday 27th November, 2006 (changed <> using html tags)
-
Here is what the code looked like:
class Functor
{
public: static bool SomeTest(int pSomeValue)
{
if (pSomeValue > pSomeConst)
return true;
return false;
};class DoesSomething
{
public: template<class T> static void Do(int pSomeParam)
{
DoHelper(pSomeParam);
...
}protected: template<class T> static void DoHelper(int pSomeParam)
{
// Help doing something
if (T::SomeTest(pSomeParam))
// Something
else
// Something else
}
};class IsACaller
{
public: void Call()
{
DoesSomething::Do<Functor>(2);
}
};This code was working, but very slow. So I scanned it and discovered I had missed the following:
DoHelper(pSomeParam);
should be replaced by
DoHelper<T>(pSomeParam);
I corrected the code, and as expected the functor was optimized away and the code ran fast. Still, I think the compiler should have complained about this. -- modified at 1:56 Monday 27th November, 2006 (changed <> using html tags)
your code lacks the <>'s ;)
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us! -
your code lacks the <>'s ;)
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us!Thanks for pointing this out. The <>'s were there but I did not use "& lt" or "& gt", so they seem to have disappeared. I'll fix it.
-
Thanks for pointing this out. The <>'s were there but I did not use "& lt" or "& gt", so they seem to have disappeared. I'll fix it.
-
Allright for HTML, any suggestions for the C++ topic?
-
Here is what the code looked like:
class Functor
{
public: static bool SomeTest(int pSomeValue)
{
if (pSomeValue > pSomeConst)
return true;
return false;
};class DoesSomething
{
public: template<class T> static void Do(int pSomeParam)
{
DoHelper(pSomeParam);
...
}protected: template<class T> static void DoHelper(int pSomeParam)
{
// Help doing something
if (T::SomeTest(pSomeParam))
// Something
else
// Something else
}
};class IsACaller
{
public: void Call()
{
DoesSomething::Do<Functor>(2);
}
};This code was working, but very slow. So I scanned it and discovered I had missed the following:
DoHelper(pSomeParam);
should be replaced by
DoHelper<T>(pSomeParam);
I corrected the code, and as expected the functor was optimized away and the code ran fast. Still, I think the compiler should have complained about this. -- modified at 1:56 Monday 27th November, 2006 (changed <> using html tags)
*off topic* I have seen this time any time again with templates. Due to the complexity of what the compiler is having to do, the optimizer can fail in all sorts of strange ways. In general, I have found the templates perform between %80 to %300 percent of their non-template counter parts. Overall, the optimizations work just fine. However, when it works well, it just tends to work a bit better than non-template code and when it works poorly, it REALLY works poorly. Scott Meyer's stuff about for_each working just as well as for is total bunk. For simple loops that can usually be contained in registers when using for will often result in stack space being used thus drastically hurting performance when using for_each.
Tim Smith I'm going to patent thought. I have yet to see any prior art.