2 ms·
So long as you write fast C#, C# is pretty fast. But you need to make C# that effectively compiles (JIT's) to the same assembly as a C or C++ program would. It'
by alkonaut 7y ago
So long as you write fast C#, C# is pretty fast. But you need to make C# that effectively compiles (JIT's) to the same assembly as a C or C++ program would. It's simply staying with a subset of C# that looks a lot like C.
It's really hard to do because you can't easily see when you pay for an abstraction (is this foreach slower than a for loop? does it allocate an enumerator? can it unroll? will this call allocate a heap array for its params? Will this call be devirtualized?)
The "Burst compiler" in Unity takes it a step further: it restricts the language to those things that are actually always fast.
https://docs.unity3d.com/Packages/com.unity.burst@0.2/manual/index.html#burst-user-guide https://docs.unity3d.com/Packages/com.unity.burst@0.2/manual...
I really wish the C# team could do something similar: add a fast AOT subset to C#.
- cm2187 7y agoI would have thought the problem with using C# for a game wouldn’t be so much the execution speed (which should be pretty close to native) than the freezes required for garbage collection.
- Rapzid 7y agoYou just don't create garbage. Object pools and stack variables.
- romanovcode 7y agoUnity is C# and there are many games which are fast. If you don't want GC to slow you down - just don't add garbage.
- alkonaut 7y agoYes, that's one of the problems. So you want to have zero garbage collection. That means you want zero garbage creation. That in turn means not allocating on the heap in the game loop, and that's rather easy to avoid doing explicitly (just don't use "new" for class types, for example!, use pooling and structs). But C# makes it tricky because a lot of the BCL types allocate under the hood. Some times even the compiler inserts allocatoins from syntax sugar (parameter arrays for varargs). These are pitfalls that the Burst subset tries to solve.
- jayd16 7y agoWe'll see. Unity promised the world with IL2CPP as well.