Pop quiz [memory usage]

Steve Schveighoffer schveiguy at yahoo.com
Sun Jun 7 04:47:52 PDT 2009


On Sat, 06 Jun 2009 21:59:39 -0700, Sean Kelly wrote:

> Steve Schveighoffer wrote:
>> On Sat, 06 Jun 2009 12:03:03 -0700, Sean Kelly wrote:
>> 
>>> bearophile wrote:
>>>> Sean Kelly:
>>>>> Particularly in D2 where append
>>>>> operations on arrays are probably less common as a result of string
>>>>> being invariant.
>>>> They aren't much common maybe because they are currently dead-slow.
>>>> Appending to an immutable string is a common operation. But I guess
>>>> Array appenders will get more common...
>>> Yes but appending to an immutable string is never performed in place,
>>> which is the only time the extra space reserved by newCapacity
>>> matters.
>>>   I suspect the memory wasted by newCapacity is more of an issue than
>>> any time savings it provides.
>> 
>> What gave you that idea?
>> 
>> void main()
>> {
>>   auto str1 = "hello".idup;
>>   auto str2 = str1;
>>   str1 ~= "world";
>>   assert(str1.ptr == str2.ptr);
>> }
> 
> auto str1 = "hello".idup;
> auto str2 = str3 = str1;
> str2 ~= " world";
> str3 ~= " garbage";
> 
> Doesn't seem terribly safe to me.

Oh, I know.  It's a long-standing issue with immutability, but I think if 
appending gets fixed as Andrei suggests, this should be fixed as well.  I 
was just saying that your statement about immutable data never being 
appended in-place was false.

-Steve



More information about the Digitalmars-d mailing list