News for hackers. Stuff that matters.
Re:A comparison of Rust's borrow checker to the one in C# by jabwd on Thursday October 31, 2024 @04:58AM (#42004728)
Except that the GC makes it exactly not viable for games and its one of the biggest problems Unity devs run into. I agree it's a great language, but its not a do it all.
Re:A comparison of Rust's borrow checker to the one in C# by SeasonalEnnui on Thursday October 31, 2024 @05:12AM (#42004827)
I could argue the opposite - GC makes it more viable for games. "GC is bad" misses too much nuance. It goes like this: developer very quickly and productively gets minimum viable game going using naive C# code. Management and investors are happy with speed of progress. Developers see frame rate stutters, they learn about hot path profiling, gen0/1/2/3 GC & how to keep GC extremely fast, stackalloc, array pooling, Span<T>, native alloc; progressively enhancing quickly until there are no problems. These advanced concepts are quick and low risk to use, and in the case of many of the advanced concepts; what you would be doing in other languages anyway.
Re:A comparison of Rust's borrow checker to the one in C# by DeathArrow on Thursday October 31, 2024 @05:26AM (#42004913)
The only reason we might see FPS drop in games, is not because C# and its GC. It's mostly because the poor usage of the graphics pipeline and the lack of optimization. As a former game developer I had to do a lot of optimization so our games run nicely on mobile phones with modest hardware.

C# it's plenty fast for game programming.

Re:A comparison of Rust's borrow checker to the one in C# by mycocola on Thursday October 31, 2024 @06:07AM (#42005197)
That entirely depends on the game. Recent example is Risk of Rain 2, which had frequent hitches caused by the C# garbage collector. Someone made a mod to fix this by delaying the garbage collection until the next load-screen — in other words, controlled memory leakage.

The developers of Risk of Rain 2 were undoubtedly aware of the hitches, but it interfered with their vision of the game, and affected users were left with a degraded experience.

It's worth mentioning that when game developers scope of the features of their game, available tech informs the feature-set. Faster languages thus enable a wider feature-set.

Re:A comparison of Rust's borrow checker to the one in C# by n4r9 on Thursday October 31, 2024 @09:11AM (#42006561)
Apparently that was released in 2019? Both C# and dotnet have had multiple major releases since then, with significant performance improvements.
Re:A comparison of Rust's borrow checker to the one in C# by Rohansi on Thursday October 31, 2024 @10:20AM (#42007195)
The GC that Unity is using is extremely bad by today's standards. C# everywhere else has a significantly better GC.

In this game's case though they possibly didn't do much optimization to reduce GC by pooling, etc. Unity has very good profiling tools to track down allocations built in so they could have easily found significant sources of GC allocations and reduced them. I work on one of the larger Unity games and we always profile and try to pool everything to reduce GC hitches.

Re:A comparison of Rust's borrow checker to the one in C# by munificent on Thursday October 31, 2024 @10:38AM (#42007369)
> It's worth mentioning that when game developers scope of the features of their game, available tech informs the feature-set. Faster languages thus enable a wider feature-set.

This is true, but developer productivity also informs the feature set.

A game could support all possible features if written carefully in bare metal C. But it would take two decades to finish and the company would go out of business.

Game developers are always navigating the complex boundary around "How quickly can I ship the features I want with acceptable performance?"

Given that hardware is getting faster and human brains are not, I expect that over time higher level languages become a better fit for games. I think C# (and other statically typed GC languages) are a good balance right now between good enough runtime performance and better developer velocity than C++.

Re:A comparison of Rust's borrow checker to the one in C# by Const-me on Thursday October 31, 2024 @10:53AM (#42007540)
> frequent hitches caused by the C# garbage collector

They probably create too much garbage. It’s equally easy to slow down C++ code with too many malloc/free functions called by the standard library collections and smart pointers.

The solution is the same for both languages: allocate memory in large blocks, implement object pools and/or arena allocators on top of these blocks.

Neither C++ nor C# standard libraries have much support for that design pattern. In both languages, it’s something programmers have to implement themselves. I did things like that multiple time in both languages. I found that, when necessary, it’s not terribly hard to implement that in either C++ or C#.

Re:A comparison of Rust's borrow checker to the one in C# by smaudet on Thursday October 31, 2024 @11:33AM (#42007919)
> In both languages, it’s something programmers have to implement themselves.

I think this is where the difference between these languages and rust shines - Rust seems to make these things explicit, C++/C# hides behind compiler warnings.

Some things you can't do as a result in Rust, but really if the rust community cares it could port those features (make an always stack type type, e.g.).

Code base velocity is important to consider in addition to dev velocity, if the code needs to be significantly altered to support a concept it swept under the rug e.g. object pools/memory arenas, then that feature is less likely to be used and harder to implement later on.

As you say, it's not hard to do or a difficult concept to grasp, once a dev knows about them, but making things explicit is why we use strongly typed languages in the first place...

Re:A comparison of Rust's borrow checker to the one in C# by SeasonalEnnui on Thursday October 31, 2024 @05:30AM (#42004933)
A good datapoint, thanks. Extending my original point - C# got really good in the last 5 years with regards to performance & low-level features. There might be an entrenched opinion problem to overcome here.
Re:A comparison of Rust's borrow checker to the one in C# by bluGill on Thursday October 31, 2024 @09:29AM (#42006704)
Anybody writing a game should be writing in a game engine. There are too many things you want in a game that just come "free" from an engine that you will spend years writing by hand.

GC can work or not when writing a game engine. However everybody who writes a significant graphical game engine in a GC language learns how to fight the garbage collector - at the very least delaying GC until between frames. Often they treat the game like safety critical: preallocate all buffers so that there is no garbage in the first place (or perhaps minimal garbage). Without garbage collection might technically use more CPU cycles, but in general they are spread out more over time and so more consistent.

Re:A comparison of Rust's borrow checker to the one in C# by lifthrasiir on Thursday October 31, 2024 @07:42AM (#42005801)
At least for Unity, the actual problem lies in IL2CPP and not C#. I have professionally used C# in real-time game servers and GC was never a big issue. (We did use C++ in the lower layer but only for the availability of Boost.Asio, database connectors and scripting engines.)
Re:A comparison of Rust's borrow checker to the one in C# by Rohansi on Thursday October 31, 2024 @10:26AM (#42007242)
Unity lets you use either IL2cPP (AOT) or Mono (JIT). Either way it will use Boehm GC which is a lot worse than the .NET GC. If your game servers weren't using Unity then they are using a better GC.
Re:A comparison of Rust's borrow checker to the one in C# by lifthrasiir on Thursday October 31, 2024 @12:04PM (#42008230)
Yeah, we rolled our own server framework in .NET mainly because we were doing MMOs and there were no off-the-shelf frameworks (including Unity's) explicitly designed for that. In fact, I believe this is still mostly true today.
Re:A comparison of Rust's borrow checker to the one in C# by zigzag312 on Thursday October 31, 2024 @06:17AM (#42005263)
Games would need alternative GC optimized for low latency instead of maximum throughput.

AFAIK it has been possible to replace the GC with alternative implementation for the past few years, but no one has made one yet.

EDIT: Some experimental alternative GC implementations:

https://github.com/kkokosa/UpsilonGC

https://www.codeproject.com/Articles/5372791/Implementing-a-...

Re:A comparison of Rust's borrow checker to the one in C# by DeathArrow on Thursday October 31, 2024 @05:21AM (#42004883)
> one of the biggest problems Unity devs run into

Unity used Mono. Which wasn't the best C# implementation, performance wise. After Mono changed its license, instead of paying for the license, Unity chose to implement their infamous IL2CPP, which wasn't better.

Now they want to use CoreCLR which is miles better than both Mono and IL2CPP.

Re:A comparison of Rust's borrow checker to the one in C# by pjmlp on Thursday October 31, 2024 @05:30AM (#42004930)
Except that is a matter of developer skill, and Unity using Mono with its lame GC implementation, as proven by CAPCOM's custom .NET Core fork based engine used for Devil May Cry on the PlayStation 5.
Re:A comparison of Rust's borrow checker to the one in C# by smaudet on Thursday October 31, 2024 @01:15PM (#42008959)
We can all agree Unity is terrible.

Would be nice to hear about a Rust Game engine, though.

Re:A comparison of Rust's borrow checker to the one in C# by pjmlp on Thursday October 31, 2024 @02:38PM (#42010011)
Check Bevy.
Re:A comparison of Rust's borrow checker to the one in C# by bob1029 on Thursday October 31, 2024 @05:23AM (#42004890)
GC in modern .NET runtime is quite fast. You can get very low latency collections in the normal workstation GC mode.

Also, if you invoke GC intentionally at convenient timing boundaries (I.e., after each frame), you may observe that the maximum delay is more controllable. Letting the runtime pick when to do GC is what usually burns people. Don't let the garbage pile up across 1000 frames. Take it out every chance you get.

Re:A comparison of Rust's borrow checker to the one in C# by Traubenfuchs on Thursday October 31, 2024 @05:58AM (#42005139)
> if you invoke GC intentionally at convenient timing boundaries (I.e., after each frame),

Manually invoking GC many times per second is a viable approach?

Re:A comparison of Rust's borrow checker to the one in C# by bob1029 on Thursday October 31, 2024 @09:38AM (#42006787)
It has worked well in my prototypes. There is a reason a GC.Collect method is exposed for use.
Re:A comparison of Rust's borrow checker to the one in C# by smaudet on Thursday October 31, 2024 @01:13PM (#42008935)
At least for this instance you have a good idea which objects are "ripe" for collection. There should be some way to specify "collect these, my infra objects don't need to be".
Re:A comparison of Rust's borrow checker to the one in C# by munificent on Thursday October 31, 2024 @10:40AM (#42007395)
It can be, yes.

You're basically trading off worse throughput for better latency.

If you forcibly run the GC every frame, it's going to burn cycles repeatedly analyzing the same still-alive objects over and over again. So the overall performance will suffer.

But it means that you don't have a big pile of garbage accumulating across many frames that will eventually cause a large pause when the GC runs and has to visit all of it.

For interactive software like games, it is often the right idea to sacrifice maximum overall efficiency for more predictable stable latency.

Re:A comparison of Rust's borrow checker to the one in C# by neonsunset on Thursday October 31, 2024 @11:22AM (#42007825)
This might be more problematic under CoreCLR than under Unity. Prematurely invoking GC will cause objects that are more likely to die in Gen 0 to be promoted to Gen 1, accumulate there and then die there. This will cause unnecessary inter-generational traffic and will extend object lifetimes longer than strictly necessary. Because live object count is the main factor that affects pause duration, this may be undesirable.

It might be more useful to use OSU! approach as a reference: https://github.com/dotnet/runtime/issues/96213#issuecomment-...

OSU! represents an extreme case where the main game loop runs at 1000hz, so for much more realistic ~120hz you have plenty of options.

Re:A comparison of Rust's borrow checker to the one in C# by smaudet on Thursday October 31, 2024 @01:10PM (#42008893)
If you could even just pass an array of objects to be collected or something, this would so much easier.

Magic, code or otherwise, sucks when the spell/library/runtime has different expectations than your own.

You expect levitation to apply to people, but the runtime only levitates carbon based life forms. You end up levitating people without their affects (weapons/armor), to the embarrassment of everyone.

There should be no magic, everything should be parameterized, the GC is a dangerous call, but it should be exposed as well (and lots of dire warnings issued to those using it).

Re:A comparison of Rust's borrow checker to the one in C# by munificent on Thursday October 31, 2024 @04:34PM (#42011221)
> If you could even just pass an array of objects to be collected or something

If you have a bunch of objects in an array that you have a reference to such that you can pass it, then, by definition, those objects are not garbage, since they're still accessible to the program.

Re:A comparison of Rust's borrow checker to the one in C# by smaudet on Thursday October 31, 2024 @04:42PM (#42011301)
Yes. Use a WriteOnlyArray or whatever, Semantics aside though...

There should be some middle ground between RAII and invoking Dispose/delete and full blown automatic GC.

Re:A comparison of Rust's borrow checker to the one in C# by lmm on Thursday October 31, 2024 @05:35AM (#42004969)
Many of the top games in recent years have used it, so you've got a funny definition of "not viable".
Re:A comparison of Rust's borrow checker to the one in C# by johnisgood on Thursday October 31, 2024 @06:09AM (#42005213)
Or roll their own, so they used GC in one way or another.
Re:A comparison of Rust's borrow checker to the one in C# by rafaelmn on Thursday October 31, 2024 @05:57AM (#42005132)
C# has much better primitives for controlling memory layout than Java (structs, reified generics).

BUT it's definitely not a language designed for no-gc so there are footguns everywhere - that's why Rider ships special static analysis tools that will warn you about this. So you can keep GC out of your critical paths, but it won't be pretty at that point. But better than Java :D

Re:A comparison of Rust's borrow checker to the one in C# by neonsunset on Thursday October 31, 2024 @06:44AM (#42005424)
> but it won't be pretty at that point

Possibly prettier than C and C++ still. Every time I write something and think "this could use C" and then I use C and then I remember why I was using C# for low-level implementation in the first place.

It's not as sophisticated and good of a choice as Rust, but it also offers "simpler" experience, and in my highly biased opinion pointers-based code with struct abstractions in C# are easier to reason about and compose than more rudimentary C way of doing it, and less error-prone and difficult to work with than C++. And building final product takes way less time because the tooling is so much friendlier.

Re:A comparison of Rust's borrow checker to the one in C# by greener_grass on Thursday October 31, 2024 @05:20AM (#42004878)
> not viable for games

> Unity devs run into

So it's viable but not perfect

Re:A comparison of Rust's borrow checker to the one in C# by Paradigma11 on Thursday October 31, 2024 @05:23AM (#42004889)
Doesnt Unity use its own GC or transpiles to C++? Unity on .Net core is more than a year away, no?
Re:A comparison of Rust's borrow checker to the one in C# by pjmlp on Thursday October 31, 2024 @05:33AM (#42004950)
It uses the prehistoric Mono GC. Additionally it transpiles IL to C++ due to many targets like consoles, and iDevices, not allowing for a JIT.

They also have a C# subset called Burst, which could have been avoided if they were using .NET Core.

Re:A comparison of Rust's borrow checker to the one in C# by neonsunset on Thursday October 31, 2024 @05:03AM (#42004760)
Unity (and its GC) is not representative of the performance you get with CoreCLR.

The article discusses ref lifetime analysis that does have relationship with GC, but it does not force you into using one. Byrefs are very special - they can hold references to stack, to GC-owned memory and to unmanaged memory. You can get a pointer to device mapped memory and wrap it with a Span<T> and it will "just work".

Re:A comparison of Rust's borrow checker to the one in C# by mafuy on Thursday October 31, 2024 @05:24AM (#42004897)
Well, when I worked in Unity I used to compile C# code with the LLVM backend. It was as fast as C++ code would be. So Unity is perhaps an example in favor of C#.
Re:A comparison of Rust's borrow checker to the one in C# by alkonaut on Thursday October 31, 2024 @08:55AM (#42006433)
It's hard to use C# without creating garbage. But it's not impossible. Usually you'd just create some arenas for your important stuff, and avoid allocating a lot of transient objects such as enumerators etc. So long as you can generate 0 bytes of allocation each frame, you won't need a GC no matter how many frames you render. The question is only this: does it become so convoluted that you could just as well have used C++?
Re:A comparison of Rust's borrow checker to the one in C# by jayd16 on Thursday October 31, 2024 @10:28AM (#42007267)
Enumerators are usually value types as long as you use the concrete type. Using the interface will box it. You can work around this by simply using List<T> as the type instead of the IEnnumerable.

You have to jump through some hoops but it's really not that convoluted and miles easier than good C++.

Re:A comparison of Rust's borrow checker to the one in C# by alkonaut on Thursday October 31, 2024 @10:31AM (#42007297)
The problem with it is that you don't know. The fundamental language construct "foreach" is one that may or may not allocate and it's hard for you as a developer to be sure. Many other low level things do this or at least used to (events/boxing/params arrays, ...).

I wish there was an attribute in C# that was "[MustNotAllocate]" which files the compilation on known allocations such as these. It's otherwise very easy to accidentally introduce some tiny allocation into a hot loop, and it only manifests as a tiny pause after 20 minutes of runtime.

Re:A comparison of Rust's borrow checker to the one in C# by ygra on Thursday October 31, 2024 @10:56AM (#42007571)
While this would be nice for certain applications, I'm not sure it's really needed in general. Most people writing C# don't have to know about these things, simply because it doesn't matter in many applications. If you're writing performance-critical C#, you're already on a weird language subset and know you way around these issues. Plus, allocations in hot loops stand out very prominently in a profiler.

That being said, .NET includes lots of performance-focused analyzers, directing you to faster and less-allocatey equivalents. There surely also is one on NuGet that could flag foreach over a class-based enumerator (or LINQ usage on a collection that can be foreach-ed allocation-free). If not, it's very easy to write and you get compiler and IDE warnings about the things you care about.

At work we use C# a lot and adding custom analyzers ensuring code patterns we prefer or require has been one of the best things we did this year, as everyone on the team requires a bit less institutional knowledge and just gets warnings when they do something wrong, perhaps even with a code fix to automatically fix the issue.

Re:A comparison of Rust's borrow checker to the one in C# by jayd16 on Thursday October 31, 2024 @11:33AM (#42007916)
If you know what types you're using, you do know. If you don't know what you're calling, that's a pretty high bar that I'm not sure C++ clears.
Re:A comparison of Rust's borrow checker to the one in C# by alkonaut on Thursday October 31, 2024 @04:20PM (#42011082)
If you are calling SomeType.SomeMethod(a, b, c) then you don't know what combintions of a, b, c could allocate unless you can peek into it or try every combination of a, b and c. So it's hard to know in the general case even with profiling and testing.
Re:A comparison of Rust's borrow checker to the one in C# by neonsunset on Thursday October 31, 2024 @10:54AM (#42007551)
Most often you do know whether an API allocates. It is always possible to microbenchmark it with [MemoryDiagnoser] or profile it with VS or Rider. I absolutely love Rider's dynamic program analysis that just runs alongside me running an application with F5, ideally in release, and then I can go through every single allocation site and decide what to do.

Even when allocations happen, .NET is much more tolerant to allocation traffic than, for example, Go. You can absolutely live with a few allocations here and there. If all you have are small transient allocations - it means that live object count will be very low, and all such allocations will die in Gen 0. In scenarios like these, it is uncommon to see infrequent sub-500us GC pauses.

Last but not least, .NET is continuously being improved - pretty much all standard library methods already allocate only what's necessary (which can mean nothing at all), and with each release everything that has room for optimization gets optimized further. .NET 9 comes with object stack allocation / escape analysis enabled by default, and .NET 10 will improve this further. Even without this, LINQ for example is well-behaved and can be used far more liberally than in the past.

It might sound surprising to many here but among all GC-based platforms, .NET gives you the most tools to manage the memory and control allocations. There is a learning curve to this, but you will find yourself fighting them much more rarely in performance-critical code than in alternatives.

Re:A comparison of Rust's borrow checker to the one in C# by Rohansi on Thursday October 31, 2024 @10:00AM (#42007000)
Unity has literally the worst implementation of C# out there right now. Not only is it running Mono instead of .NET (Core) but it's also not even using Mono's generational GC (SGen). They have been working on switching from Mono to .NET for years now because Mono isn't being updated to support newer C# versions but it will also be a significant performance boost, according to one of the Unity developers in this area [1].

IL2CPP, Unity's C# to C++ compiler, does not help for any of this. It just allows Unity to support platforms where JIT is not allowed or possible. The GC is the same if using Mono or IL2CPP. The performance of code is also roughly identical to Mono on average, which may be surprising, but if you inspect the generated code you'll see why [2].

[1] https://xoofx.github.io/blog/2018/04/06/porting-unity-to-cor... [2] https://www.jacksondunstan.com/articles/4702 (many good articles about IL2CPP on this site)

Re:A comparison of Rust's borrow checker to the one in C# by para_parolu on Thursday October 31, 2024 @11:43AM (#42008015)
I believe Unity switched to net core last year
Re:A comparison of Rust's borrow checker to the one in C# by Rohansi on Thursday October 31, 2024 @01:21PM (#42009013)
They did not - it is still a work in progress with no announced target release date. They also have no current plans to upgrade the GC being used by IL2CPP (their C# AOT compiler).

https://discussions.unity.com/t/coreclr-and-net-modernizatio...

Re:A comparison of Rust's borrow checker to the one in C# by jayd16 on Thursday October 31, 2024 @10:04AM (#42007042)
The two biggest engines, Unreal and Unity, use a GC. Unity itself uses C#. C# is viable for games but you do need to be aware of the garbage you make.

It's really not that hard to structure a game that pre-allocates and keeps per frame allocs at zero.