I'm working with a loop with a very high iteration count, and so code within it will be quite performance critical. At some point in the loop I take a previously populated array of doubles and pass it to a function that for our purposes we can consider just a sum function, that being it needs to loop over each element in the array and combine it into a value.
Then later I need to take this first array and pass it again to the same function but with the last element changed. The immediate approach that comes to mind is to copy the first n-1 elements into a new buffer, then set this last element as needed. However, I feel as this is in a large loop this would be inefficient, so I've been thinking of ways to minimize this overhead.
In particular I'm wondering if it would be possible to use mmap
to map the first n-1 elements of our second buffer onto the first n-1 elements of the original buffer, and then change the last, unmapped element as needed. This should be able to work using copy-on-write, so though a second buffer is allocated there isn't actually any data copying happening (obviously as long as the second buffer is only read). I could then be able to pass this second buffer to the same function which will act on it transparently without any knowledge of the mapping, the addresses up to n-1 translating into the first buffer, and the nth address actually being in the second buffer.
I've tried to throw together some code to see if this can be done and work as expected, but my main problem right now is mmap
only maps arrays to file descriptors, rather than arrays to arrays.
My questions then are
- Can I achieve this behavior using
mmap
? - How can I implement this without using file descriptors, and if I have to use them, how best should I do it?
I will mention as well that it would be best if the solution remains completely transparent to the function these buffers are passed to, due to how this code base is intended to be used.