News for hackers. Stuff that matters.
Re:Why I Write Games in C (yes, C) by xyproto on Saturday September 21, 2019 @02:09PM (#21036905)
Interestingly, it's fully possible to disable the automatic garbage collection in Go to achieve this.

Disable the garbage collector:

  debug.SetGCPercent(-1)
Trigger garbage collection:

  runtime.GC()
It is also possible to allocate a large block of memory and then manage it yourself.
Re:Why I Write Games in C (yes, C) by littlestymaar on Saturday September 21, 2019 @03:43PM (#21037452)
Due the low throughput of Go's GC (which trades a lot of it in favor of short pause duration), you risk running out if memory if you have a lot of allocations and you don't run your GC enough times.
Re:Why I Write Games in C (yes, C) by xyproto on Saturday September 21, 2019 @03:52PM (#21037511)
For a computer game, if you start out by allocating a large block of memory, then manage it yourself, I don't see how this would be a problem.
Re:Why I Write Games in C (yes, C) by littlestymaar on Saturday September 21, 2019 @04:22PM (#21037702)
You're not using the GC at all then. Why use Go (and praise its GC) in that case?
Re:Why I Write Games in C (yes, C) by Skunkleton on Saturday September 21, 2019 @05:29PM (#21038032)
For the joy of getting the round peg through the square hole of course.
Re:Why I Write Games in C (yes, C) by xyproto on Saturday September 21, 2019 @06:09PM (#21038196)
Go has many advantages over C that are not related to GC.
Re:Why I Write Games in C (yes, C) by littlestymaar on Sunday September 22, 2019 @03:44AM (#21040168)
In a context where you don't allocate memory, you lose a lot of those (for instance, you almost cannot use interfaces, because indirect calls cause parameters to those calls to be judged escaping and unconditionally allocated on the heap).

Go is a good language for web backend and other network services, but it's not a C replacement.

Re:Why I Write Games in C (yes, C) by xyproto on Wednesday September 25, 2019 @05:37AM (#21069158)
If you allocate a large block of memory manually at the start of the program, then trigger the GC manually when it suits you, won't you get the best of both worlds?
Re:Why I Write Games in C (yes, C) by Valmar on Sunday September 22, 2019 @12:40AM (#21039699)
Go also has many disadvantages, compared to plain C.
Re:Why I Write Games in C (yes, C) by xyproto on Sunday September 22, 2019 @03:06AM (#21040066)
Can't think of any. Do you have an example?
Re:Why I Write Games in C (yes, C) by littlestymaar on Sunday September 22, 2019 @03:22AM (#21040101)
You can't call native libraries without going through cgo. So unless you don't want to have audio, draw text and have access to the graphic APIs, you'll need cgo, which is really slow due to Go's runtime. For game dev, that's a no go (pun intended).

Additionally, the Go compiler isn't trying really hard at optimizing your code, which makes it several times slower on a CPU-bound task. That's for a good reason: because for Go's usecase, compile-time is a priority over performances.

Saying that there is no drawbacks in Go is just irrational fandom…

Re:Why I Write Games in C (yes, C) by xyproto on Sunday September 22, 2019 @08:07AM (#21040971)
You are only talking about the Go compiler from Google.

GCC also supports Go (gccgo) and can call native libraries just like from C.

I'm not saying there are no drawbacks in Go, just that I can't think of any advantages of C over Go.

Re:Why I Write Games in C (yes, C) by takeda on Saturday September 21, 2019 @09:08PM (#21038977)
Go was pushed as a C replacement, but very few C programmers switched to it, it seems like it took hearts of some of Python, Ruby, Java etc programmers.
Re:Why I Write Games in C (yes, C) by freyr on Saturday September 21, 2019 @11:49PM (#21039555)
Nonetheless, Go has many advantages over C that are not related to GC.
Re:Why I Write Games in C (yes, C) by takeda on Sunday September 22, 2019 @06:35PM (#21044417)
So does Python or Ruby, that doesn't mean it is a C replacement.
Re:Why I Write Games in C (yes, C) by majewsky on Saturday September 21, 2019 @02:14PM (#21036939)
> It is also possible to allocate a large block of memory and then manage it yourself.

At which point you're mostly just writing C in Go.

Re:Why I Write Games in C (yes, C) by pm90 on Saturday September 21, 2019 @02:22PM (#21036991)
Actually you're not.

I would very much prefer a stripped down version of Go used for these situations rather than throwing more C at it. The main benefits of using Go are not the garbage collection, its the tooling, the readability (and thus maintainability) of the code base, the large number of folks who are versatile in using it.

Re:Why I Write Games in C (yes, C) by kerkeslager on Saturday September 21, 2019 @04:21PM (#21037694)
Readability is subjective.

Large user base? C is number 2. Go isn't even in the top 10.[1]

Tooling? C has decades of being one of the most commonly used languages, and a general culture of building on existing tools instead of jumping onto the latest hotness every few months. As a result, C has a very mature tool set.

[1] https://www.tiobe.com/tiobe-index/

Re:Why I Write Games in C (yes, C) by strken on Saturday September 21, 2019 @04:48PM (#21037842)
Unfortunately the excellent standard library is a major benefit of Go, and it uses the GC, so if you set GOGC=off you're left to write your own standard library.

I would also like to see a stripped-down version of Go that disables most heap allocations, but I have no idea what it would look like.

Re:Why I Write Games in C (yes, C) by metiscus on Saturday September 21, 2019 @02:43PM (#21037108)
Are you saying that there are more go developers than c developers? Is there a user survey that shows such things? I'm curious what the ratio is.
Re:Why I Write Games in C (yes, C) by rdbell on Saturday September 21, 2019 @03:36PM (#21037413)
I'd be willing to wager that C programmers would be more comfortable working with a Golang codebase than Golang programmers would be working with a C codebase.

There may be more "C programmers" by number but a Golang codebase is going to be more accessible to a wider pool of applicants.

Re:Why I Write Games in C (yes, C) by weberc2 on Saturday September 21, 2019 @03:48PM (#21037485)
In my experience it takes a few days for a moderate programmer to come up to speed on Go, whereas it takes several months for C. You need to hire C programmers for a C position, you can hire any programmers for a Go position.
Re:Why I Write Games in C (yes, C) by cozzyd on Saturday September 21, 2019 @06:09PM (#21038195)
If they don't already know C though, how well will they cope with manual memory management?
Re:Why I Write Games in C (yes, C) by nicoburns on Saturday September 21, 2019 @09:27PM (#21039041)
How do people learn C without knowing about manual memory management? They learn about it as they learn the language. This can be done in any language that allows for manual memory management (and most have much better safeguards and documentation than C, which has a million ways to shoot yourself in the foot)
Re:Why I Write Games in C (yes, C) by weberc2 on Saturday September 21, 2019 @08:34PM (#21038849)
It will be a learning curve, but a much, much smaller one than learning C.
Re:Why I Write Games in C (yes, C) by kerkeslager on Monday September 23, 2019 @04:29PM (#21053239)
But the entire point of this line of questioning is that there are more programmers who already know C.
Re:Why I Write Games in C (yes, C) by kerkeslager on Saturday September 21, 2019 @04:22PM (#21037700)
Re:Why I Write Games in C (yes, C) by weberc2 on Saturday September 21, 2019 @03:45PM (#21037471)
You’re writing in a much improved C. Strong type system (including closures/interfaces/arrays/slices/maps), sane build tooling (including dead simple cross compilation), no null-terminated strings, solid standard library, portability, top notch parallelism/concurrency implementation, memory safety (with far fewer caveats, anyway), etc. Go has it’s own issues and C is still better for many things, but “Go with manually-triggered GC” is still far better than C for 99.9% of use cases.
Re:Why I Write Games in C (yes, C) by bufo on Saturday September 21, 2019 @04:38PM (#21037801)
Go’s compiler is not at all optimized for generating fast floating point instructions like AVX and its very cumbersome to add any kind of intrinsics. This might not matter for light games but an issue when you want to simply switch to wide floating point operations to optimize some math.
Re:Why I Write Games in C (yes, C) by weberc2 on Saturday September 21, 2019 @08:44PM (#21038877)
Yeah, C compilers optimize much more than Go compilers. Performance is C’s most noteworthy advantage over Go.
Re:Why I Write Games in C (yes, C) by xyproto on Sunday September 22, 2019 @05:14PM (#21044001)
GCC can compile both C and Go. I searched for benchmarks but found none for GCC 9 that compares the performance of C and Go. Do you have any sources on this?
Re:Why I Write Games in C (yes, C) by weberc2 on Sunday September 22, 2019 @09:01PM (#21045134)
I don’t have a source, but it’s common knowledge in the Go community. Not sure how GCC works, but it definitely produces slower binaries than gc (the standard Go compiler). There are probably some benchmarks where this is not the case, but the general rule is that gcc is slower. gc purposefully doesn’t optimize as aggressively in order to keep compile times low.

Personally I would love for a —release mode that had longer compile times in exchange for C-like performance, but I use Python by day (about 3 orders of magnitude slower than C) so I’d be happy to have speeds that were half as fast C. :)

Re:Why I Write Games in C (yes, C) by xyproto on Sunday September 22, 2019 @03:09AM (#21040076)
Which compiler? The one from Google, GCC (gccgo) or TinyGo?
Re:Why I Write Games in C (yes, C) by omaranto on Saturday September 21, 2019 @07:30PM (#21038588)
Does Go really let you use closures, arrays, slices and maps when you disable the garbage collector? If so, does that just leak memory?
Re:Why I Write Games in C (yes, C) by weberc2 on Saturday September 21, 2019 @08:37PM (#21038853)
Yes, the idea is that you must invoke the GC when you’re not in a critical section. Alternatively you can just avoid allocations using arenas or similar. (You can use arrays and slices without the GC).
Re:Why I Write Games in C (yes, C) by omaranto on Sunday September 22, 2019 @09:59AM (#21041383)
To make sure I understand, is this an accurate expansion of your comment?

Yes it would leak, to avoid leaking you could invoke the GC when you’re not in a critical section. Alternatively, if you don't use maps and instead structure all your data into arrays, slices and structs, you can just avoid allocations using arenas or similar. (You can use arrays and slices without the GC, but maps require it).

Re:Why I Write Games in C (yes, C) by weberc2 on Sunday September 22, 2019 @10:30AM (#21041511)
Yes, that is correct. Anything that allocates on the heap requires GC or it will leak memory. Go doesn’t have formal semantics about what allocates on the heap and what allocates on the stack, but it’s more or less intuitive and the toolchain can tell you where your allocations are so you can optimize them away. If you’re putting effort into minimizing allocations, you can probably even leave the GC on and the pause times will likely be well under 1ms.
Re:Why I Write Games in C (yes, C) by Insanity on Saturday September 21, 2019 @03:37PM (#21037419)
And not to forget that using Go correctly you'd end up doing mostly stack pushes and pops