title: .NET Optimizing operations on an array of mathematical vectors with SIMD
body: <p>I developed a game, where periodically vectors are added to each others. For instance: <code>position += movement;</code> is the movement which is done on every tick of the game for every unit in the playfield.</p>
  
  <p>A vector looks like this, ofc with additional methods in it:</p>
  
  <pre><code>public struct Vector
  {
      public float X;
      public float Y;
  
      public Vector(float x, float y)
      {
          X = x;
          Y = y;
      }
  
      public static Vector operator +(Vector l, Vector r)
      {
          return new Vector(l.X + r.X, l.Y + r.Y);
      }
  }
  </code></pre>
  
  <p>I want to optimize those calculations to the maximum, because the playfield has many such units and those calculations are done more often, than querying values of the vectors for other operations.</p>
  
  <p>My idea is now to save the X and Y coordinates in two separate arrays for a faster calculation and to gather new Vectors from a holder or manager class. Maybe something like this:</p>
  
  <pre><code>public class VectorHolder
  {
      internal float[] x = new float[1024];
      internal float[] y = new float[1024];
      internal bool[] inUse = new bool[1024];
  
      public Vector NewVector()
      {
          return new Vector(this, aFreeSlotInTheArrays);
      }
  
      public void Add(VectorHolder holder)
      {
          int simdBlocks = Vector&lt;float&gt;.Count;
  
          for (int position = 0; position &lt; 1024; position += simdBlocks)
          {
              (new Vector&lt;float&gt;(x, position) + new Vector&lt;float&gt;(holder.x, position)).CopyTo(x, position);
              (new Vector&lt;float&gt;(y, position) + new Vector&lt;float&gt;(holder.y, position)).CopyTo(y, position);
          }
      }
  }
  
  public struct Vector
  {
      internal VectorHolder holder;
      internal int index;
  
      internal Vector(VectorHolder holder, int index)
      {
          this.holder = holder;
          this.index = index;
      }
  
      public float X =&gt; holder.x[index];
      public float Y =&gt; holder.y[index];
  
      public void Add(Vector vector)
      {
          holder.x[index] += vector.holder.x[vector.index];
          holder.y[index] += vector.holder.y[vector.index];
      }
  }
  </code></pre>
  
  <p>Here the <code>Vector</code>s of two <code>holder</code>s can be added (<code>VectorHolder.Add</code>) with very high performance. However the access to <code>X</code> or <code>Y</code> of a <code>Vector</code> now is quite slow.</p>
  
  <p>In my idea, the Vector wouldn't store the <code>holder</code> and <code>position</code>. It rather would store pointers to <code>X</code> and <code>Y</code> so that I wouldn't need to access the holders arrays directly.</p>
  
  <p>However, I don't know how to pin (<code>fixed</code>) pointers to arrays permanently, without making them a <code>fixed</code> field with predefined length. Also my method is not very good on the long run, because the memory of the holder will fragment and therefor the SIMD instructions will decrease in performance because more and more empty <code>Vector</code> slots will also be calculated.</p>
  
  <p><strong>My Question now is: Is there something like this already in the .NET Framework or on NuGet and I just overlooked it? Something which at best also takes care of the fragmentation, etc? If not, how can I pin memory permanently so that I can work on it without always calling <code>fixed</code>? Do you have a better suggestion to solve my problem than my approach?</strong></p>
  
  <p><em>Please note: The code in this question is only pseudo-code which may not run if you copy this to your IDE. The code is just there for better understanding of the question and of course would be not with fixed values, etc. in a real implementation.</em></p>