socket send O(N**2) complexity

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Sep 23 05:01:51 CEST 2009


On Wed, 23 Sep 2009 00:25:52 +0100, Nobody wrote:

> On Mon, 21 Sep 2009 16:33:08 -0400, Jack Diederich wrote:
> 
>>> AIUI, as a python string is imutable, a slice of a string is a new
>>> string which points (C char *) to the start of the slice data and with
>>> a length that is the length of the slice, about 8 bytes on 32 bit
>>> machine.
>> 
>> Not in CPython.  While some special strings are re-used (empty string,
>> single letters) if you take a slice of an existing string a new buffer
>> is allocated and the slice memcpy'd into it.
> 
> Er, why?
> 
> I can understand doing this for mutable sequences, but it doesn't seem
> to make much sense for strings.

Consider:

huge_string = "abcdef"*1000*1000*1000
tiny_string = huge_string[42:45]
del huge_string

Under the current behaviour, huge_string will be garbage collected. With 
the proposed string-view, it won't be. It would be surprising and 
disturbing if taking a tiny slice of a huge string prohibited the huge 
string from being garbage collected.


-- 
Steven



More information about the Python-list mailing list