News for hackers. Stuff that matters.
Re:C meeting is over. C23 added: by kaba0 on Saturday February 19, 2022 @06:07AM (#30396179)
C isn’t that small, compare it to a Zig hello world. Fast is relative — due to C not having good expressive/abstracting powers, it will leave you to inferior solutions, eg. counting string length multiple times at call sites, vs C++’s small string optimization, which is simply not possible in a user-ergonomic way in C. Regarding obviousness, I would add UB here, so Zig for example would beat it.

C is not any closer to the metal than other system level programming languages, this myth should just die. Your code at -O3 gets mangled to oblivion, unless you are a compiler writer of the respective compiler, you will have no idea on the generated code. This is the exact same as with C++, Rust, etc, hell, C doesn’t have proper simd support, so in a way the former two is closer to the metal.

But I do agree on portability and integrations - so C will not die, but I really have a hard time seeing why should I choose C over any of the listed languages, unless I target some obscure CPU architecture.

Re:C meeting is over. C23 added: by 10x-dev on Saturday February 19, 2022 @06:43AM (#30396371)
Are you going to mix all those languages into one project and somehow use their advantages but steer away from their disadvantages?

Sure, other languages have caught up or have improved on some of the features where C shines.

Let's remove portability and integration from the feature list, because that's strongly related to C's tenure.

Which one of the languages you listed matches the rest of the feature set I brought up?

  * Small language
  * Fast compile times
  * Fast binaries
  * Small binaries
  * Great debugging experience
  * Close to the metal
In my opinion, they all fail in at least one category, and that's expected - additional functionality can't come for free. You can only accept its cost.
Re:C meeting is over. C23 added: by foxfluff on Saturday February 19, 2022 @08:59AM (#30397159)
And they fail in other categories too, e.g. C can be very terse whereas most modern so-called replacements tend towards verbose. For me that's a development exactly in the wrong direction; I'd rather have a more terse C.
Re:C meeting is over. C23 added: by xigoi on Saturday February 19, 2022 @01:19PM (#30399185)
Zig? I personally haven't used it, but based on what I've heard about it, it fulfills these criteria.
Re:C meeting is over. C23 added: by chaz72 on Saturday February 19, 2022 @04:09PM (#30400551)
That’s what I am betting on - but it isn’t 1.0 yet, and that matters too.
Re:C meeting is over. C23 added: by dzaima on Saturday February 19, 2022 @07:24AM (#30396609)
In C it's possible (and, to some level, very much encouraged) to write your own things to replace whatever built in thing you don't like for whatever reason, be it speed, size, portability.

C not having fancy built-in structures means programmers are more careful about choosing simple ones, which dramatically cuts down the amount of completely pointless generated code. -O3 can optimize registers and maybe memory reads/writes, but it can't remove reallocation from each call to a vector push, remove a reference count from a whole object type, or do pretty much anything with any moderately complex heap data structure. Sure, that comes at the cost of more advanced things being horrible to write, but it's a trade-off.

And I'll just disagree that UB must be unobvious. To me, it's one of the most useful optimization tools. You can check for it with sanitizers, and explicitly invoke it to convey information about hidden behavior to a compiler. It's not magic.

Re:C meeting is over. C23 added: by kaba0 on Saturday February 19, 2022 @10:59AM (#30398031)
> C not having fancy built-in structures means programmers are more careful about choosing simple ones

Resulting in linked lists everywhere which have pretty terrible performance characteristics. You should be worried about those way before the occasional vector push reallocation cause you any problem. (And you can specify initial capacity so there is that)

Re:C meeting is over. C23 added: by dzaima on Saturday February 19, 2022 @11:32AM (#30398311)
linked lists - true, but at least when you write them you'll be certain of what they do, and, coupled with knowledge of cache performance, precisely how bad they are. About reallocation I don't even mean the copying, but just the code needing to check for the chance overflow on every push. Besides the obvious waste of instructions to check for that, it also clobbers registers even if the branch isn't taken, and thus will easily result in a lot more unnecessary spilling to the stack. This kind of pointless thing happens in a ton of data structures.
Re:C meeting is over. C23 added: by kaba0 on Saturday February 19, 2022 @02:52PM (#30399935)
Branch prediction exists so the common case can be made very fast (much faster than jump to this remote pointer), and also, rust uses an optimizing compiler for a reason. Eg., you use a vector inside a for, the vector’s code can be inlined and the checking part be done only at the boundary. Also, one can also manually perform an unchecked push/get/whatever as well.
Re:C meeting is over. C23 added: by dzaima on Saturday February 19, 2022 @04:21PM (#30400647)
The common case may be fast, but it's still slower than not having a rare case in the first place. The untaken branch will still affect register allocation (out of the ~15 x86-64 general-purpose registers, only 5 (6 if you count rbp) preserve data after a non-inlined function call in the System-V ABI (of which there will be at least one, ending in malloc somewhere), and the compiler has to accommodate both taken and untaken at the branch end) and will eliminate any hope of SIMD vectorization, among other wrecked optimizations.

Unchecked operations are acceptable if your code has a single hot loop, but if you have a hundred small functions, each taking 1% of the time, you probably won't carefully examine every stdlib function each of them uses and write code to "work around" every unnecessary thing the stdlib does.

Yes, that's a ton of micro-optimization, but micro-optimization can bring a ton of speedup, so I'll take whatever makes it simpler (or not needed in the case where you already know what will happen due to having written it)

Re:C meeting is over. C23 added: by kaba0 on Saturday February 19, 2022 @05:48PM (#30401262)
Feel free to wrap it into a repr(transparent) type which exposes the unsafe operations by default.

But I seriously doubt that programs would benefit much from these micro-optimizations - there are rare and unfortunate cases where indeed there is no one single bottleneck for a program and thus there is no simple way to improve performance, but the vast majority of programs spend all their life in a tight hot loop, and anything else doesn’t matter in the slightest - hell, as mentioned C gets away with as many linked data structure as they want, but writing those in goddamn bash would suffice as well.

Re:C meeting is over. C23 added: by dzaima on Saturday February 19, 2022 @06:04PM (#30401372)
That depends on the project. There definitely are projects in which micro-optimizations such as this wouldn't be beneficial for one reason or another, but there definitely also are ones where they're make-or-break. For those, C is pretty good, and you're not fighting against the language (or the core intent of the language at least).

(I'd guesstimate that micro-optimizations would go a very long way in improving performance of nearly every program anywhere, but I don't have much data on that besides the couple projects where I've tried to care about performance, and having achieved pretty decent results)

Re:C meeting is over. C23 added: by deeviant on Sunday February 20, 2022 @01:09AM (#30403617)
I don't think I've ever used a linked list in 12 years of embedded C programming.
Re:C meeting is over. C23 added: by moralestapia on Saturday February 19, 2022 @10:31AM (#30397817)
>due to C not having good expressive/abstracting powers

I disagree, if anything, C's scantness forces you to abstract things much properly, unless you plan to write pages of boilerplate code here and there.

Re:C meeting is over. C23 added: by kaba0 on Saturday February 19, 2022 @05:39PM (#30401206)
With all due respect - how do you write a string library in C then? Char* is not one.
Re:C meeting is over. C23 added: by moralestapia on Sunday February 20, 2022 @01:25PM (#30407964)
An appropriate data type (you could use structs maybe) and methods for the common operations, concat, length, etc...

It's not as trivial "string s" like in other languages, but it's also not that big of a deal. Also, you make sure it works properly and you just use it anywhere you want.