by t8sr · 5y ago · 0 comments
You can really tell, though, that the language wasn't meant to be used this way. Some common annoyances are:
1) C# is unable to declare fixed-size arrays in structs. There are ways around it, but they are all more unsafe, require code generation or hit the GC. If you've ever written kernel code, you know that fixed-size data members are really common, so this is more annoying that you might think.
2) C# types are either always references (classes) or always values (everything else). There are reference types, but they are restricted so that, for example, the array subscription operator cannot return them. This means that you can't directly operate on values in an array. Once again, there are ways around this, but they involve pointers, which pull in a bunch of the "unsafe" language.
3) The standard library makes a lot of choices that, in hindsight, are extremely unfortunate. Data structures carry generation numbers that increment on various operations and invalidate iterators. This makes a lot of operations that are sublinear in C++ take linear time in C#. You pretty much can't use the C# stdlib for any low-level code.
The projects that successfully use C# in low-level code pretty much all roll their own containers, stdlib and a module that implements black-magic stuff, usually called something like UnsafeUtil. On balance, having written a bunch of lower-level C# code, I'd still slightly prefer it to C++ for (e.g.) talking to the GPU driver, but there's no denying that that's a very low bar to clear. For general kernel code I wouldn't use it.
The 4 languages I would consider for low-level code these days are, in order:
- C/C++ (footguns galore, but lots of existing literature)
- Go (surprisingly easy to control the GC and get it to do what you want)
- Rust (the Rick & Morty of programming languages - not a compliment)
- C# (See above)
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...
.Net 6 of makes sense because it's supposed to be an LTS version. However, if the release and EOL projections[0] I don't think it actually matters all that much: "LTS releases get free support and patches for 3 years. Current releases get free support and patches for 18 months."
That feels fairly aggressive for a library and language for business applications. I imagine many projects won't be released until the version of .Net used to build it is EOL.
[0]: https://dotnet.microsoft.com/platform/support/policy/dotnet-...
Rick&Morty being a very long going (and excellent) parody version of Back To The Future franchise I'm not sure what qualities this characterization imbues on a programming language and would like to hear your views on the matter (since I found your analysis of C# very much agreeable).
My comment was referring to the impression that people who like Rust seem to think that you either also accept Rust as the best thing ever, or you don’t get it. I probably shouldn’t have mentioned it, because now the thread will be about Rust.
I don't think Rust is in any way unique in having people who think that way...
An anecdote: I recently opened a position on my team, and got two applicants on the same day who ignored the project description, and basically said we should hire them to tell us how everything we do could be better if we rewrote multiple projects in Rust.
Of course it's completely possible for a programming language to be both (A) really well-designed and (B) have annoying people among its community. I suspect that's the case here.
https://www.stroustrup.com/quotes.html#:~:text=%22There%20ar....
I love Rust, but I think it does go over the top, more so than other languages.
Rust is the good kind of smart. Actual functional solutions to difficult problems. R&M is the cringe-worthy kind of smart. Built specifically to please the viewers personal ego.
What about Zig? https://ziglang.org
The language itself is really cool, I'm a big fan even if I've not had many chances to write it professionally. It's a really well thought out language, I wouldn't use it in every single scenario but there's a lot of legitimate use-cases for it (especially if you need lots of performance but don't fancy the footguns of C++). The learning curve is quite steep but it's nowhere near as bad as some people make it out to be.
And of course all of them will fight over the specifics (in the case of python, I've been guilty of this when it comes to tabs vs spaces).
The only other langauages I see people this with, are VB & PHP (for somewhat different reasons).
It happens when some new band push the needle for a genre, its only when this happens that the "modern" stuff start to look old.
C is getting deprecated while C++ will need to catch up somehow (if there's a way).
This is actually a good effect of Rust, making people think more that safety is important and that its possible to have more of that in languages without a GC.
(Sorry for hijacking the question, that was not directed at me, but this is a good debate so i couldn't resist)
1) You mention there's a workaround -- I can't recall exactly what it is but I think an attribute added to the array can be used to force this behavior -- that's a language limitation, for sure, in that there's effectively no way using language syntax to declare that (and no way for the compiler to verify that)
2) The latest version of the language blurs the lines here in a few ways -- using ref types allows one to "directly operate on values in an array" without resorting to unsafe.
I think there's really two things to consider in these cases: (1) Can C# handle the requirement at all? (2) Can C# handle it in a way that is compiler verified, or runtime verified without the use of `unsafe`?
The answer to the first question, in my years of working with the language, have always been "Yes". Ultimately, I can find an attribute or fall back to unsafe and do what I need to do without the compiler's help, but the frequency with which I've needed to do so has been "very close to never"[0]. The answer to the second question is often "Yes", too, especially in the most recent versions. The few cases where I have had to drop to `unsafe` for a performance critical piece of code, I have been able to replace with ReadOnlySpan/Span in every case I've encountered, for example.
[0] But I don't write systems software in C#
Agreed on #3, a lot of the BCL is just miserable in terms of design. There have been improvements lately but I'm still stuck using my own custom containers for various cases.
One good move the BCL and runtime teams have made over time is to just expose more unsafe primitives so you can easily build your own stuff - the Memory and Unsafe classes give you lots of useful tools, Span<T> and Memory<T> let you safely work with memory allocated in various ways, and various BCL stuff has been updated to accept Spans instead of requiring you to use managed arrays.
Hard to blame Unity here, though. They have written their own compiler backend that converts CIL to C++ and that was back in the .NET Framework days when performance wasn't that high a priority for language and framework design.
Their transpiler also had a lot in common with ones for other targets (I know for a fact that in the past it used to be very similar structurally to JSIL) which probably improves the likelihood that they would have been able to leverage community knowledge and contributions here.
It's understandable though that they want their transpiler and C#->shader compiler to be their secret sauce, though, since they see them as unique advantages over Godot, Unreal, etc.
Taking Go as example, there are many pitfalls to using it in low-level situations, but the GC is, in practice, never a source of significant issues.
The real problems are:
1) The foreign function interface (CGO) requires a context switch in both directions. (Not because of GC, but because of Go's threading model.) A lot of work ends up being in trying to avoid calls between Go and C in hot loops.
2) Especially talking to the kernel, there are surprising sharp edges in the ABI and C calling convention. I once spent a week trying to populate a bit field correctly. (But other languages would have the same problems matching any C behavior that's left up to the compiler.)
3) The lack of memory fences is a big deal when trying to use kernel-managed ring buffers.
The GC, comparatively, is problem-free for two reasons:
1) A lot of embedded code uses static buffers anyway.
2) The problem of managing memory doesn't go away just because you're in ring0. The Linux VFS, for example, is heavily garbage collected, it just uses a jerry-rigged GC consisting of counting references. I'm sure if I make a categorial statement on this someone on HN will take me to school, but I think a real GC might be a net performance win in some places in the Kernel, like the VFS.
Finally, people often bring up that GC is fine for throughput, but not for latency. Fair enough, but manual reference counting is also not better: someone has to call free at some point, and if you happen to draw the short straw in a blocking system call, it still affects latency. The amortization behavior is probably different, but not in any way that I think matters to most people.
https://blog.discord.com/why-discord-is-switching-from-go-to...
"After digging through the Go source code, we learned that Go will force a garbage collection run every 2 minutes at minimum. In other words, if garbage collection has not run for 2 minutes, regardless of heap growth, go will still force a garbage collection."
Discussed here at https://news.ycombinator.com/item?id=22238335
Of course that difference also tends to go away if you're using static buffers.
Sure, you need to be careful, but given that you'd be doing systems programming anyway, you probably would prefer to have explicit control over bounds checking, since the compiler/JIT is (unfortunately) not sophisticated enough yet to elide bounds checking in many cases.
You aren't going to see C# or Go used in the Linux kernel.
"Rust (the Rick & Morty of programming languages - not a compliment)"
You mean something that is good to great but that "outsiders" or late adopters feel unexplainably threatened by and thus feel the need to attack it and its admirers? Where ironically the detractors are louder and more numerous than the advocates ever were?
Rust is particularly susceptible to this effect as it has been racking up wins and growing in influence, making people feel insecure about their current skillset/knowledge (always a weird motivator for rhetoric in this industry).
I have only dabbled in Rust a few times. It feels very unintuitive and "write-only" from my 1000 foot, ignorant perspective. But I realize that's on me, and appreciate the focus and purpose of the language.
Rust today seems like a 2013 Tesla Model S. A couple of years ago it was the GM EV1: It used to be largely promise of a future to come with a loads of downsides, where today it's expensive to get started with, has a lot of incompatibilities, but it's superior to most alternatives. Tomorrow it and those like it will be the norm.
for example `n.unbind().bind()` can convert to a different type but `inside_n = n.unbind()` will let you work with the data inside the container `n` and after you are finished you can call `inside_n.bind()` to lift it back up to the container type.
Nor C++ or Ada, so what? Linux isn't some kind of golden rule for all OSes on the galaxy.
I am more than happy to do bare metal coding in C# (Meadow) and Go (TinyGo) without having to deal with Linux.
Volumes could be written as a reply.
>Linux isn't some kind of golden rule for all OSes on the galaxy.
Just about every operating system or major piece of system software is written in C, or limited C++ that is defacto C. Rust has a universe more of a likelihood to supplant those than Go or C#. And in fact is the only modern language making any headway in those cases. We're about two decades past people deploying the trite "Oh well that's just legacy inertia..." argument.
Google didn't write Fuchsia in Go, and hasn't used an iota of it in ChromeOS or Android. Microsoft uses somewhere approximating 0% .NET/C# code in the Windows core. Microsoft research made a toy .NET OS once that, unsurprisingly, went nowhere, just as you can find irrelevant toy example projects pushing the same premise, but they're dead ends.
This is of course meaningless for the vast majority of projects that the residents of HN are working on, where the compromises of Go, C#/.NET, GCs, etc, are massively outweighed by the benefits they bring to those sorts of projects.
>I am more than happy to do bare metal coding in C# (Meadow) and Go (TinyGo) without having to deal with Linux.
Okay?
My friends writing safety critical bare metal industrial control software using Ada would, I suspect, feel this was a somewhat limited perspective.
If you ever do consider writing such volumes, I would recommend getting co-authors to cover the areas you're less familiar with.
As for the rest it would be a waste of my time on Earth to prove you wrong and change your mind.
Okay?
It's weird to crown Go as better when it is nicer simply by ignoring very low-level work then pretend that the argument of OS development is irrelevant.
A bit of an exaggeration, but its like saying HTML is a better "programming" language than javascript when creating documents, then ignore the fact HTML isn't actually programmable.
Oh sure, the only explanation is that people feel threatened and insecure; the language is totally perfect and its admirers have never wasted anyone's time or demanded that other people blindly drop everything and start porting to their favorite language.
> Rust today seems like a 2013 Tesla Model S. A couple of years ago it was the GM EV1: It used to be largely promise of a future to come with a loads of downsides, where today it's expensive to get started with, has a lot of incompatibilities, but it's superior to most alternatives. Tomorrow it and those like it will be the norm.
What a beautiful analogy, considering that https://en.m.wikipedia.org/wiki/Electric_car_use_by_country puts electric cars at 4.6% of all cars sold in 2020.
I've been through enough waves of asshole fan boys for different things now that I can mostly tune them out, but they're basically never the people actually getting things done so even on the days I can't tune them out I try not to let them reflect badly on the project.
Similarly, Rick and Morty is still funny to me even if I do basically have to block any vocal fan of it I encounter on twitter.