title: Fastest way to copy a blittable struct to an unmanaged memory location (IntPtr)
body: <p>I have a function similar to the following:</p>
  
  <pre><code>[MethodImpl(MethodImplOptions.AggressiveInlining)]
  public void SetVariable&lt;T&gt;(T newValue) where T : struct {
      // I know by this point that T is blittable (i.e. only unmanaged value types)
  
      // varPtr is a void*, and is where I want to copy newValue to
      *varPtr = newValue; // This won't work, but is basically what I want to do
  }
  </code></pre>
  
  <p>I saw Marshal.StructureToIntPtr(), but it seems quite slow, and this is performance-sensitive code. If I knew the type <code>T</code> I could just declare <code>varPtr</code> as a <code>T*</code>, but... Well, I don't.</p>
  
  <p>Either way, I'm after the fastest possible way to do this. 'Safety' is not a concern: By this point in the code, I know that the size of the struct <code>T</code> will fit exactly in to the memory pointed to by <code>varPtr</code>.</p>