8 ms·
If you really wanted to, you can program in a subset of C# that is similar to C. You can avoid the garbage collector by using a native memory allocator and then
by MarkSweep 8y ago
If you really wanted to, you can program in a subset of C# that is similar to C. You can avoid the garbage collector by using a native memory allocator and then manipulate pointers with the usual * and -> operators.
While you probably don't want to write a whole program like that, sometimes it is helpful to use these features when porting code from C or for performance reasons. See for example this implementation of memmove that is used for copying strings and other things:
https://github.com/dotnet/coreclr/blob/7ddd038a33977b152e856448443425cbc6b591c6/src/System.Private.CoreLib/src/System/Buffer.cs#L183 https://github.com/dotnet/coreclr/blob/7ddd038a33977b152e856...
- golergka 8y agoYou mean, something like this? https://blogs.unity3d.com/2019/02/26/on-dots-c-c/ https://blogs.unity3d.com/2019/02/26/on-dots-c-c/ TLDR: arguably the most popular game engine of our time has already implemented exactly that (although in more of an experimental and unstable stage yet).
- FreakLegion 8y agoNot quite. This has been possible in C# for 15 years with AllocHGlobal. It's handy for avoiding the speed and especially memory[1] overhead of managed objects. 1. https://codeblog.jonskeet.uk/2011/04/05/of-memory-and-strings/ https://codeblog.jonskeet.uk/2011/04/05/of-memory-and-string...
- JustSomeNobody 8y agoThis is basically what I do. We don't have real time constraints, but we do have a limit to how long processing can take so we need to shave as many ms off how long our code executes as possible, so we pretty much use just the bare minimum features of the language and profile the crap out of everything. One could argue that C# wasn't the best choice, but that was out of my hands.
- voltagex_ 8y agoAre you able to share any examples of functions where writing C# like that gave measurable benefits? I'm over in web-land and it's rare that C# itself is the bottleneck.
- scott00 8y agoOne example from my line of work that could conceivably translate to web-land would be parsing numbers. I wrote a version of double.Parse() with less features for an order of magnitude speedup. The program was doing data analysis on a bunch of CSV files, and just parsing the files was way slower than EBS I/O.
- voltagex_ 8y agoUh, wow. // Managed code is currently faster than glibc unoptimized memmove // TODO-ARM64-UNIX-OPT revisit when glibc optimized memmove is in Linux distros // https://github.com/dotnet/coreclr/issues/13844