Does Python use copy-on-write for copies of lists?

3.2k views Asked by At

Suppose I copy an existing list:

existing_list = [ 1, 2, 3 ];
copied_list = existing_list[:]

...

copied_list[2] = 'a' // COW happens here

[Some edits]

I heard that Python uses copy-on-write when either copied_list or existing_list is mutated. Is this true?

Seems to me like an over-complication that requires locking all over the place (think multi-threading).

For clarity: I am not looking for a COW impl. I'm just trying to understand what's Python standard behavior.

4

There are 4 answers

2
Raymond Hettinger On BEST ANSWER

There is no copy-on-write. When you run copied_list = existing_list[:], a new list is built and populated immediately. Here is the source: http://hg.python.org/cpython/file/2.7/Objects/listobject.c#l467

0
Ignacio Vazquez-Abrams On

This isn't "copy-on-write", this is "existing references continue to exist until replaced". Nothing to see here, move along.

0
AudioBubble On

Most Python implementations use locking all over the place anyway, and it ruins multi-threading anyway (the GIL). But still, I don't think copy-on-write is used. It requires further locking and organization, it's a pretty low-level optimization, and the cost of copying will be smaller than usual as everything is a reference (so you just copy N pointers).

You can read the source if you care enough. I didn't dissect all methods that may copy, but a quick search ("copy", "write", "lock") didn't find anything indicating COW or a similar mechanism.

1
Sven Marnach On

No. If you want a list implementation with copy-on-write, try blist.