What is Clone-on-write (Cow)?

Asked

Viewed 74 times

4

I’m studying Rust and recently discovered the existence of Cow, one smart-Pointer that works to make Clone-on-write.

The description of the page seemed confusing to me, since I don’t have much knowledge on the subject.

  • What is Clone-on-write?
  • What is the purpose of the smart-Pointer Cow rust?

I must point out that the term "clone" comes from the semantics of Ownership Rust. Most other languages that implement this probably refer to this structure as Copy on write.

  • 2

    Here is an explanation: https://answall.com/a/23737. The example is in PHP, but the logic of copy-on-write is the same in all languages.

1 answer

5


The concept is actually quite simple, but the use in Rust is not so much, as almost everything in Rust.

The technique of copying when writing has to do with immutability (plus), then you have an object that is passed by reference to somewhere, so the object is not copied, it just has a pointer to the object. In theory this form allows the function to move the content of this object, but in this case it does not want the object to be modified, it must be immutable, and that is where the technique comes in. If a change is requested (written) the object will be copied to a new object to make the change in it leaving immune the original object.

This way you have a low cost for normal use that will probably not write anything, but also the protection of immutability if a change is made on the object.

The guy string of most languages is the best example of this. Many people do not understand why the change in the text is not reflected when it leaves a function for example. It is because it uses the Copy on Write technique. The semantics of the type is value so the normal would be to copy the object, but it costs expensive on a potentially large object that is a string, at the same time as you can want to touch the text. So he doesn’t charge you until you need it, the guy knows he can’t let the object be changed, he takes care of providing a copy if he tries to change something.

The COW technique is precisely what confuses, the person hopes that if the object allows an alteration he should see the change anywhere where he accesses the object, but he does not realize that there was a copy. Curious because if the person thinks of type by value is not difficult to understand, this guy always makes copies, even if he doesn’t have one write. The COW I combine both.

Originally mutable types never use COW. remembering that Rust’s preference is for immutable types.

Cow

The guy Cow is just a pointer ready to deal with that where you know that semantics is that. It is necessary to abstract whether this reference is property (owned) or borrowed (borrowed), according to the well-known rules by those who understand de Rust. From what I understand this is necessary because it is not known yet whether you will have a new object or not, do not know if the resulting object happens to be yours or is only the loan made when passed.

I find the example of instructive documentation:

use std::borrow::Cow;

fn abs_all(input: &mut Cow<[i32]>) {
    for i in 0..input.len() {
        let v = input[i];
        if v < 0 {
            // Clones into a vector if not already owned.
            input.to_mut()[i] = -v;
        }
    }
}

// No clone occurs because `input` doesn't need to be mutated.
let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]);
abs_all(&mut input);

// Clone occurs because `input` needs to be mutated.
let slice = [-1, 0, 1];
let mut input = Cow::from(&slice[..]);
abs_all(&mut input);

// No clone occurs because `input` is already owned.
let mut input = Cow::from(vec![-1, 0, 1]);
abs_all(&mut input);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.