title: Memory layout optimization in C#
body: <p>Disclaimer : there might be some misconceptions in the phrasing below, please correct me if i misinterpreted the wy my code is handled in C# between the moment i write it to the point it looks like zeroes and ones</p>
  
  <p><strong>The questions are the following (those are linked) :</strong></p>
  
  <p>Is there any way in C# where my Data structures and/or my data manipulation implementation, will have a performance impact whether I use optimization techniques or not?</p>
  
  <p>What does the compiler do when outputting IL, is it reliable? 
  Meaning : if i make my data SOA will it be SOA in IL? Always?</p>
  
  <p>What happens to my data structure when the JIT reads the IL? is it changed? is it optimized automatically to fit my processor?</p>
  
  <p>cf: <a href="http://channel9.msdn.com/events/Build/2013/4-329" rel="nofollow noreferrer">that talk about C/C++</a></p>
  
  <p>I know that this talk is targeted at native code, and talks about the specifics of the processor layout vs your data layout in native code.</p>
  
  <p>I also know the C# compiler and the JIT compiler will optimize stuff for me in regards of those issues.</p>
  
  <p>Basically I wonder if those kind of optimization will have an impact on my perfs : </p>
  
  <ul>
  <li>SOA instead of AOS</li>
  <li>Vectors access patterns (to be accessed contiguously in memory)</li>
  <li>etc... you name it...</li>
  </ul>
  
  <p>I work in game development and performance is critical, we manipulate large amounts of data and we need to do that minimum 24 times per second, I cannot have the GC do stuff for 300ms or memory to be accessed/allocated all over the place when I'm trying to detect collisions between 3000 different objects</p>
  
  <p>For reference about stuff i read but did not really answer the question : </p>
  
  <ul>
  <li><p><a href="http://blogs.msdn.com/b/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx" rel="nofollow noreferrer">Excellent Eric Lippert article about structs and values types in C#</a> (please read it if you think values types are always on the stack in C# you're in for a treat)</p></li>
  <li><p><a href="http://channel9.msdn.com/Shows/Defrag-Tools/Defrag-Tools-125-PerfView-Part-8" rel="nofollow noreferrer">Excellent video about PerfView to track your GC behaviour and it's impact on your perfs</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/21046409/best-practices-to-optimize-memory-in-c-sharp">That SO question about Best practices to optimize memory in C#</a> (and more importantly it's answer)</p></li>
  </ul>
  
  <p>But those do not answer the performance cost relative to processor and data layout implementation.</p>
  
  <hr>
  
  <p>To go further after what Hans answered : </p>
  
  <p>When you say : "You can pursue SOA but that doesn't help. Yes, your program will slow down because of all that structure copying and does so in a deterministic way. But it doesn't stop the rain. You get the worst of both, a slow program and the exact same pauses."</p>
  
  <p>It does not mean that my program benefits nothing from SOA it WILL be faster (potentially) because it will help processing my data. Just that it will have no impact on the GC in itself.</p>
  
  <p>Other thing is that If i do not do SOA or other improvements in my data layout, the compiler won't improve that for me right? i cannot rely on the compiler to deal with that kind of things?</p>