Copy-on-write
In short: Copy-on-write means: a copy is only promised at first, not made. Only when someone changes the data is it really copied.
In more detail: Several users share the same data as long as all of them only read. When one writes, it gets a private copy of the affected part. This saves time and memory.
In Depth
- Processes: After
fork()parent and child share the same memory pages. The page is copied only on write. - File systems: Btrfs, ZFS and APFS write changes to new blocks, enabling fast snapshots.
- Programming languages: Some strings and arrays (for example in Swift) copy only on change.
- Immutable data: The principle fits persistent data structures well.
- Cost: The first write is slower and triggers a page fault.
See also: Virtual memory and paging