I was thinking about writing a struct say
MyVec<T,U> which implements all the interfaces of
Vec<(T,U)> (except for size changing things like
Pop() which I don't care about at the moment) but internally stores the data not as an array of
(T,U) tuples, but with two arrays, on array of
T and a second array of
There might be efficiency gains of this approach in some circumstances, for example, if one wanted to use SIMD instructions.
My thoughts of a implementation for a two tuple was to allocate a region of memory = size(T) * N + size(U) * N, adding some padding if required to align U, where N is the requested vector size.
Then have a struct which contains the following
- A pointer to a region of memory allocated above
- A pointer to the start ot the
Uarray in that region
N, the length of the vector.
- (the capacity if I want to grow the vector, but I'm not worried about that at the moment).
My concern is how I could implement the
Index operations. To return the proper result, I think I'd need to construct the result from the parts of the two arrays and return that. And in the case of a
IndexMut, if that returned reference is modified, I'll need to write it back to the array, but I'm not sure how. Perhaps it could be done with a special tuple type that writes back to the array on drop but I'm not sure if I'd be able to make the lifetimes work for this.
So I'm basically just asking for ideas on this, or whether it has be done (and whether there's a better approach) or whether it can't be done.