Search⌘ K
AI Features

Shrinking the Data to Iterate Through by Using Parallel Arrays

Explore how parallel arrays can reduce data size and speed up iteration in C++. Understand the memory layout changes, performance improvements, and the trade-offs including code complexity and data synchronization risks when splitting data into multiple arrays.

Iterating using parallel arrays

Next, we are going to try a more aggressive way of shrinking the amount of data we need to iterate through by using parallel arrays. First, a warning: this is an optimization that, in many cases, has too many drawbacks to be a viable alternative. Don't take this as a general technique and apply it without thinking twice. We will come back to the pros and cons of parallel arrays after looking at a few examples.

By using parallel arrays, we simply split the large structures into smaller types, similar to what we did with the authentication information for our User class. But instead of using pointers to relate objects, we store the smaller structures in separate arrays of equal size. The smaller objects in the different arrays that share the same index form the complete original object.

An example will clarify this technique. The User class we have worked with consists of 40 bytes. It now only contains a username string, a pointer to the authentication information, an integer for the ...