Search code examples
c++arrayscachingvector

Dealing with subvectors in costant time


I'm implementing an algorithm and I need to be really efficient. My algorithm works on a set of equal and constant size vectors of bools, and I would like to keep them consecutive in memory to avoid cache misses. Since it's used a lot of time, my solution at the moment is to concatenate all the vectors in the set in a unique bigger vector, before running the algorithm. This should make all the vectors consecutive in the memory, but give me another problem: I can't access anymore a single vector that is in the set. Of course, I can use

vector<bool>(concatenation.begin()+i, concatenation.end()+i+vector_size)

but this would make a copy of the data, vanishing my original objective of optimizing performance. Ideally, I would like to have a function that returns me a const vector<vector_type>& of the subvector, since I only need to read the content and not modify it during the algorithm execution.

I'm open also to completely different solutions to do it, but if it is possible I would like to avoid using the iterators.

I've also to say that the size of the vectors of bools is known at compile-time, so maybe an array would work, but I've read that they shouldn't be used because of they are a wrapper to unsafe C arrays.


Solution

  • I only need to read the content and not modify it during the algorithm execution

    Well first off you can't use vector<bool> like this because it acts as a bit field so you can't "extract" sequences of it. Use vector<char> instead.

    That said, you can easily get a pointer to your data, like:

    char *array = concatenation.data() + i;
    // use it as a normal array, for example: array[0]
    

    I've read that [std::array] shouldn't be used because of they are a wrapper to unsafe C arrays

    What do you think std::vector is? It's all arrays, all the way down!