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

4

5

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.

Hebephrenia answered 2/11, 2011 at 13:40 Comment(0)
A
9

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

Anisotropic answered 2/11, 2011 at 15:4 Comment(2)
i wonder why, ppl keep saying that slicing a list give you reference to the same list. like a window.Same
Likely the reason is that slicing in some other languages does share the underlying data, so people may be assuming the same is true for Python but they haven't actually checked. Or it could be wishful thinking.Anisotropic
W
2

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

Wham answered 2/11, 2011 at 13:42 Comment(0)
C
2

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.

Calceolaria answered 2/11, 2011 at 13:44 Comment(0)
F
2

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

Festus answered 2/11, 2011 at 13:45 Comment(1)
Are you sure blist is copy on write? Its docs don't mention that. At first glance, it looks like a simple binary search tree to allow faster insertions while preserving the sorted property.Diet

© 2022 - 2024 — McMap. All rights reserved.