Live data from Hacker News

Why I Write Games in C (yes, C)

jonathanwhiting.com

71–80 of 556 posts

Re: Why I Write Games in C (yes, C)

#71
post #46

> The stop-the-world garbage collection is a big pain for games, stopping the world is something you can't really afford to do. I love this opinion from games programmers because they never qualify it and talk about what their latency budgets are and what they do in lieu of a garbage collector. They just hand wave and say "GC can't work". The reality is you still have to free resources, so it's not like the garbage c…

You somewhat explained exactly why a garbage collector can't work. Just because you need to free up memory, doesn't mean a garbage collector is the only solution. There's a big difference over having control of when to release the memory, or having no control of that. For instance, if you know that there are no user inputs or any kind of interactive things going on, you can afford to take a couple millisecond hit freeing memory.

Re: Why I Write Games in C (yes, C)

#72
post #46

> The stop-the-world garbage collection is a big pain for games, stopping the world is something you can't really afford to do. I love this opinion from games programmers because they never qualify it and talk about what their latency budgets are and what they do in lieu of a garbage collector. They just hand wave and say "GC can't work". The reality is you still have to free resources, so it's not like the garbage c…

>The reality is you still have to free resources, so it's not like the garbage collector is doing work that doesn't need to be done.

Does it? In most game I expect resource management to be fairly straightforward, allocation and freeing of resources will mostly be tied to game events which already require explicit code. If you already have code for "this enemy is outside the area and disappears" is it really that much work to add "oh and by the way you might also free the associated resources while you're at it". I don't need a GC thread checking at random intervals "hey let's iterate over an arbitrary portion of memory for an arbitrary amount of time to see if stuff needs dropping yo!".

I realize that I'm quite biased though because I'm fairly staunchly in the "garbage collection is a bad idea that causes more problems than it solves" camp. It's a fairly extremist point of view and probably not the most pragmatic stance.

One place where it might not be quite as trivial would be for instance resource caching in the graphic pipeline. Figuring out when you don't need a certain texture or model anymore. But that often involves APIs such as OpenGL which won't be directly handled by the GC anyway, so you'll still need explicit code to manage that.

That being said I'd still chose (a reasonable subset of) C++ over C for game programming, if only to have access to ergonomic generic containers and RAII.

I write quite a lot of C. When I do I often miss generics, type inference, an alternative to antiquated header files and a few other things. I never miss garbage collection though (because it's a bad idea that causes more problems than it solves).

Re: Why I Write Games in C (yes, C)

#73
post #46

> The stop-the-world garbage collection is a big pain for games, stopping the world is something you can't really afford to do. I love this opinion from games programmers because they never qualify it and talk about what their latency budgets are and what they do in lieu of a garbage collector. They just hand wave and say "GC can't work". The reality is you still have to free resources, so it's not like the garbage c…

For games generally you want zero allocations or frees during gameplay. For a GC'd language you want to avoid the collector running during gameplay.

Having done both they are remarkably similar. Lots of pooling and pre-allocation. Most GC based languages are a bit harder both because they tend not to give you a chunk of memory you can do whatever with and require a lot of knowledge about which parts will allocate behind your back. It's also harder to achieve because you typically get no say when the collector will run.

There are loads of commercial games that pay no attention to this though written in both kinds of language.

Re: Why I Write Games in C (yes, C)

#74
post #46

> The stop-the-world garbage collection is a big pain for games, stopping the world is something you can't really afford to do. I love this opinion from games programmers because they never qualify it and talk about what their latency budgets are and what they do in lieu of a garbage collector. They just hand wave and say "GC can't work". The reality is you still have to free resources, so it's not like the garbage c…

I know this subject quite well and I will later publish a detailed article.

The real run-time cost of memory management done well in a modern game engine written without OOP features is extremely low.

We usually use a few very simple specialized memory allocators, you'd probably be surprised by how simple memory management can be.

The trick is to not use the same allocator when the lifetime is different.

Some resources are allocated once and basically never freed.

Some resources are allocated per level and freed all at once at the end.

Some resources are allocated during a frame and freed all at once when a new frame starts.

And lastly, a few resources are allocated and freed randomly, and here the cost of fragmentation is manageable because we're talking about a few small chunks (like network packets)

Re: Why I Write Games in C (yes, C)

#75
post #27

Earlier quoted context omitted.

The guy should try Rust and then explain why he prefers either, that would be interesting.

Interesting. I want to like rust but everytime I dive in it is getting more and more complicated and verbose.

My experience with Rust is that I have to fight the compiler a lot, but when the program compiles, it works. If it doesn't work, it means there's an error with my file/network paths or I did something in the wrong order, errors which no language can save me from.

Rust also becomes a lot less verbose when you get better at it. The ? operator is especially useful.

Re: Why I Write Games in C (yes, C)

#76
post #46

> The stop-the-world garbage collection is a big pain for games, stopping the world is something you can't really afford to do. I love this opinion from games programmers because they never qualify it and talk about what their latency budgets are and what they do in lieu of a garbage collector. They just hand wave and say "GC can't work". The reality is you still have to free resources, so it's not like the garbage c…

I’m not a game programmer but with RAII for example (I realize this is not a C idiom), it really becomes a non-issue if you have things scoped properly. For everything that doesn’t fit into this box you probably wouldn’t be relying on a GC anyway, the GC may do the final memory reap but scope control is still often manual in memory-managed languages.

Re: Why I Write Games in C (yes, C)

#77
post #58

The author's opinion is uncommon but not unique. A few examples: https://handmadehero.org/ https://ourmachinery.com/post/physical-design/ Simple libs widely used in game dev circles: https://github.com/nothings/stb This one is a full game engine with tools made for educational purpose: https://www.raylib.com/ I do write games and game engine code and tools in C++ without using any of the OOP features. I know quite a…

> I do write games and game engine code and tools in C++ without using any of the OOP features. Excuse my C/C++ ignorance, but why not simply use C?

If C gets operator overloading and RAII, there will be no good reason to use C++ for me!!

Re: Why I Write Games in C (yes, C)

#78
post #5

So... essentially "I know and like C". Which is great! But as you say, that's not particularly useful for anybody else.

The guy should try Rust and then explain why he prefers either, that would be interesting.

He clarified in the post that he doesn't like the complexity that C++ brings, he probably won't appreciate Rust's either.

Re: Why I Write Games in C (yes, C)

#79

Earlier quoted context omitted.

Handmadehero started as C but transition to C++ AFAICT ?

There is no classes, extremely rare use of templates in the Handmade Hero code. C++ with structs and 99% procedural code is actually very close to vanilla C. Let's not be pedantic here.

Classes and structs in C++ are almost the same thing with the exception of the default member visibility, unless you mean a POD?

Re: Why I Write Games in C (yes, C)

#80
post #46

> The stop-the-world garbage collection is a big pain for games, stopping the world is something you can't really afford to do. I love this opinion from games programmers because they never qualify it and talk about what their latency budgets are and what they do in lieu of a garbage collector. They just hand wave and say "GC can't work". The reality is you still have to free resources, so it's not like the garbage c…

Here are some common alternatives I've seen and used:

1) Don't allocate or free resources during gameplay. Push it to load time instead. This works for things like assets that don't change much.

2) Use an arena that you reset regularly. This works well for scratch data in things like job systems or renderers.

3) Pick a fixed maximum number of entities to handle, based on hardware capacity, and use an object pool. This works well for things that actually do get created and destroyed during gameplay, on a user-visible timescale.

Together, these get you really far without any latency budget going toward freeing resources. And there is always something else you could put that budget toward instead, which is why any amount of GC is often seen as a waste.

Post reply on HN