Copy-on-write: Difference between revisions

Content deleted Content added
Update storage description to make clear entire file is not copied when modified.
Tags: Visual edit Mobile edit Mobile web edit
 
(10 intermediate revisions by 8 users not shown)
Line 2:
{{Use dmy dates|date=November 2023}}
 
'''Copy-on-write''' ('''COW'''), sometimesalso referred to ascalled '''implicit sharing'''<ref>{{cite web |title=Implicit Sharing |url=https://doc.qt.io/qt-5/implicit-sharing.html |website=Qt Project |access-date=10 November 2023 |archive-date=8 February 2024 |archive-url=https://web.archive.org/web/20240208175543/https://doc.qt.io/qt-5/implicit-sharing.html |url-status=live }}</ref> or '''shadowing''',<ref>{{cite journal |last=Rodeh |first=Ohad |title=B-Trees, Shadowing, and Clones |journal=ACM Transactions on Storage |volume=3 |issue=4 |date=1 February 2008 |page=1 |citeseerx=10.1.1.161.6863 |s2cid=207166167 |doi=10.1145/1326542.1326544 |url=http://liw.fi/larch/ohad-btrees-shadowing-clones.pdf |archive-url=https://web.archive.org/web/20170102212904/http://liw.fi/larch/ohad-btrees-shadowing-clones.pdf |archive-date=2 January 2017 |access-date=10 November 2023 }}</ref> is a [[Resource management (computing)|resource-management]] technique used in [[computer programming]] to efficiently implement a "duplicate" or "copy" operation on modifiable [[System resource#General resources|resources]]<ref name="Linux">{{cite book |last1=Bovet |first1=Daniel Pierre |last2=Cesati |first2=Marco |date=1 January 2002 |title=Understanding the Linux Kernel |url=https://books.google.com/books?id=9yIEji1UheIC&q=%22copy%20on%20write%22&pg=PA295 |publisher=O'Reilly Media |isbn=9780596002138 |page=295 |access-date=10 November 2023 |archive-date=15 September 2024 |archive-url=https://web.archive.org/web/20240915132745/https://books.google.com/books?id=9yIEji1UheIC&q=%22copy%20on%20write%22&pg=PA295#v=snippet&q=%22copy%20on%20write%22&f=false |url-status=live }}</ref> (mostused commonlyin memory[[computer programming|programming]] to manage shared data efficiently. Instead of copying data right away when multiple programs use pagesit, storagethe same data is shared between programs until one tries to modify it. If no changes are sectorsmade, no private copy is created, saving [[System resource#General resources|resources]].<ref name="Linux" /> A copy is only made when needed, ensuring each program has its own version when modifications occur. This technique is commonly applied to memory, files, and data structures).
 
==In virtual memory management==
Line 19:
 
===Examples===
The [[String (C++)|string]] class provided by the [[C++ standard library]] was specifically designed to allow copy-on-write implementations in the initial C++98 standard,<ref name="meyers">{{cite book |first=Scott |last=Meyers |author-link=Scott Meyers |date=2012 |title=Effective STL |publisher=Addison-Wesley |pages=64–65 |isbn=9780132979184 }}</ref> but not in the newer C++11 standard:<ref>{{cite web |title=Concurrency Modifications to Basic String |url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html |website=Open Standards |access-date=10 November 2023 |archive-date=10 November 2023 |archive-url=https://web.archive.org/web/20231110024434/https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html |url-status=live }}</ref>
<syntaxhighlight lang="cpp">
std::string x("Hello");
Line 28:
</syntaxhighlight>
 
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.<ref>{{cite web |last1=Pauli |first1=Julien |last2=Ferrara |first2=Anthony |last3=Popov |first3=Nikita |title=Memory management |date=2013 |url=https://www.phpinternalsbook.com/php5/zvals/memory_management.html#reference-counting-and-copy-on-write |website=PhpInternalsBook.com |access-date=10 November 2023 |archive-date=10 November 2023 |archive-url=https://web.archive.org/web/20231110024435/https://www.phpinternalsbook.com/php5/zvals/memory_management.html#reference-counting-and-copy-on-write |url-status=live }}</ref>
 
In the [[Qt (software)|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 [[Multithreading (computer architecture)|multiple threads]] without the need of [[Lock (computer science)|locking mechanisms]] such as [[Mutual exclusion|mutexes]]. The benefits of COW are thus valid in both single- and multithreaded systems.<ref>{{cite web |title=Threads and Implicitly Shared Classes |website=Qt Project |url=https://doc.qt.io/qt-5/threads-modules.html#threads-and-implicitly-shared-classes |access-date=10 November 2023 |archive-date=3 December 2023 |archive-url=https://web.archive.org/web/20231203002914/https://doc.qt.io/QT-5/threads-modules.html#threads-and-implicitly-shared-classes |url-status=live }}</ref>
 
==In computer storage==
COW may also beis used as the underlying mechanism for [[Snapshot (computer storage)|snapshots]], such as those provided by [[logical volume management]],in file systems such as [[Btrfs]],like [[ZFS]], [[ReFS]] and [[BcachefsBtrfs]],<ref>{{cite web |last=Kasampalis |first=Sakis |date=2010 |title=Copy-on-Write Based File Systems Performance Analysis and Implementation |page=19 |url=https://sakisk.me/files/copy-on-write-based-file-systems.pdf |access-date=10 November 2023 |archive-date=5 May 2024 |archive-url=https://web.archive.org/web/20240505220510/https://sakisk.me/files/copy-on-write-based-file-systems.pdf |url-status=live }}</ref> and database servers such as [[Microsoft SQL Server#Replication Services|Microsoft SQL ServerReFS]]. Typically, the snapshots store only the modified data, and are stored close to the original[[Bcachefs]], soas theywell areas only a weak form ofin [[incrementallogical backupvolume management]] and cannotdatabase substituteservers forsuch aas [[fullMicrosoft backup]].<ref>{{citeSQL webServer#Replication Services|last=ChienMicrosoft |first=TimSQL |title=Snapshots Are NOT Backups |url=https://wwwServer]].oracle.com/database/technologies/rman-fra-snapshot.html |website=Oracle.com |publisher=Oracle |access-date=10 November 2023 }}</ref>
 
In traditional file systems, modifying a file overwrites the original data blocks in place. In a copy-on-write (COW) file system, the original blocks remain unchanged. When part of a file is modified, only the affected blocks are written to new locations, and metadata is updated to point to them, preserving the original version until it’s no longer needed. This approach enables features like [[Snapshot (computer storage)|snapshots]], which capture the state of a file at a specific time without consuming much additional space. Snapshots typically store only the modified data and are kept close to the original. However, they are considered a weak form of [[incremental backup]] and cannot replace a full backup.<ref>{{cite web |last=Chien |first=Tim |title=Snapshots Are NOT Backups |url=https://www.oracle.com/database/technologies/rman-fra-snapshot.html |website=Oracle.com |publisher=Oracle |access-date=10 November 2023 |archive-date=10 November 2023 |archive-url=https://web.archive.org/web/20231110024434/https://www.oracle.com/database/technologies/rman-fra-snapshot.html |url-status=live }}</ref>
 
==See also==
Line 45 ⟶ 47:
==References==
{{Reflist}}
 
==External links==
* {{cite web |title=A history of copy-on-write memory management |url=https://obvious.services.net/2011/01/history-of-copy-on-write-memory.html |website=A keen grasp of the obvious |access-date=18 November 2024 |date=21 January 2011}}
 
{{File systems}}