title: Fixing performance decrease using double wrapper class for bit manipulations (C++, clang)
body: <p>The question I have is "Can I write a bitset-type class which can both be used with an internal unsigned and dynamic_bitset internal representation without losing the performance of an exclusive unsigned bitset class?"</p>
  
  <p>To give some context, I'm trying to create a class which acts as a bitset where frequent manipulation that I require are implemented. First draft of this class uses an unsigned long representation internally.</p>
  
  <p>However I need to go beyond 64 or 128 bits in some cases and this would require the use of either boost's dynamic bitset, or an array of unsigned longs as an internal representation.</p>
  
  <p>Now, my first draft is as performant as the code that would simply use a naked unsigned long (using the -O3 flag for my compiler). And I fully realise that I'm not able to maintain this performance in the case of using a dynamic bitset. However I would like to only write my algorithms once, using my class, instead of writing one code with unsigned representation and one using dynamic bitset. So I created a bitsetwrapper Class which has a pointer to an abstract bitset which can either be the bitset with the internal unsigned long bitset or a bitset with an internal dynamic bitset. Which derived class it shall point to is then decided by the amount of bits you need to use.</p>
  
  <p>This way the I never have to worry about using pointers to abstract classes as these are confined in my wrapper. An example:</p>
  
  <pre><code>    class BitsetBase{}
      class UnsignedBitset : public BitsetBase{
              unsigned long representation;
      }
      class DynamicBitsetBitset : public BitsetBase{
              dynamic_bitset&lt;&gt; representation;
      }
  
      class BitsetWrapper{
          *BitsetBase bitset;
      }
  </code></pre>
  
  <p>Now I've run into some performance issues which I have so far failed to fix completely.</p>
  
  <p>Initial performance benchmarks are as follows (relative comparison):</p>
  
  <pre><code>    Unsinged long code : 1s
      UnsingedBitset code : 1s
      BitsetWrapper code (using UnsingedBitset) : 4s
  </code></pre>
  
  <p>To give you some extra context, many copies are being made in all 3 instances. This is what causes the increase in the BitsetWrapper to 4s. Because in my initial attempt I used "new" to initialise Bitset instances.</p>
  
  <p>Now I managed to circumvent new completely by initialising UnsingedBitset externally and passing them as an argument for my wrapper constructor.<br>
  Yielding a significant performance increase.</p>
  
  <pre><code>    Unsinged long code : 1s
      UnsingedBitset code : 1s
      BitsetWrapper code (using UnsingedBitset) : 2.4s
  </code></pre>
  
  <p>However it is vital to reach the 1s performance. I'm quite surprised that the UnsignedBitset version has the same performance as the raw Unsigned long code. My guess is that the compiler can optimise it in someway, but can no longer do that for the "double" wrapper. Does anyone have an idea why the performance is so much worse, and if there is an other way to fix my issues? (ps. I've also tried boost::variant this is also 3 times slower)</p>
  
  <p>Code example:</p>
  
  <pre><code>    for(1000000 loops){                
          AnyClass bitset(random_input) 
          while(!bitset.equalsZero()){
              bitset.removeLeastSignificantBit()
              AnyClass bitset2 = bitset
              bitset2.invert()
              while(!bitset2.equalsZero()){
                  result += bitset2.someManipulation();
              }
          }
      }
  </code></pre>
  
  <p>To ask the question more clearly. Is it possible for me to create a wrapper around a representation which can internally chose which representation it should use (based on some parameter) without losing performance if the internal representation is unsigned long compared to the wrapper with a fixed unsigned long representation.</p>
  
  <p>Example of a code called is :</p>
  
  <pre><code>    void invert(){
              representation = ~representation;
      )
  </code></pre>
  
  <p>(no performance loss)
  Which would then become: </p>
  
  <pre><code>   void invert(){
         bitset_instance-&gt;invert();
     }
  </code></pre>
  
  <p>in the Bitset wrapper (performance loss).</p>