From: Robert Klemme Date: 2008-05-06T01:20:08+09:00 Subject: Re: String copy-on-write question On 05.05.2008 18:07, ts wrote: > Lars Christensen wrote: > > Well, it's best if you look at rb_str_substr() in string.c > >> str[100..-1] # this costs only a small amount of memory > > ruby just need to adjust the pointer and the length in the new > object > >> str[100..-2] # this costs around 16MiB bytes of memory > > one character is missing from the previous string, if it do the > same thing than previously then it must > * adjust the pointer > * adjust the length > * add \0 at the end > > This mean that fatally it has modified the string, this is why it > duplicate. > >> p $'.size # This is free >> p $`.size # This costs another 8MiB. > > same reason here. Interesting. Do you also happen to know why not an additional field is used that stores the length? Is the reason maybe usage of C library string functions that work on zero terminated strings? Cheers robert