|
|
String copy-on-write question
Hello group,
Ruby implements copy-on-write for strings, so you can do stuff like
this very cheaply:
str = 0.chr * (2**24) # 16MiB allocated
str[100..-1] # this costs only a small amount of memory
How come this optimization does not apply in this case?:
str[100..-2] # this costs around 16MiB bytes of memory
As a side effect, if using regexps on a large string, the pre-match
and post-match variables behave differently:
s = 0.chr * (2**23) + "Hello" + 0.chr * (2**23) # About 16MiB
allocated (after GC)
s.scan(/Hello/) { |m| p m } # This is free
p $'.size # This is free
p $`.size # This costs another 8MiB.
Any insights?
Lars
|
|
0
|
|
|
|
Reply
|
larsch (43)
|
5/5/2008 3:37:02 PM |
|
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.
Guy Decoux
|
|
0
|
|
|
|
Reply
|
decoux (1351)
|
5/5/2008 4:07:09 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
shortcutter (5766)
|
5/5/2008 4:15:38 PM
|
|
Robert Klemme wrote:
> Interesting. Do you also happen to know why not an additional field is
> used that stores the length?
I've not understood : it has a field which give it the length of
the string, for example with
str = '0' * 200
str[100 .. -1]
the first object (in str) will have 200 for its length
the field length in the new object will have the value 100
> Is the reason maybe usage of C library
> string functions that work on zero terminated strings?
only matz know this :-)
Guy Decoux
|
|
0
|
|
|
|
Reply
|
decoux (1351)
|
5/5/2008 4:33:52 PM
|
|
On 05.05.2008 18:33, ts wrote:
> Robert Klemme wrote:
>> Interesting. Do you also happen to know why not an additional field is
>> used that stores the length?
>
> I've not understood : it has a field which give it the length of
> the string, for example with
Ah, ok. This happens when one is too lazy to look into the source. :-)
Somehow I had assumed that the length was not stored because you made
the point that the \0 could not be inserted without altering the
original. I concluded, there is no length. :-)
> str = '0' * 200
> str[100 .. -1]
>
> the first object (in str) will have 200 for its length
> the field length in the new object will have the value 100
>
>> Is the reason maybe usage of C library
>> string functions that work on zero terminated strings?
>
> only matz know this :-)
Well, maybe he'll stop by and enlighten us.
Kind regards
robert
|
|
0
|
|
|
|
Reply
|
shortcutter (5766)
|
5/5/2008 4:46:15 PM
|
|
|
4 Replies
37 Views
(page loaded in 0.332 seconds)
Similiar Articles: Some text processing questions - comp.lang.vhdl... testbench, I came upon a couple of questions regarding text / string ... can come to your rescue: procedure copy_string ... string into a line variable: write(L, string ... How do I copy recordsets from one database table to another ...... Post Question | ... to the data arrays, converting back to a string, and then writing ... Copy String from Sting - comp.soft-sys.matlab How do I ... Copy String from Sting - comp.soft-sys.matlabI couldn't copy part of the string in Matlab. Like I have abc='2346+45' and I want to ... Post Question | Groups ... Loki's AssocVector versus std::map in mt - comp.lang.c++.moderated ...I also have another question. How many test runs of a particular mt test code ... "Copy-on-write" string implementations are generally not thread-safe. If you're using copy ... cannot write std::map via ostream_iterator? - comp.lang.c++ ...... but it must be possible to write > std::pair<const string ... std::copy( ml.begin ... const char ** syntax question - comp.lang.c++.moderated cannot write std::map ... How to copy a file and force overwirte in perl - comp.lang.perl ...I have to write a perl script to copy and overwrite a file, I tried to use ... files then remove them first before you do the copy. ... OVERWRITTEN - aix Rookie question ... converting a character string into a variable name - comp.lang ...I have a FORTRAN question. The problems is: I want to write several variables, arrays and ... END SELECT WRITE(*,*) output_string -- -- jovan Ask the "write" EXIF question and Tiff tags - comp.soft-sys.matlab ...> > > > I could just create a copy of the Tiff, create a new Tiff object t, read the ... Ask the "write" EXIF question and Tiff tags - comp.soft ... > > > > I could just create a ... Iterating over a String - comp.lang.java.help... the for:each syntax would not let me write code of the form: String ... will install the cool cuneiform font and copy ... Index over ... gensub() question - comp.lang.awk ... is there a shortcut to copy the whole buffer to the clipboard ...hello,all is there a shortcut to copy the whole buffer to the clipboard? or how to write ... Post Question ... kill-new (buffer-string))) And bind ... Implement copy-on-write string... | CareerCupcopy on write means, there is an actual copy made , only when there the string is actually altered. For eg, String a="hello"; String b=a; <- both b and a object share ... c++ - How to implement Copy-on-Write? - Stack OverflowHey! I want to implement a copy-on-write on my custom C++ String class, and I wonder how to... I tried to implement some options, but they all turned out very ... 7/25/2012 12:43:49 AM
|
|
|
|
|
|
|
|
|