Copy-on-write
Copy-on-write (COW), sometimes referred to as implicit sharing[1] or shadowing,[2] is a resource-management technique used in computer programming to efficiently implement a "duplicate" or "copy" operation on modifiable resources[3] (most commonly memory pages, storage sectors, files, and data structures).
In virtual memory management
Copy-on-write finds its main use in
Copy-on-write can be implemented efficiently using the
The copy-on-write technique can be extended to support efficient
Copy-on-write pages are also used in the Linux kernel's same-page merging feature.[4]
In software
This section needs expansion. You can help by adding to it. (October 2017) |
COW is also used in
Examples
The
std::string x("Hello");
std::string y = x; // x and y use the same buffer.
y += ", World!"; // Now y uses a different buffer; x still uses the same old buffer.
In the PHP programming language, all types except references are implemented as copy-on-write. For example, strings and arrays are passed by reference, but when modified, they are duplicated if they have non-zero reference counts. This allows them to act as value types without the performance problems of copying on assignment or making them immutable.[7]
In the Qt framework, many types are copy-on-write ("implicitly shared" in Qt's terms). Qt uses atomic compare-and-swap operations to increment or decrement the internal reference counter. Since the copies are cheap, Qt types can often be safely used by multiple threads without the need of locking mechanisms such as mutexes. The benefits of COW are thus valid in both single- and multithreaded systems.[8]
In computer storage
COW may also be used as the underlying mechanism for
See also
- Allocate-on-flush
- Dirty COW – a computer security vulnerability for the Linux kernel
- Flyweight pattern
- Memory management
- Persistent data structure
- ReFS
- Wear leveling
References
- ^ "Implicit Sharing". Qt Project. Retrieved 10 November 2023.
- S2CID 207166167. Archived from the original(PDF) on 2 January 2017. Retrieved 10 November 2023.
- ^ ISBN 9780596002138. Retrieved 10 November 2023.
- ^ Abbas, Ali. "The Kernel Samepage Merging Process". alouche.net. Archived from the original on 8 August 2016. Retrieved 10 November 2023.
{{cite web}}
: CS1 maint: unfit URL (link) - ISBN 9780132979184.
- ^ "Concurrency Modifications to Basic String". Open Standards. Retrieved 10 November 2023.
- ^ Pauli, Julien; Ferrara, Anthony; Popov, Nikita (2013). "Memory management". PhpInternalsBook.com. Retrieved 10 November 2023.
- ^ "Threads and Implicitly Shared Classes". Qt Project. Retrieved 10 November 2023.
- ^ Kasampalis, Sakis (2010). "Copy-on-Write Based File Systems Performance Analysis and Implementation" (PDF). p. 19. Retrieved 10 November 2023.
- ^ Chien, Tim. "Snapshots Are NOT Backups". Oracle.com. Oracle. Retrieved 10 November 2023.