Definition
Storage scanning is the work a log-structured store does to find a record by reading its stored entries newest first; a log-structured store appends every new version of a record instead of overwriting the old one. Garbage collection §8.16 is the work of reclaiming space in such a store: copying the entries still needed out of an old sector of flash memory §6.6, then erasing that sector. Amortized analysis §14.4 spreads the total work of a sequence over its operations; it bounds the average, not any single operation.
Where it fits
Main entry: Amortized analysis (§14.4).
The amortized analysis entry gives the general method; this entry applies it to save latency in a store on flash memory, where two sources of work give two shapes on a plot of latency against save number. Programming flash can only change bits from 1 to 0. Returning bits to 1 needs an erase of a whole sector, the unit of erasing (some parts call it a page), and erasing takes far longer than reading or programming. A store on flash therefore writes each new value to fresh space and leaves the old value in place as a stale entry.
Scanning causes a rise. Each record is named by an identifier, a number or key. Before a save, a store may look up the current entry for the same identifier, for example to skip a save whose value has not changed. Without a cache, the lookup scans entries from the newest backwards, and every save adds an entry, so save latency climbs as the store fills. A lookup cache, a table in RAM that records for each identifier the flash address of its newest entry, makes a lookup one table read however many entries the store holds. The NVS (non-volatile storage) subsystem of the Zephyr real-time operating system §6.33 offers a lookup cache as the build option CONFIG_NVS_LOOKUP_CACHE.
Reclamation causes a pause. When the sector being written is full, the store must free the oldest sector. It copies that sector’s live entries, those that no newer entry has replaced, into a fresh sector and then erases the old one. This garbage collection runs inside whichever save found the sector full, so that one save is far slower than its neighbors. The lookup cache changes how an entry is found, not how many live entries must be copied or how long an erase takes, so the pause remains.
Invented numbers, in units of work: each save writes its entry for 2 units. Without the cache, save number since the last reclamation also scans the entries written before it, at 0.5 units each. Every 8th save also triggers reclamation, which costs 40 units. Without the cache, saves 1 to 7 cost 2, 2.5, 3, 3.5, 4, 4.5 and 5 units, a steady rise, and save 8 costs units. With the cache, saves 1 to 7 cost 2 units each and save 8 costs units: the rise is gone, and the pause is not.
Amortized over one cycle of 8 saves, the cache lowers the cost per save from units to units. That amortized cost of 7 units sits beside a single save of 42 units: amortized analysis guarantees the total, 56 units for every 8 saves, and says nothing about the slowest save.
To attribute a pause to reclamation, record when each reclamation starts and ends and check that the slow saves coincide with it. To attribute a flattened rise to the cache, run the same sequence of saves with and without the cache, starting from the same storage contents.
Limits and common mistakes
Work units are not milliseconds; erase and programming times depend on the flash part. A measurement shorter than one reclamation cycle never shows the pause: in this model, saves 1 to 7 average units without the cache and 2 units with it.
An amortized cost is not a deadline bound. A task with a deadline needs the slowest single save, which the worst-case execution time §9.33 entry treats.
A cache held in RAM is empty after a reset, so the store must rebuild it, typically with one full scan at start-up. The cache moves the scanning to start-up and costs RAM.
A periodic spike on a plot is a clue, not proof: a timer or a radio event can produce the same shape.
Questions answered
Why can a cache flatten the early rise but not remove a later pause?
A cache can flatten the early rise but not remove a later pause because the two come from different kinds of work, and the cache removes only one of them. In a log-structured store, which appends every new version of a record instead of overwriting the old one, the early rise in save latency comes from scanning: without a cache, each lookup reads the stored entries newest first, and every save adds an entry. A lookup cache, a table in RAM that records where each identifier’s newest entry lies, turns the lookup into one table read, so the rise flattens.
The later pause comes from garbage collection: when the sector being written is full, the store copies the entries still in use out of the oldest sector of flash memory, the unit that can only be erased as a whole, and erases that sector, all inside one save. The cache reduces neither the copying nor the erase. In an invented model where every save costs 2 units of work and every 8th save adds 40 units of reclamation, the amortized cost is units per save, yet the 8th save still costs 42 units.
Related entries: Memoization and caching (§14.20), Garbage collection (§8.16), Worst-case execution time (WCET) (§9.33), NVS record (§8.15), Flash memory (§6.6).