liveBook · Manning

Contiguous memory layout

As explained in Fast Python: High performance techniques for large datasets

A contiguous memory layout stores logically adjacent data elements at adjacent memory addresses, without gaps between them.

A contiguous layout places successive data elements at successive memory addresses. For a matrix, this means one dimension is stored contiguously; whether rows or columns are adjacent depends on the layout. section 6.1.2 section 4.1.2

Contiguity affects how efficiently a CPU can reuse fetched data. Reading adjacent elements can let one memory transfer supply several operations, while jumping between distant elements may require repeated transfers. The difference can become pronounced for large matrices, though small ones may fit in cache. section 6.1.2

When working with arrays, account for:

  • Traversal order: access neighboring elements in the dimension stored contiguously when optimizing for cache use. section 6.1.2
  • Layout convention: C-based code commonly stores rows contiguously, while Fortran-based code commonly stores columns contiguously; NumPy’s backend may use either. section 6.1.2
  • Array views: shape and strides describe how an array maps onto memory, and reshaping or transposing can produce views that share memory without implying contiguous access. section 4.1.2
import numpy as np
 
linear = np.arange(10, dtype=np.uint32)

This listing allocates a one-dimensional NumPy array used to illustrate contiguous storage of successive elements. section 4.1.2

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Explains cache reuse through matrix traversal and relates array views to strides, but leaves allocation details for later.
Examples
Compares doubling adjacent matrix elements with doubling elements separated across rows, and demonstrates reshaped and transposed NumPy views.
What you can do after
Understand how data layout and traversal order affect cache reuse when accessing matrices and NumPy arrays.
How it connects
Connects memory layout to CPU caches, NumPy arrays, array views, and strides.

Also explained in

Compare how 2 books explain contiguous memory layout →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.