Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not all .NET games are made equal.

First of all, there is the runtime they are actually using.

Then the language version.

Finally, how well the devs know how to do low level optimisations.



Unfortunately you mostly don't do low-level optimisations in C#. You can manipulate raw memory but unsafe C# is horrifyingly unsafe - far more dangerous than writing C - and code written with it has to navigate a minefield to interact safely with ordinary C#.

Instead, heavy optimisation in C# is usually "high level but knowing the runtime in great detail" optimisation. Stuff like knowing the hairy details of the GC implementation (eg by reading Pro .NET Memory Management and/or the single 30,000 line C++ file that contains the GC implementation [1]) and using this knowledge to write ordinary C# code in such a way that the GC can handle its usage patterns very efficiently, or doing things like object pooling that try to cut the GC out of the picture altogether.

Essentially, it's knowing what you want the machine to do and figuring out a way to trick the runtime into doing that instead of what it wants to do. I find it massively frustrating and after doing it for a few years I moved back to languages that try to do what you tell them to do instead of the other way round. Some people seem to thrive on it, though, and manage to get impressive results - see, eg. Marc Gravell's blog.

[1] https://github.com/dotnet/runtime/blob/main/src/coreclr/gc/g...


I've shipped games in C, C++ and C# and I don't understand your basis for 'unsafe C# is more dangerous than C'. What is the argument here? Modern C# especially has things like ref returns and span to allow you to do type safe, bounds checked operations on stack allocated buffers, mmap'd files etc in a way that isn't possible in C.

I do a lot of gnarly stuff in C# on .NET 4.8 (no spans! it's too old!) and still almost never run into memory safety issues or crashes, it's far more stable than my experiences working with C/C++ codebases.

I recently got rid of one of the main crash sources in my codebase - a line for line port (unsafe, pointers) of a C library that I was using. I replaced it with a from scratch C# rewrite using 'ref' (no pointers) and got better performance with no memory safety crashes.


So I'm mostly talking about using unsafe C# as a way of doing things that the runtime otherwise prevents you from doing - i.e. you're in a C# codebase and want to implement some data structure or algorithm that doesn't sit welll with the GC/runtime. As an example, perhaps it relies on many contiguous buffers larger than the LOH threshold, or uses many medium-lifespan objects (a common usage pattern that GC, at least of the .NET variety, has no better answer to than "try not to do that" but which arena allocation handles with essentially no overhead).

I'd distinguish between those use cases and ones where you're using unsafe C# to interface with external unmanaged code (I've done both). Such interop-like use cases are also rather hairier than writing C against those APIs because with C at least you can generally consume a header file and have some confidence that your compiler understands the binary interface you'll be interacting with at runtime, whereas in C# the burden of trying to ensure that the data types and signatures are correct against the actual binary loaded is on you, and it can be a heavy one especially if you want your code to work cross platform. (Yes, MS have come up with things like C++/CLI that should help, but have shown no commitment to them. If you want things to reliably work across platforms and in the future you're basically stuck with P/Invoke and unsafe.)

But the dangers are worse for the former kind of code. Here the worst risks arise around the interactions between the unsafe code and the safe code. Typically in this sort of situation you want to wrap your nasty unsafe code in a nice, safe API. You want to make it so that ordinary C# callers can't make mistakes with your managed API that would cause crashes, leaks, etc. In fact, it's more than "want". You really have to. It's C# and people need to be able to program in it like it's C#, without expecting that a missed "using" somewhere is going to cause a segfault. And that can be really hard to do. The crux of the problem is that the GC wants to manage the lifetimes of the managed objects and offers no way to hook into those lifetimes other than finalizers, with their well known limitations/downsides or disposal which you can't rely on callers to invoke. If you create/manipulate some unmanaged resource and wrap it in a managed API there are lots of fun little gotchas to run into, such as the fact that an object can be garbage collected while a method on it is executing. [1]

I believe it's for this reason that interesting unmanaged data structures written in unsafe C# and exposed as safe-to-use C# types aren't really a thing in the .NET world. It really is very hard indeed to do it safely and efficiently. Unsafe is more commonly used for interop glue, where it's better suited (but still a bit risky).

The need to do things like this has led to improvements such as spans, but I'm not a fan. A ref struct is a horribly, arbitrarily limited thing (it can basically only exist on the stack) and I found programming with Spans an exercise in frustration as a result. For one thing, the moment you find you need a span of spans you'll find yourself reaching for those grubby raw pointers again. And at best they really just give you bounds checking. They don't help at all with the hard problem: resource management (and neither does Memory).

So the comparison with C - what do I mean by "more dangerous"? Well, it's a subjective thing, of course. I don't really mean there are more opportunities to screw up. What I mean is that when I was writing this stuff I had to work harder, think harder and move slower not to screw up. The pitfalls were less obvious, there was less prior art, fewer established practices and a general feeling that I was going against the grain. YMMV, but it's not something I'm going to miss.

[1] https://devblogs.microsoft.com/oldnewthing/20100810-00/?p=13...


I advise to update yourself what using C#11 offers in possibilities beyond only unsafe code blocks.

Based on my C and C++ years, it is hardly any difference from doing optimization, knowing what the compiler actually generates, how is the runtime and standard library actually implemented, and above all getting to use the proper data structures, algorithms and a profiler.


Nah, I'm not going back to C# and don't plan to stay up to date with it. If you're talking about ref structs, Spans etc, I'm perfectly aware and have used those features in anger (in more ways than one).

It clearly suits some people but it isn't for me.


Fine, however better focus on good game design and not what language to use, see Minecraft, Stardew Valley, Celeste, Bastion,....

Anyone that disregards languages used by them, should also have better sales numbers to go along.


> Stardew Valley, Celeste, Bastion

Those are all games where the average frame time is so low GC pauses are basically irrelevant.

And Minecraft was re-written in C++ for a reason.


Minecraft Bedrock exists for game consoles and mobile phones, where Microsoft naturally wouldn't write an AOT compiler just for it.

Minecraft Java, not only keeps being sold, it is where new features are tested, before the Bedrock team implements similar capabilities.

Any online shop where we can appreciate the fruits of how a better language choice translates into improved game design and increased sales?


I agree that understanding how the GC was built is very important. Little nuances like forcing workstation mode and explicitly calling GC.Collect can make all the difference.

You can get very far with a GC language like this. The part that bothers me are the built-ins that allocate. TPL, AspNetCore, et. al. If I want a truly zero-alloc C# solution, I have to go all the way to the bottom with abstractions like Socket, NetworkStream, SslStream, etc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: