BigInt -- a challenge for std.allocator

Dmitry Olshansky dmitry.olsh at gmail.com
Tue Oct 29 07:58:46 PDT 2013


29-Oct-2013 16:22, Don пишет:
> On Tuesday, 29 October 2013 at 11:26:45 UTC, Froglegs wrote:
>>
>>>    BigInt n = 100;
>>>    n += 10;
>>>    ++n;
>>>
>>> ... and both the 2nd and 3rd lines will result in a reallocation even
>>> though there is no need for it, because only n is referencing the
>>> memory it wraps.
>>
>>
>>  Does BigInt not have overloaded operators for adding normal integers?
>> Is it converting 10 to a BigInt prior to adding it to n?
>
> Yes, it has overloaded operators, but that's not relevant. The issue is
> that
> initially n points to an block of memory, containing [100].
>
> Then, if you use COW, n+= 10 isn't in-place. It has to create a new
> array, containing [110].
> Then ++n creates a third array. [111].
>

Can't it use ref-counted COW? Then since there is only 1 reference to 
the original block it may mutate it in-place else it creates new chunk 
with refs=1 and decrements the original count. Maybe I'm missing 
something but it shouldn't be that hard.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list