Efficient random updates to mutable buffer pointer

Hi all. I am writing an application that needs to be able to update a buffer of memory (around ~50mb) by reading a given triplet of doubles, incrementing them with a certain value, and then writing them back. Unfortunately the accesses will be somewhat random and unpredictable.

I've got two questions:

  1. What is the most efficient way to read-modify-write to a buffer in Swift? I'm currently using an UnsafeMutableBufferPointer and then doing buffer[ix] = buffer[ix] + value, where the elements in the array are structs which are triplets of doubles. Is there a more efficient way to index into the buffer? Would it be more efficient to use raw doubles instead of structs? I figured this would be more efficient than an array, but haven't actually tested that.
  2. This problem seems trivially parallelisable. I could just give each thread its own buffer to work on, and then sum the buffers, but I'd rather avoid this expensive step. If multiple threads are doing read-modify-write operations to largely uncorrelated locations in the same ~50mb buffer, are there efficiency concerns here? I'm aware there's a race condition with multi-threaded read-modify-write but for my use case this doesn't matter, even 1 in a 100 updates getting stomped by other threads would not be a problem.

For your second question, you'll probably want to try to partition the space among your threads. If two or more threads are working on the same cache line (64-128 byte chunks on most CPUs) that will be slower than if they're working on separate ones.