Terminology (6)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Locality
- Data used together sitting close together in memory, so fewer lines are fetched.
- Indirection
- Reaching data through a pointer or reference instead of holding it directly.
- Dependent load
- A load whose address comes from a value loaded earlier, so it cannot start until that load finishes.
- Pointer chasing
- Following a chain of pointers, such as a linked list, one dependent load at a time.
- Cache line
- A block managed/transferred as a unit by a cache. This lesson’s model fixes 64 bytes; actual implementation/cache-level sizes must be established separately.
- Typed array
- A JavaScript array such as Float64Array that stores numbers contiguously in a buffer.
Why layout affects access
Locality describes how data used together is arranged and reused. A cache manages blocks of bytes called cache lines, so nearby values may share a line. Indirection reaches a value through an address or reference, adding another step between a container entry and the data being read.
Two traversals can visit the same values while covering different byte ranges or obtaining their next addresses differently. Contiguous storage lets an index determine an element’s address; a linked traversal obtains the next address from data it has just read. Footprint and address dependencies describe different costs, and neither alone measures elapsed time.
01Layouts
What does each layout allocate?
A marketplace’s nightly payout job totals each store’s sales from the day’s orders. Its decoder hands it an array of pointers to order records; an array of the records themselves would hold the same numbers. Reading every order costs very different amounts: one layout is a single run of memory, the other is a run of addresses that each lead somewhere else.
The native value containers put Order records inline in contiguous element storage. Pointer/owning-box containers keep an extra level of indirection; independently created records have no adjacency guarantee. The TypeScript comparison uses buffer columns because ordinary object-array semantics do not specify a physical record layout.
Each program holds the day’s 1000 orders two ways and, where the language allows, counts the heap allocations.
The object array preserves references/identity without a specified physical object layout. Float64Array exposes consecutive amounts in its backing buffer.
// Two ways for the payout job to hold a day's 1000 orders: an array of order
// objects, and typed arrays with one column per field.
type Order = {
store: number;
cents: number;
};
const orders: Order[] = Array.from({ length: 1000 }, (_, i) => ({
store: i % 40,
cents: 500 + i
}));
const cents = new Float64Array(1000).map((_, i) => 500 + i);
const stores = new Int32Array(1000).map((_, i) => i % 40);
let fromObjects = 0;
for (const order of orders) fromObjects += order.cents;
let fromTyped = 0;
for (let i = 0; i < cents.length; i++) fromTyped += cents[i];
console.log(
`totals ${fromObjects} ${fromTyped}, typed bytes per amount: ${cents.BYTES_PER_ELEMENT}`
);
console.log(`stores kept: ${stores.length}`);
npx tsc main.ts --noEmit --strict && node main.ts
The quoted allocation counts are historical observations from the recorded builds, not new runs or language guarantees. They depend on the concrete container construction, nonzero record size, caller retention and optimizer. The later 64-byte/8-byte-pointer traversal profile is an authored model, not a measurement of this machine’s cache or native pointer width.
02Lines
How many lines does a traversal touch?
Cache hardware transfers/manages data in lines whose size depends on the implementation and cache level. This lesson declares 64-byte aligned lines: nonnegative byte address a is in line floor(a / 64). It does not infer a cache line size from the historical host architecture. A value occupies every line from the one holding its first byte to the one holding its last, so a 16-byte value starting 8 bytes before a line boundary touches two lines.
Counting the union of covered lines gives a footprint for the declared traversal. With an initially empty unlimited ideal cache and one fetch per distinct line, that union also counts ideal first fetches. It does not count real misses, traffic, instructions, prefetching, eviction or timing. An empty range touches no line.
03Pointer chasing
Why does a linked list wait?
Array element addresses can be computed from the base and index without loading the preceding element’s contents. The order table also links each store’s orders into a list: every order points to that store’s next order, and the payout for one store follows the chain. In the declared model, following next introduces an address dependency from the preceding order. Count these edges after the known head; do not turn that count into a processor schedule or elapsed time.
04Practice
Finish the traversal cost model
The lab is the payout job’s traversal cost model. It reads a layout of order records from stdin and prints the distinct 64-byte lines a traversal touches and its dependent loads. It models costs; it measures nothing on the machine. Each level supplies the parts that already work.
The traversal cost model
Count the lines of records stored by value, count the pointer array, then follow one store’s order chain.
In the frontend Sum a visible amount column Optional
A payout table can receive its amount column as a Float64Array. This example sums the requested row range through a subarray view. The view exposes consecutive eight-byte elements and shares its backing storage; it does not copy the amounts. That layout fact does not establish render speed or cache-miss counts. The caller supplies valid row bounds and a new prop when the displayed data changes.
export default function PageTotal({
cents,
from,
to
}: {
cents: Float64Array;
from: number;
to: number;
}) {
const page = cents.subarray(from, to);
const total = page.reduce((sum, value) => sum + value, 0);
return <output>{(total / 100).toFixed(2)} USD</output>;
}
The visible total reads only the selected amount elements. Keeping the view can keep the entire backing buffer reachable; byteLength describes the view, not all retained storage.
Sources: ECMAScript: TypedArray subarray shares the backing buffer.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?