Definition
Deamortization is a technique that gives every operation of a data structure a small worst-case cost when the usual design has only a small amortized cost §14.4: a small average over any sequence of operations, with occasional expensive ones. Instead of doing a large piece of deferred work at once, the structure does a bounded share of it in each following operation, and keeps an invariant, a rule true after every operation, so that results match those of finished work. Incremental array migration is the standard example.
The idea
A dynamic array §14.5 keeps its elements side by side in one block of memory, with spare slots. When the block is full, the next append reserves a block twice as large, copies every element across and then writes the new one. Amortized analysis shows that appends starting from an empty array cost at most element operations in total, so each append costs a constant on average. The average hides one long append: growing an array of elements copies all of them in a single call. If writing one element takes 10 ns, that call takes about ms, and a control loop with a deadline of 1 ms per cycle misses its deadline in that cycle, although the average cost is tiny.
Deamortization pays the same bill in instalments. Once the bigger block is reserved, each following append copies one old element and then writes its own. The copying is spread over exactly the appends that fill the new block, so it is finished before another growth is needed. The total work does not change; only its timing does. The picture of instalments stops at one point: while the copying is in progress, the old elements are split between two blocks, some already copied and some not, and every read and write must know which block holds the current value of each element.
How it works
The design below is for an array that supports appending at the end and reading and writing by index, used by one thread at a time. Its state during a migration is:
the old block, with capacity , which was full when growth started;
the new block, with capacity ;
the length , the number of valid elements, whose indices are 0 to ;
the migration frontier , the number of old elements already copied: old indices below have been copied to the same indices in the new block.
The operations are:
Start of growth. An append that finds the array full, with , reserves the new block and sets .
Append during migration. Copy the old element at index to index of the new block and add 1 to ; then write the appended element at index of the new block and add 1 to .
End of migration. When , every old element has been copied. Release the old block; the new block, of capacity , is now the only block.
Read index . First check that , and reject any other index. Then read the new block if (already copied) or (appended after growth started), and read the old block if (not yet copied).
Write index . Check that and use the same rule. A write to an index not yet copied goes to the old block, so that the later copy carries the new value across.
The copying finishes in time. Growth starts at , and the new block is full at , so exactly appends happen in between. Each copies one of the old elements, so the last old element is copied by the append that fills the new block, and the next growth finds no unfinished copying. Copying two old elements per append would finish halfway, after appends, and leave a margin.
The same pattern appears elsewhere. A hash table §14.7 that grows can move a few entries from its old table to its new one on each operation and search both tables until the move is complete; this is called incremental rehashing. Incremental garbage collection §8.16 reclaims unused memory a little at a time instead of in one long pause.
Worked example
The old block has capacity and holds the elements a, b, c and d at indices 0 to 3. Four appends follow. Count one unit for each element copied or written.
Append
e: the array is full, so reserve a new block of 8 slots and set . Copyato new index 0 (); writeeat new index 4 (). Cost 2.Append
f: copybto new index 1 (); writefat new index 5 (). Cost 2.Append
g: copycto new index 2 (); writegat new index 6 (). Cost 2.Append
h: copydto new index 3 (); writehat new index 7 (). Cost 2. The migration is complete; release the old block.
The new block holds a to h. The four appends cost units. The usual design spends units on append e (four copies and one write) and 1 unit on each of the other three, units in total. The total is the same; the largest single append fell from 5 units to 2.
Reads in the middle. After append f, and . Index 1 is below , so it is read from the new block, which holds the copied b. Index 3 lies between and , so it is read from the old block, which still holds d; slot 3 of the new block has not been written yet and holds no valid element. Index 5 is at least , so it is read from the new block, which holds f. Index 6 is rejected because it is not below , even though the new block has a slot 6.
Writes in the middle. Still at , the program sets index 3 to x. The rule sends the write to the old block, and when append h later copies old index 3, it carries x across. Had the write gone to the new block instead, that copy would have overwritten x with the stale d, and the write would have been lost.
Stated precisely
The guarantees below rest on four assumptions.
The only operations are appends at the end and reads and writes by index; there is no removal and no shrinking.
One operation runs at a time.
Copying or writing one element takes a fixed, bounded time.
Reserving and releasing a block are counted separately from element operations, and a reservation always succeeds.
Invariant. During a migration from an old block of capacity , with length and frontier , two statements hold after every operation. First, . Second, for every index with , the current value of element is in the new block if or , and in the old block if . The first statement holds at the start, where and , and each append raises both and by 1; it implies that reaches exactly when reaches , the moment the new block is full. The second statement holds at the start, when all elements are in the old block. A copy moves index into the new block with its current value before grows; an append adds index to the new block; a write changes only the block that the rule names. So every read returns the most recently written value, as in an ordinary array.
Bounds. Every append costs at most 2 element operations, one copy and one write, plus a constant amount of bookkeeping, and the append that starts a growth also reserves one block. Every read or write costs one element access and a constant number of comparisons. Over any sequence, the total number of element operations equals that of the usual design, because each old element is copied exactly once per growth. During a migration both blocks exist: slots, which is 12 slots for .
Research results go further. Brodnik, Carlsson, Demaine, Munro and Sedgewick (1999) give resizable arrays in which all operations take constant time and the space beyond the stored elements is only , at most a constant times , and show that this extra space is optimal up to a constant factor. Tarjan and Zwick (2022) note that standard doubling gives constant amortized, or even constant worst-case, time per operation with space, and analyze the trade-off between space and time. The two-block design above uses extra space and claims none of the better space bounds.
Limits and common mistakes
Two units per append is not a constant wall-clock §9.36 time. Reserving a block can take a variable time, the allocator may clear the memory first, and the reservation can fail; releasing the old block has a cost too. A worst-case time per append needs bounds for those steps as well, or blocks reserved in advance.
Progress depends on appends. If appends stop in the middle of a migration, both blocks stay in use indefinitely. Reads and writes remain correct, but the extra memory is not returned. Finishing the copying in the background needs another thread and therefore synchronization between threads.
The argument is for one thread at a time. If another thread reads while an append copies an element and changes , the reader can see a value of and a slot that do not belong together, which is a data race §9.3. A concurrent version needs locks, or a lock-free §9.13 design with its own proof.
Elements move. An element’s address changes when it is copied, and the old block is released at the end of the migration. A pointer to an element taken before the copy then becomes a dangling pointer §4.1, one that refers to released memory. Clients must hold indices, not addresses.
Removing elements, shrinking the array or inserting in the middle breaks the counting argument; each needs its own schedule and proof.
Memory stays high for longer. The usual design holds both blocks only during the one append that copies. The deamortized design holds both for the whole migration, which with one copy per append lasts until the new block is full.
Deamortization does not reduce the total work and usually adds a little, because every read and write must test which block to use. It pays off when a bound on each operation matters, as in a hard real-time system §6.34 or a service with a latency limit per request. Otherwise the simpler amortized design is usually faster overall.
Questions answered
What changed between amortization and deamortization here?
What changed is when the copying is done, not how much copying is done. In the amortized design, a dynamic array that doubles its capacity copies all old elements inside the one append that finds it full, so that append costs element operations (5 for ) while the others cost 1, and the guarantee covers only the total over a sequence of appends. In the deamortized design, that append reserves the larger block but copies only one old element, and each following append copies one more. Every append then costs at most 2 element operations, one copy and one write, and the copying ends exactly when the new block becomes full. For the four appends after a growth from , both designs spend 8 units in total, but the largest single append falls from 5 units to 2.
Three things come with the change. A migration frontier , the number of old elements copied so far, must route every read and write: indices below or at least are in the new block, and indices from to are still in the old block. Both blocks, slots in all, stay reserved for the whole migration instead of during one append. And the per-append bound counts element operations only: the time to reserve memory, a failed reservation and access by several threads at once need their own bounds.
Related entries: Amortized analysis (§14.4), Array (§14.5), Asymptotic complexity (§14.2), Heap fragmentation (§4.9), Time-of-check to time-of-use (TOCTOU) (§9.26), Hash table (§14.7), Object lifetime (§4.1). Further reading: [201, 202].