All field notes

The bytes are only the beginning.

A buffer stores bytes. To read something meaningful from it, you also need a description of how those bytes should be interpreted.

The distinction is simple, but it shows up across file formats, networking, graphics, and ordinary application data.

Storage and interpretation are separate

Consider eight bytes. They might represent eight small unsigned values, four larger integers, or part of a structured record. Knowing the total size does not tell you which interpretation is correct.

JavaScript makes that separation visible. An ArrayBuffer holds storage, and a typed-array view interprets a region of it as values of a particular type. Multiple views can refer to the same storage. The MDN typed-array guide explains this relationship.

const storage = new ArrayBuffer(8);
const bytes = new Uint8Array(storage);
 
bytes[0] = 12;
bytes[1] = 34;
 
// The view refers to storage; it is not a separate copy.
console.log(bytes.byteLength); // 8

The useful questions are about the contract around the data: element size, ordering, bounds, and lifetime.

A layout is a set of promises

A row of values might look contiguous in a drawing, while the actual format includes padding between rows. A multibyte field also needs a byte-order convention.

These details belong in the representation’s definition. Treating them as incidental makes it easy for two parts of a program to read the same bytes differently.

QuestionWhat it clarifies
What is one element?How many bytes to interpret together
Where does the next element start?Stride and padding
Who owns the storage?Responsibility for its lifetime
Who may modify it?Rules for access and coordination

Sharing adds another contract

Representation describes how to interpret data. Concurrency adds questions about who may access it and when.

In Rust, Send concerns transferring ownership across threads, while Sync concerns sharing references across threads. They describe different permissions; neither says that an algorithm will be fast or free of higher-level coordination problems. The Rust Book’s discussion of Send and Sync is a useful reference.

Before choosing an abstraction, it helps to write down three things: what the bytes mean, who owns them, and how access is coordinated. Those questions often expose a misunderstanding before it becomes a debugging session.

Continue readingConcurrency needs a budget.