title: Efficient cloning of cached objects
body: <p>We have an application that performs comparisons on data objects to determine if one version of the object is different than another. Our application also does some extensive caching of these objects, and we've run into a bit of a performance issue when it comes to doing these comparisons.</p>
  
  <p>Here's the workflow:</p>
  
  <ol>
  <li>Data item 1 is the current item in memory. This item was initially retrieved from cache and deep cloned (all sub objects such as Dictionaries etc). Data item 1 is then edited, and its properties are modified.</li>
  <li>We are then comparing this object against the original version that was stored in cache. Since Data item 1 was cloned and its properties changed, these objects should be different.</li>
  </ol>
  
  <p>There are a couple of issues here. </p>
  
  <p>The main issue is our deep clone method is very expensive. We profiled it against a shallow clone and it was 10x slower. That's crap. Here's our method to deep clone:</p>
  
  <pre><code>    public object Clone()    
      {
          using (var memStream = new MemoryStream())
          {
              var binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
              binaryFormatter.Serialize(memStream, this); 
              memStream.Seek(0, SeekOrigin.Begin);
              return binaryFormatter.Deserialize(memStream);
          }
      }
  </code></pre>
  
  <p>We were initially using the following to clone:</p>
  
  <pre><code>public object Clone()
  {
      return this.MemberwiseClone();
  }
  </code></pre>
  
  <p>This was more performant, but because it does a shallow clone all the complex objects that were properties of this object, such as Dictionaries etc, were not cloned. The object would still contain the same reference as the object that was in the cache, therefore the properties would be the same upon comparison.</p>
  
  <p>So, does anyone have an efficient way of doing a deep clone on C# objects that would cover cloning the entire object graph?</p>