title: performance and memory optimization (class vs struct)
body: <p>I am starting to work on a polygon library, whose side are bezier curves. The basic don't quite work yet so I can't really write stress / performance test program yet. nor show much code.</p>
  
  <p>At any rate I have classes like that</p>
  
  <pre><code>public class BezierFragment
  {
      Point2D[] controlPoints; // length: 2, 3 or 4
  }
  public struct Point2D { public double X, Y; }
  </code></pre>
  
  <p>But I am wondering  whether I should instead use struct (for less heap allocation and more stack copies)</p>
  
  <pre><code>public struct BezierFragment
  {
      byte order; // 2, 3, or 4
      Point2D p0, p1, p2, p3;
  }
  public struct Point2D { public double X, Y; }
  </code></pre>
  
  <p>The later is much easier on the memory management, but might require more copy (of 65 bytes) struct. I wonder which one would be best. 
  How would I figure out the best option?</p>
  
  <p>The (possible) memory management issues of the first choice might only be apparent when lots of Point2D[] array are saved in generation 2 memory pool and then destroyed...</p>
  
  <p>Any tip?</p>