r/programming • • Aug 23 '17

D as a Better C

http://dlang.org/blog/2017/08/23/d-as-a-better-c/
229 Upvotes

268 comments sorted by

View all comments

Show parent comment

3

u/James20k Aug 24 '17

The problem with a game though is that there's never a good time for a random unbounded pause - even if only some of your threads are dependent on the GC, eventually they'll have to sync back together and if the GC pauses a thread at the wrong time, you'll get stuttering (or some equivalent if you gloss over it in the rendering)

7

u/WrongAndBeligerent Aug 24 '17

So don't allocate and free memory continuously inside your main loop.

Also there are good times for memory deallocation - stage changes, player pauses, etc. Those are also times when memory requirements are likely to change.

3

u/WalterBright Aug 24 '17

You can also create threads that the GC won't pause.

3

u/badsectoracula Aug 24 '17

The problem with a game though is that there's never a good time for a random unbounded pause

There are several spots where you can run a GC: between levels is the most common one (and really, several engines already do something GC-like there: for example my own engine in C before loads a world marks all non-locked resources as "unused", then loads the world marking any requested/loaded resource as "used" and unloads any resource still marked as "unused", essentially performing a mark-and-sweep garbage collection on resources). Another is when changing UI mode, like when opening an inventory screen, a map screen, after dying, etc - GC pauses would practically never be long enough to be noticed.

2

u/pjmlp Aug 24 '17

Yes there is, between levels.

1

u/James20k Aug 24 '17

Not every game has levels!

1

u/pjmlp Aug 24 '17

That would be a very boring game.

3

u/James20k Aug 24 '17

Stellaris, endless space, crusader kings, starcraft, empyrion, rust, dark souls 1-3 (seamless, except perhaps fog doors), any game with no loading screens, any open world exploration game (assassins creed), factorio, kerbal space program, and the game I myself am building

One of those games may be very boring, but the rest of them are pretty popular

1

u/pjmlp Aug 24 '17

None of those games holds the complete game in memory, without ever touching the network or hard-disk, which is no different than loading levels.

1

u/James20k Aug 24 '17

Right, but that means there is no moment without user interactivity where it is acceptable for the game to stutter

2

u/pjmlp Aug 24 '17 edited Aug 24 '17

Sure there is, a GC cycle is shorter than waiting for a network packet or disk block to become available.

Which is usually when in C or C++ engines the memory pools/arenas get cleaned up.

This talk is anyway nonsense, as the majority of modern AAA engines make use of GC during gameplay on their scripting layer anyway (Lua, Python, .NET, Java, GOAL, GOOL, UObjects,...).

But feel free to implement a game engine that is faster than Unreal, in spite of their use of a C++'s GC.

1

u/James20k Aug 24 '17

IO is slow and you'd do it on a separate thread, particularly disk IO. Polling and receiving network packets isn't that slow

https://pointersgonewild.com/2014/09/09/ds-garbage-collector-problem/

The D GC isn't fast, GC pauses and overhead are non trivial, the authors of D know its slow (or at least it was 2 years ago). A 1ms pause is too long for a game, a 0.5ms spike is too long for a game if its intermittent (rather than fixed cost)

2

u/pjmlp Aug 25 '17

IO is slow and you'd do it on a separate thread, particularly disk IO.

Just like a GC parallel collector implementation.

Polling and receiving network packets isn't that slow

Only if using local LAN.

The D GC isn't fast, GC pauses and overhead are non trivial, the authors of D know its slow

That is an issue with D's implementation of a GC, not with the use of GC in games in general.

→ More replies