by tmountain · 2y ago · 0 comments
More specifically with .NET6+, I am seeing GC pauses measured in the 1~5ms range in my DIY engines when I strategically call GC.Collect() on a rapidly-recurring basis. Another tweak I made was to disable concurrent/server/background GC in the project config. Foreground+workstation GC appears to provide the lowest jitter in this arrangement. My engine's main loop operates on batches of events pulled from a ring buffer abstraction, and if one of those events just so happens to be a scheduled GC, then I deal with it right there in-line on the same thread. I do not attempt to defer it to a GC thread or some other weirdness.
The only strategic options seem to be to either explicitly manage all the memory 100%, or take out the trash as often as possible. You could also entertain the cruise missile GC strategy, but Microsoft has made it nearly impossible to completely disable GC in latest .NET without employing black magic low level DLL hackery. I can definitely see a situation where you don't care about GC because you can just recycle the process between business events, rounds of gameplay, etc. Process.Exit() is a viable GC technique in some domains.
This isn't just a language-specific feature and is actually one of the core design decisions made in the C++ engine. Almost every object in Godot inherits the RefCounted base class - as a consequence even in C# scripting mode Godot-specific objects generally have reference counting semantics (and therefore also provides the WeakRef type to deal with reference cycles yourself.)
This is a great point, and something someone pointed out in the Q/A portion at the end.. I don't think Icaza cared though, he seemed very motivated to slay the dragons of his past and make good after working so hard to integrate C# into Godot.
There's another talk about the GDScript from the same conference that I've been meaning to finish watching: https://www.youtube.com/watch?v=WNR4Tne5DRE
Also given the state of Swift outside Apple ecosystem, I guess he only plans to target developers on their platforms.
Swift is a really interesting language, and Apple does distribute official toolchain binaries for Linux and Windows. But yeah it's still a bit of an awkward choice if you're not using Xcode, hopefully that improves.
CAPCOM has their own engine based on a fork from .NET Core, used it on Devil May Cry for the PlayStation 5.
Good luck getting similar adoption for Swift outside Apple platforms, specially game consoles.
If not C# alongside C++, studios would rather use Rust not Swift, e.g. Embark and Activision.
Metal has now C++ bindings, as Apple had to cede Objective-C and Swift bindings weren't being welcomed by major studios.
I like Swift, however I don't see SwiftGodot going anywhere outside iDevices.
That isn't bad if that is the goal though, plenty of companies are doing quite well being single platform.
Modern GC implementations offer an important advantage - very cheap allocations and batched deallocations which allow to sustain high allocation traffic that goes through the heap.
This comes at a cost of non-deterministic memory usage and object de-allocation. Further tradeoff is made between GC pause times, allocation throttling (Go) and even higher memory footprint (ZGC).
My knowledge on the exact overhead of Swift's reference counting is very limited but there's a good chance it comes with significant upfront cost that you don't have to pay in languages with GC or with compile-time-defined allocation semantics (Rust and C++ RAII). There's a reason why Apple invested in atomics being as cheap[0] as they are on their ARM64 cores.
Overall, every time I find Swift benchmark numbers on the internet, they turn out to be far[1][2] from near-Rust performance despite being a language built on top of LLVM.
[0] https://dougallj.github.io/applecpu/firestorm-int.html (e.g. CAS, CASAL, DMB)
[1] https://github.com/ixy-languages/ixy-languages
[2] https://github.com/jinyus/related_post_gen#multicore-results
Unity's long term plan appears to become more tightly integrated with .NET and to avoid maintaining their own .NET stuff.
Dlangs GC for instance guarantee that it will only run on allocation of new memory so it's possible to architect the game to avoid meaningful pauses
Generally speaking, indefinitely preventing GC from running is not possible (you always end up putting some data on the heap) therefore an optimal strategy is similar to any other language - limiting allocations and reusing memory through object and array pooling. This will ensure that GC pauses are as infrequent and as short as possible. It's important to note that if there is sufficient allocation budget - the GC will not run.
This way, in a well written code the GC may only ever trigger every few hundred frames and only take a millisecond to run. In fact, OSU! has been able to get consistently good frame times even back on .NET Framework.
[0] https://github.com/dotnet/runtime/blob/main/docs/design/core...
[1] https://maoni0.medium.com/dynamically-adapting-to-applicatio...