Itay Maman
Itay Maman

Reputation: 30733

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

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.

Upvotes: 5

Views: 3344

Answers (4)

Raymond Hettinger
Raymond Hettinger

Reputation: 226376

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

Upvotes: 9

user395760
user395760

Reputation:

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.

Upvotes: 2

Sven Marnach
Sven Marnach

Reputation: 601789

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

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798746

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

Upvotes: 2

Related Questions