Issue #
Non-consecutive array access may impact performance.
Actions #
Consider using techniques like loop fusion, loop interchange, loop tiling or changing the data layout to avoid non-consecutive access in hot loops.
Relevance #
Accessing an array in a non-consecutive order is less efficient than accessing consecutive positions because the latter maximises locality of reference.
Code examples #
Consider the example code below to illustrate the presence of non-consecutive access patterns. The elements of array a
are accessed in a non-consecutive manner. In the scope of the outer loop for_i
, all the iterations access the first row of the array. Thus, the code exhibits repeated accesses to all the elements of the first row, a total number of times equal to rows
.
void example(float **a, unsigned rows, unsigned cols) {
for (unsigned i = 0; i < rows; ++i) {
for (unsigned j = 0; j < cols; ++j) {
a[0][j] = 0.f;
}
}
}
Related resources #
References #
- Memory access pattern (non-consecutive array access)
- Locality of reference
- Loop interchange
- Loop tiling