
# Chunks and lazy loading

A layer is cut into **chunks of 256 KiB**, each compressed with zstd and named by the SHA-256 of
its content. The guest reads the layer through a loop device from a file the host serves; each
read is answered from the chunks it touches, which are loaded the first time.

## Where a chunk comes from

1. the **cache** of decompressed chunks (16 MiB by default, shared by the images of a process);
2. the **store** of compressed chunks: `dirStore()` on Node (`~/.cache/untainer/chunks`),
   OPFS or IndexedDB in a browser (`browserStore()`), or any object with `get`, `put` and
   `has`;
3. the image's **source**: its directory, or HTTP.

Every chunk is decompressed and checked against its hash before any byte of it is used: a damaged
chunk is `ERR_CHUNK_HASH`, never data, and a damaged copy in a store is fetched again. A second
start of an image fetches only its `manifest.json`.

## A start, measured

A first visit in Chrome, from a server on the same machine, with the image laid out by its start:

| Image | Ready | Requests | Downloaded |
| --- | --- | --- | --- |
| node:24-alpine | 0.8-0.9 s | 13 | 33.9 MB |
| postgres:17-alpine (initdb included) | 5.9 s | 12 | 29.2 MB |

"Downloaded" is everything the page fetched: the image's part and untainer's own files, the
kernel among them. Of node:24-alpine's 33.9 MB, 19.2 MB is the image (its boot bundle), out of
60.3 MB of chunks. The server is local, so the times are the guest's work, not a network's.

The boot bundle is what keeps the requests few: the chunks the start reads come in a few large
files instead of one request each. What the container reads later is fetched chunk by chunk.

Measured by [`spikes/21-stores/browser.mjs`](https://github.com/OskarLebuda/untainer/blob/main/spikes/21-stores/browser.mjs); the runs are in
[`docs/findings.md`](https://github.com/OskarLebuda/untainer/blob/main/docs/findings.md), "Cold start in Chrome".

## Decoding

Decompressing and hashing happen with `node:zlib` on Node, and in a dedicated worker in a
browser, so the page's thread never does it.
