Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using D for a realtime application?

I am considering using d for my ongoing graphics engine. The one thing that turns me down is the GC.

I am still a young programmer and I probably have a lot of misconceptions about GC's and I hope you can clarify some concerns.

I am aiming for low latency and timing in general is crucial. From what I know is that GC's are pretty unpredictable, for example my application could render a frame every 16.6ms and when to GC's kicks in it could go up to any number like 30ms because it is not deterministic right?

I read that you can turn down the GC in D, but then you can't use the majority of D's standard library and the GC is not completely off. is this true?

Do you think it makes sense to use D in a timing crucial application?

like image 437
Maik Klein Avatar asked Dec 27 '22 03:12

Maik Klein


1 Answers

Short answer: it requires lot of customization and can be really difficult if you are not an experienced D developer.

List of issues:

Memory management itself is not that big problem. In real-time applications you never ever want to allocate memory in a main loop. Having pre-allocated memory pools for all main data is pretty much de-facto standard way to do such applications. In that sense, D is not different - you still call C malloc directly to get some heap for your pools and this memory won't be managed by a GC, it won't even know about it.

However, certain language features and large parts of Phobos do use GC automagically. For example, you can't really concatenate slices without some form of automatically managed allocation. And Phobos simply has not had a strong policy about this for quite a long time.

Few language-triggered allocations won't be a problem on their own as most memory used is managed via pools anyway. However, there is a killer issue for real-time software in stock D : default D garbage collector is stop-the-world. Even if there is almost no garbage your whole program will hit a latency spike when collection cycle is ran, as all threads get blocked.

What can be done:

1) Use GC.disable(); to switch off collection cycles. It will solve stop-the-world issue but now your program will start to leak memory in some cases, as GC-based allocations still work.

2) Dump hidden GC allocations. There was a pull request for -vgc switch which I can't find right now, but in its absence you can compile your own druntime version that prints backtrace upon gc_malloc() call. You may want to run this as part of automatic test suite.

3) Avoid Phobos entirely and use something like https://bitbucket.org/timosi/minlibd as an alternative.

Doing all this should be enough to target soft real-time requirements typical for game dev, but as you can see it is not simple at all and requires stepping out of stock D distribution.

Future alternative:

Once Leandro Lucarella ports his concurrent garbage collector to D2 (which is planned, but not scheduled), situation will become much more simple. Small amount of GC-managed memory + concurrent implementation will allow to meet soft real-time requirements even without disabling GC. Even Phobos can be used after it is stripped from most annoying allocations. But I don't think it will happen any soon.

But what about hard real-time?

You better not even try. But that is yet another story to tell.

like image 185
Mihails Strasuns Avatar answered Jan 07 '23 07:01

Mihails Strasuns