home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 33,049 of 33,346   
   Andy Lutomirski to All   
   Vector sizes and other missed optimizati   
   16 May 13 12:45:36   
   
   From: luto@amacapital.net   
      
   This has been annoying me for a while:   
      
   #include    
      
   template   
   class Vec   
   {   
   public:   
     Vec(size_t len) : len_(len) { data_ = new T[len]; }   
     ~Vec() { delete data_; }   
     Vec(const Vec &) = delete;   
     void operator = (const Vec &) = delete;   
      
     size_t size() const { return len_; }   
     T &operator [] (size_t pos) { return data_[pos]; }   
   private:   
     size_t len_;   
     T * data_;   
   };   
      
   template   
   void IncrementVec(Vec &v)   
   {   
     for (size_t i = 0; i < v.size(); i++)   
       ++v[i];   
   }   
      
   void f1(Vec &v)   
   {   
     // This is slow -- the compiler can't figure out that data_[i]   
     // does not alias len_;   
     IncrementVec(v);   
   }   
      
   void f2(Vec &v)   
   {   
     // This is fast -- type-based alias analysis works.   
     IncrementVec(v);   
   }   
      
   The code generation for f1 loads len_ on every iteration of the loop.   
   libstdc++'s vector avoids this problem by storing a one-past-the-end   
   pointer instead of a size, but that has other issues (in my use case,   
   I'm storing a multidimensional object, and encoding strides in pointers   
   gets awkward).   
      
   Are there any ways, idiomatic or otherwise, to tell a compiler (even in   
   theory) that data_ can't point to len_?  Adding a restrict qualifier to   
   data_ has no effect in g++ (correctly AFAICT -- data_ isn't a function   
   parameter).   
      
   Thanks,   
   Andy   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca