Journal Articles
Browse in : |
All
> Journals
> CVu
> 155
(10)
All > Topics > Programming (877) Any of these categories - All of these categories |
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.
Title: Copy on Write
Author: Administrator
Date: 03 October 2003 13:16:00 +01:00 or Fri, 03 October 2003 13:16:00 +01:00
Summary:
Body:
What exactly is Copy On Write (COW)? This has been the source of some discussion on accu-general just recently. Copy on Write is a lazy optimisation [Jim Hyslop]. Consider a string-class implemented in such a way that it only stores a pointer to a string of data [Terje Slettebø].
The normal way to make a copy of such an object would be to create a new instance of the object, allocate enough memory to hold the string and then copy the string from the old object into the newly allocated memory pointed to by the new object. This is called a Deep Copy. However, if the string data is particularly long it may be quicker to just copy the value of the pointer from one instance of the string-object to the other and have both instances pointing at the same string data.
Then you have to make sure that if one of the string-objects modifies the string data the other does not get its string data modified as well [Terje Slettebø]. So we need a way of determining whether more than one string points to the same data [Phil Nash]. This is achieved by introducing a system of reference counting. Every time a new copy of the string-object is created the reference count in incremented. The reference count is decremented again each time one of the string-objects is destroyed. When the reference count falls to 0 the string itself is destroyed.
But how does this stop an instance of the string-object modifying the string data pointed to by all the other string-objects? Each time a string-object wants to modify the string data it checks the reference count. If the reference count is 1 it knows it is the only string-object pointing at the string data and it can modify it however it wants. If the reference count is greater than 1 it must perform a deep copy (as described above) and decrement the reference count by 1. This process of not performing a deep copy until the 'data' needs to be modified is termed Copy On Write.
However, for many programs though the average string is approximately six characters long. The extra overhead of COW, including the difficulty of identifying when a write operation is about to take place [Phil Nash], takes more time than just doing the deep copy. Once multi-threading is taken into account it becomes very difficult to make COW work correctly for an object such as std::string and the extra overhead of needing to synchronize (even for distinct strings used in different threads, if they might be copies of the same string) is an extra nail in the coffin [James Dennett].
Notes:
More fields may be available via dynamicdata ..