Do you use D's GC?

jfondren julian.fondren at gmail.com
Tue Aug 3 19:04:06 UTC 2021


On Tuesday, 3 August 2021 at 18:34:55 UTC, Steven Schveighoffer 
wrote:
> On 8/3/21 2:21 PM, jfondren wrote:
>> On Tuesday, 3 August 2021 at 18:18:48 UTC, Steven 
>> Schveighoffer wrote:
>>> It's no different for length setting:
>>>
>>> ```d
>>> buffer = new int[100];
>>> auto buffer2 = buffer;
>>> buffer.length = 50;
>>> buffer.length = 51; // must reallocate
>>> buffer[$-1] = 10;
>>> ```
>> 
>> All of the buffer-stomping "must reallocate" cases are 
>> preserved in the rewrite that only assigns buffer.length when 
>> the new value is larger. The cases that were omitted, that 
>> were a performance drain, were same-length or less-length 
>> assignments.
>> 
>
> There is not a reallocation on shrinking of length. Or am I 
> misunderstanding your statement?

I think you're understanding it but not believing it. Again:

```d
void ensureHasRoom(ref ubyte[] buffer, size_t length) {
   buffer.length = length; // this is a serious bottleneck
}

void ensureHasRoom(ref ubyte[] buffer, size_t length) {
   if (buffer.length < length) {
     buffer.length = length; // this is fine actually
   }
}
```

The story is that the situation was improved by excluding only 
those cases that should not have incurred any reallocations. 
Every single invocation of the bottlenecking `ensureHasRoom` that 
should have reallocated would still do so with the fixed 
`ensureHasRoom`. So something else was going on.


More information about the Digitalmars-d mailing list