title: Dealing with std::string/std::vector member variables while using boost::singleton_pool
body: <p>I am writing a performance critical application in which I am creating large number of objects of similar type to place orders. I am using <a href="http://www.boost.org/doc/libs/1_49_0/libs/pool/doc/html/boost_pool/pool/interfaces.html#boost_pool.pool.interfaces.interfaces.singleton_pool" rel="nofollow noreferrer">boost::singleton_pool</a> for allocating memory. Finally my class looks like this.</p>
  
  <pre><code>    class MyOrder{
      std::vector&lt;int&gt; v1_;
      std::vector&lt;double&gt; v2_;
  
      std::string s1_;
      std::string s2_;
  
  public:
      MyOrder(const std::string &amp;s1, const std::string &amp;s2): s1_(s1), s2_(s2) {}
  
      ~MyOrder(){}
  
      static void * operator new(size_t size); 
      static void operator delete(void * rawMemory) throw();
      static void operator delete(void * rawMemory, std::size_t size) throw();
  
  };
  
  struct MyOrderTag{};
  typedef boost::singleton_pool&lt;MyOrderTag, sizeof(MyOrder)&gt; MyOrderPool; 
  
  void* MyOrder:: operator new(size_t size)
  {
      if (size != sizeof(MyOrder)) 
          return ::operator new(size);
  
      while(true){
          void * ptr = MyOrderPool::malloc();
          if (ptr != NULL) return ptr;
  
          std::new_handler globalNewHandler = std::set_new_handler(0);
          std::set_new_handler(globalNewHandler);
  
          if(globalNewHandler)  globalNewHandler();
          else throw std::bad_alloc();
  
      }
  }
  
  void MyOrder::operator delete(void * rawMemory) throw()
  {
      if(rawMemory == 0) return; 
      MyOrderPool::free(rawMemory);
  }
  
  void MyOrder::operator delete(void * rawMemory, std::size_t size) throw()
  {
      if(rawMemory == 0) return;
      if(size != sizeof(Order)) {
          ::operator delete(rawMemory);
      }
      MyOrderPool::free(rawMemory);
  }
  </code></pre>
  
  <p>I recently posted a <a href="https://stackoverflow.com/questions/10466845/custom-allocation-using-boost-singleton-pool-slower-than-default">question</a> about performance benefit in using boost::singleton_pool. When I compared the performances of <a href="http://www.boost.org/doc/libs/1_49_0/libs/pool/doc/html/boost_pool/pool/interfaces.html#boost_pool.pool.interfaces.interfaces.singleton_pool" rel="nofollow noreferrer">boost::singleton_pool</a> and default allocator, I did not gain any performance benefit. When someone pointed that my class had members of the type std::string, whose allocation was not being governed by my custom allocator, I removed the std::string variables and reran the tests. This time I noticed a considerable performance boost. </p>
  
  <ol>
  <li><p>Now, in my actual application, I cannot get rid of member variables of time std::string and std::vector. Should I be using <a href="http://www.boost.org/doc/libs/1_49_0/libs/pool/doc/html/boost_pool/pool/interfaces.html#boost_pool.pool.interfaces.interfaces.pool_allocator" rel="nofollow noreferrer">boost::pool_allocator</a> with my std::string and std::vector member variables? </p></li>
  <li><p>boost::pool_allocator allocates memory from an underlying std::singleton_pool. Will it matter if different member variables (I have more than one std::string/std::vector types in my MyOrder class. Also I am employing pools for classes other than MyOrder which contain std::string/std::vector types as members too) use the same memory pool? If it does, how do I make sure that they do one way or the other?     </p></li>
  </ol>