The D standard library is built on GC, is that a negative or positive?

Hipreme msnmancini at hotmail.com
Thu Dec 15 13:37:37 UTC 2022


On Thursday, 15 December 2022 at 11:21:56 UTC, IGotD- wrote:
> On Thursday, 15 December 2022 at 08:42:22 UTC, ikod wrote:
>>
>> Probably this is common case when your program works in tight 
>> loop - like game, or webserver under high load. Process just 
>> have no spare time to handle GC cleanups (for stop-the-world 
>> collectors).
>
> One interesting observation here in the forum and also in 
> articles, blogs etc about the D garbage collector about how you 
> can speed up memory management. All those ideas and tricks are 
> based on that you in some way should avoid or circumvent the 
> garbage collector. Then the question is, how good is a computer 
> algorithm if you are supposed to avoid it all the time?
>
> Also, it doesn't matter if "the GC stop world" duration are 
> very short. It takes time for the OS scheduler to stop all 
> thread which probably is a significant part of the GC pause. 
> Then depending on the load the is also a duration until the 
> thread is started again.

Read the following algorithm:

```d
auto myArray = [1, 9, 5, 20, 30];
foreach(value; myArray)
{
     myArray.sort();
     writeln(value);
}
```

This is such misuse. Array sorting is a real heavy task that you 
must avoid all the time, and when you do it, you will want to 
cache its result. It is the same thing that happens with GC.

I use GC carelessly on initialization, but after I'm running my 
game loop, I totally avoid all kind of allocations. In a game for 
example, I'm using a text display which needs to concatenate 
strings and show its result: There is 2 ways to do it:

1: Will it need change? Then I use my `@nogc String`.
2: Does it happen only at initialization? Use `string` as anyone 
would do.

Even then, this is not that important, I have coded a lot of 
games in Javascript and the GC never made my game slow. In 
Javascript it is actually impossible to run from GC. The only 
reason I'm doing that is for not making my engine contribute to 
user code GC feeding.


More information about the Digitalmars-d mailing list