title: Slices of structs vs. slices of pointers to structs
body: <p>I often work with slices of structs. Here's an example for such a struct:</p>
  
  <pre><code>type MyStruct struct {
      val1, val2, val3    int
      text1, text2, text3 string
      list                []SomeType
  }
  </code></pre>
  
  <p>So I define my slices as follows:</p>
  
  <pre><code>[]MyStruct
  </code></pre>
  
  <p>Let's say I have about a million elements in there and I'm working heavily with the slice:</p>
  
  <ul>
  <li>I append new elements often. (The total number of elements is unknown.)</li>
  <li>I sort it every now and then.</li>
  <li>I also delete elements (although not as much as adding new elements).</li>
  <li>I read elements often and pass them around (as function arguments).</li>
  <li>The content of the elements themselves doesn't get changed.</li>
  </ul>
  
  <p>My understanding is that this leads to a lot of shuffling around of the actual struct. The alternative is to create a slice of pointers to the struct:</p>
  
  <pre><code>[]*MyStruct
  </code></pre>
  
  <p>Now the structs remain where they are and we only deal with pointers which I assume have a smaller footprint and will therefore make my operations faster. But now I'm giving the garbage collector a lot more work.</p>
  
  <ul>
  <li>Can you provide general guidelines of when to work with structs directly vs. when to work with pointers to structs?</li>
  <li>Should I worry about how much work I leave to the GC?</li>
  <li>Is the performance overhead of copying a struct vs. copying a pointer negligible?</li>
  <li>Maybe a million elements is not much. How does all of this change when the slice gets much bigger (but still fits in RAM, of course)?</li>
  </ul>