Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Functor not optimized away [modified]

Functor not optimized away [modified]

Scheduled Pinned Locked Moved Clever Code
htmlhelp
6 Posts 4 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pierre Leclercq
    wrote on last edited by
    #1

    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)

    P T 2 Replies Last reply
    0
    • P Pierre Leclercq

      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)

      P Offline
      P Offline
      peterchen
      wrote on last edited by
      #2

      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!

      P 1 Reply Last reply
      0
      • P peterchen

        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!

        P Offline
        P Offline
        Pierre Leclercq
        wrote on last edited by
        #3

        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.

        G 1 Reply Last reply
        0
        • P Pierre Leclercq

          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.

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          The code for < is < :)

          --- b { font-weight: normal; }

          P 1 Reply Last reply
          0
          • G Guffa

            The code for < is < :)

            --- b { font-weight: normal; }

            P Offline
            P Offline
            Pierre Leclercq
            wrote on last edited by
            #5

            Allright for HTML, any suggestions for the C++ topic?

            1 Reply Last reply
            0
            • P Pierre Leclercq

              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)

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #6

              *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.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups