Question
Why prepare a candidate runtime object before committing persistent state §8.1?
Short answer
A candidate runtime object is prepared before the commit so that every step that can fail runs while a failure is still harmless. Before the commit, a failed preparation leaves the old version in force, and the device discards the candidate. After the commit, persistent storage names the new version; if building its runtime object fails then, for example for lack of memory, the device is committed to a version it cannot run. Preparing first moves that failure to the harmless side, at the price of memory for both versions.
Terms in the question
Candidate: the new version being installed, for example a new program for an interpreter §2.13 together with its settings.
Runtime object: the form of a version that the running system uses, kept in RAM. It is built from the stored bytes and consists of the program after it has been decoded and checked, the tables and buffers it needs, and any resources it holds, such as timers.
Persistent state: data that survives a restart. Here it is the stored versions and the record that says which version is current.
Committing: the single write to persistent storage after which every restart uses the candidate.
Preparing: doing all the work that can fail (decoding, checking, allocating memory, initializing) and producing a complete runtime object that nothing uses yet.
Activation: the moment the running system starts to use the prepared object, ideally by storing one pointer.
Full answer
Step 1: list the steps and mark which can fail.
Staging, storing the candidate’s bytes: the storage can be full, or a write can fail.
Preparation: the format can be invalid, a check can reject the program, a resource such as a timer can be unavailable, and an allocation can fail. An allocation fails when too little memory is free, or when the free memory is split into pieces that are each too small, which is called heap fragmentation §4.9.
Commit: one write. When the record that names the current version is kept in two alternating locations, a failed or interrupted commit leaves the old record in force.
Activation: one pointer store, which cannot fail.
Step 2: the cost of a failure depends on its side of the commit. Call the old version A and the candidate B. Before the commit, the stored record and the running system both still say A, and a failure costs only freeing the partly built object of B. After the commit, storage says B, and a failure leaves three bad options.
Keep running A from memory. The running system and storage now disagree. The next restart switches to B and may fail in the same way. Any state changed at the commit, such as a raised anti-rollback §8.26 floor (the lowest version the device will accept) or a replay counter, may already reject A.
Restart, to build B in fresh memory. If the build fails again, the device restarts in a loop unless a fallback exists.
Write the commit back to A. That is a second commit. It can itself fail or be cut by a power loss, and it needs a crash argument of its own.
Step 3: order the steps so that every failure comes first. The safe order is stage, prepare, commit, activate, and finally reclaim the memory of A. The same rule underlies two-phase commit §8.8, in which every participant first secures everything it needs to finish and only then is the decision recorded. It also underlies the copy-and-swap idiom of C++, in which all work that can fail is done on a copy and the result is installed by an operation that cannot fail. After a successful preparation, the commit is the last step that can fail, and its failure is harmless.
Step 4: meet the four conditions the order depends on.
Memory for both versions. The object of A stays in use while the object of B is built, so the peak memory §11.37 is the object of A plus the object of B plus any temporary memory that preparation uses. If that peak may not fit, the device must find out before the commit, by reserving the space when it starts or by refusing candidates that are too large.
The same bytes. The object must be prepared from exactly the bytes that the commit names. The device compares the digest (the output of a cryptographic hash function §5.1) of the bytes it prepared from with the digest recorded in the commit; otherwise it may prepare one version and commit another.
A retained fallback. A restart discards the prepared object, which lived in RAM, so the start-up code must prepare B again from storage. That can still fail, for example because memory is laid out differently at start-up. The stored bytes of A must therefore stay intact until B has started successfully.
Infallible activation and safe reclamation. Switching readers to B must be one store that cannot fail, and the memory of A may be freed only after every reader that may still use A has finished.
Worked example
Take a heap of 90 KiB; a heap is the memory area from which a program allocates blocks while it runs. The allocator gives each request the lowest free address range that fits; ignore its own bookkeeping bytes. The object of A uses the range from 0 to 40 KiB. A long-lived 4 KiB block that belongs to another part of the system lies from 40 to 44 KiB. The free memory is the single range from 44 to 90 KiB, which is KiB. Candidate B needs one block of 50 KiB for its object, and its preparation needs a further 10 KiB of temporary buffer, which it frees at the end.
Prepare first. The allocation of 50 KiB fails, because the largest free range is 46 KiB. Nothing persistent has changed: A keeps running, and the device reports that B is too large. Even without fragmentation, the peak would be KiB, more than the 90 KiB heap. The largest object this heap can prepare beside A, with the 10 KiB buffer, is KiB.
Commit first. The device commits B, then frees the object of A to make room. The free memory is now two ranges, 0 to 40 KiB and 44 to 90 KiB, together KiB. That total exceeds the KiB that B needs, yet no single range holds 50 KiB: the larger range is 46 KiB. The allocation fails. Storage names B, the object of B cannot be built, and the object of A is gone. Both orders meet the same allocation failure; only preparing first meets it while A is still in force.
Prepare first, in a heap of 128 KiB. With the same layout, the free range runs from 44 to 128 KiB, which is KiB. The object of B takes 44 to 94 KiB and the buffer 94 to 104 KiB, a peak of KiB in use, within 128 KiB. Preparation frees the buffer, leaving KiB in use. The device commits B, activates it with one pointer store, and frees the object of A once every reader of A has finished, leaving KiB in use.
Follow-up questions and limits
Does preparing first make the update unable to fail? No. It moves the failures of preparation to before the commit. The commit write can still fail, which is harmless because the stored record still names A. The preparation at the next start can still fail, which is why the bytes of A are kept. Activation must be designed so that it cannot fail.
What if memory cannot hold both versions? Then the design must choose explicitly. It can refuse every candidate above a size computed in advance (36 KiB in the example). It can reserve, when the device starts, a second region big enough for the largest candidate it will accept. Or it can stop A before preparing B, accept an interruption of service, and keep a fallback that needs no allocation at all, so that a failed preparation still ends in a defined state.
Why not simply retry the allocation after the commit? Until a retry succeeds, the device is committed to a version it is not running, for an unknown time. Fragmentation often persists: in the example, the 4 KiB block stays where it is for as long as its owner keeps it.
This answer does not cover how readers switch from A to B without mixing the two versions, or when the memory of A may be freed; the question on publication and reclamation §17.14 treats both.
Fullest treatment: Staging versus commit versus activation (§8.22).
Related entries: Peak versus retained memory (§11.37), Ownership (§4.2), Immutable publication (§9.24), Recovery fallback (§8.29), Prepare before irreversible commit and allocation-free fallback (§19.3.8), Heap fragmentation (§4.9), Two-phase commit (§8.8).