News for hackers. Stuff that matters.
Re:C and C++ Prioritize Performance over Correctness by kelnos on Friday August 18, 2023 @01:51PM (#37179491)
I think it's a bit out of line to call this malice.

The C standard was written when people wanted a language that was easy to read and write, but could have similar performance to hand-written assembly. Performance, as the article title points out, was a bigger priority than correctness or eliminating foot guns.

Most of us here weren't around when many/most programs were written in assembly, and when machines were very constrained in how much memory they had and how many CPU cycles they could spare. Nowadays, performance concerns usually come behind speed and correctness of development. So it's hard to truly feel what trade offs the original designers of C (and the original C89 standards committee) had to make.

I'm not convinced implementation-defined behavior is really any better, though. Packagers and end-users should not have to ensure that the code their compiling is "compatible" with the compiler or hardware architecture they want to use. Developers shouldn't have to put a "best compiled with FooC compiler" or "only known to work on x86-64". That would not be an improvement.

I do agree that, these days, there's no excuse for writing language specifications that include undefined behavior. But C has a lot of baggage.

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Friday August 18, 2023 @02:14PM (#37179829)
That is usually a myth.

From the people that were there at the time.

"Oh, it was quite a while ago. I kind of stopped when C came out. That was a big blow. We were making so much good progress on optimizations and transformations. We were getting rid of just one nice problem after another. When C came out, at one of the SIGPLAN compiler conferences, there was a debate between Steve Johnson from Bell Labs, who was supporting C, and one of our people, Bill Harrison, who was working on a project that I had at that time supporting automatic optimization...The nubbin of the debate was Steve's defense of not having to build optimizers anymore because the programmer would take care of it. That it was really a programmer's issue.... Seibel: Do you think C is a reasonable language if they had restricted its use to operating-system kernels? Allen: Oh, yeah. That would have been fine. And, in fact, you need to have something like that, something where experts can really fine-tune without big bottlenecks because those are key problems to solve. By 1960, we had a long list of amazing languages: Lisp, APL, Fortran, COBOL, Algol 60. These are higher-level than C. We have seriously regressed, since C developed. C has destroyed our ability to advance the state of the art in automatic optimization, automatic parallelization, automatic mapping of a high-level language to the machine. This is one of the reasons compilers are ... basically not taught much anymore in the colleges and universities."

-- Fran Allen interview, Excerpted from: Peter Seibel. Coders at Work: Reflections on the Craft of Programming

Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @02:21PM (#37179937)
I fail to see to the point of your post in context of this thread.

What, in the parent post, do you consider a myth?

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Friday August 18, 2023 @02:28PM (#37180028)
C's original performance.

Optimization taking advantage of UB had to be introduced during the 1990's to make it into a reality.

Until the widespread of 32 bit hardware, even junior Assembly devs could relatively easy outperform C compilers.

Which is what the interview is about, originally C lacked the optimization tools, relying on developers, with tons of inline Assembly.

Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @02:44PM (#37180261)
> Which is what the interview is about, originally C lacked the optimization tools, relying on developers, with tons of inline Assembly.

I'm afraid that is not the conclusion I draw from the snippet you posted.

It's very clear to me that the HLL under question at that time prevented the programmer from performing the low-level optimisations that the C programmer could optionally do. The debate appeared to be be whether to let the language exclusively optimise the code, or just do no optimisation and let the programmer do it.

This is why the sentence ": Oh, yeah. That would have been fine. And, in fact, you need to have something like that, something where experts can really fine-tune without big bottlenecks because those are key problems to solve. " is in there - it's because the HLL languages literally wouldn't let the programmer optimise at the level that C would let the programmer optimise.

Honestly, if the proponent for the HLL language didn't add in "Use the HLL, but not for OS and low-level code where you really need to fine-tune", you'd have a point, but they said it, and so you don't.

Re:C and C++ Prioritize Performance over Correctness by pjmlp on Friday August 18, 2023 @04:03PM (#37181337)
Right, that is why C code was polluted with inline Assembly until optimizers started taking advantage of UB.

Inline Assembly isn't C.

Re:C and C++ Prioritize Performance over Correctness by memefrog on Friday August 18, 2023 @08:44PM (#37184168)
Of course inline assembly is C. Most languages don't let you do inline assembly. That isn't because they don't like it, but because the way those languages get translated to machine code is highly variable. C is the only language where the semantic model is simple enough that you can see how every language construct would translate to assembly language and write your own inline assembly language in between.
Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @02:59AM (#37186275)
It definitely isn't.

What page of C standard describes it?

As for the rest of your comment, unless you are using a PDP-11, you will be surprised.

Re:C and C++ Prioritize Performance over Correctness by memefrog on Saturday August 19, 2023 @03:21AM (#37186374)
C predates the C standard, which describes a common subset of C implementations. It is useful for writing code which works across a variety of implementations. For example, you can write code that works on the Byzantine optimising compilers, which will attempt to render your code incorrectly if at all possible, but will make strictly conforming programs run very quickly. But it is not necessary to write code to the C standard, and many C implementations do not conform to it. The Plan9 C compiler, for example, expects 'void main(void)' for a program's main function, while the standard requires 'int main(void)'.
Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @06:19AM (#37187267)
Lets make it easier then, what page of K&R C describes inline Assembly?
Re:C and C++ Prioritize Performance over Correctness by memefrog on Saturday August 19, 2023 @06:36AM (#37187360)
[delayed]
Re:C and C++ Prioritize Performance over Correctness by pjmlp on Saturday August 19, 2023 @10:04AM (#37188900)
Likewise compiler specific extensions don't define universal truths about languages, that aren't part of the respective standard.

Had you been clever, you would have pointed out that inline Assembly is defined in the ANSI C++ standard, with the mention of asm (content) language construct, leaving the meaning of content implementation defined.

As it is also defined in the Ada language standard, Modula-2 standard revision, and Oakwood Guidelines for Oberon-2 implementation.

That is the thing mixing up C with compiler specific features, confusing the language for its standard and assuming no other language in the planet offers similar capabilities.

Re:C and C++ Prioritize Performance over Correctness by memefrog on Saturday August 19, 2023 @10:16AM (#37189021)
I am not remotely interested in what the ISO standard says about C, and I am even less interested in what the ANSI standard says about C++, a completely different language.

C is not defined by or constrained to the standard. It predates it and every implementation extends it, as is intended by the standard.

The person confusing the standard with the language is you.

Re:C and C++ Prioritize Performance over Correctness by defrost on Saturday August 19, 2023 @06:36AM (#37187361)
The more important detail is which "standard C" recognises inline asm as a common as dirt C extension.

Page 514 (of version n1256) of the ISO C Standard

Moving target, to be, sure, but it's been there for 20+ years now.

Annex J.5.10 The asm keyword

to quote a stack overflow answer (that sounds a lot like an answer commonly given back in the day on Usenet and #C IRC channels)

     informative, not normative, so an implementation need not provide inline assembly, and if it does it's not prescribed in which form. But it's a widespread extension, though not portable since compilers do indeed implement it differently.
https://www.open-std.org/JTC1/SC22/WG14/

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

Re:C and C++ Prioritize Performance over Correctness by aidenn0 on Saturday August 19, 2023 @01:22AM (#37185786)
My recollection is it was trying to keep up with Fortran's performance on numeric code as an impetus for taking advantage of UB (variations on what eventually became the "restrict" keyword were discussed but rejected for c89 and that idea comes directly from Fortran, which can assume arguments do not alias)
Re:C and C++ Prioritize Performance over Correctness by lelanthran on Friday August 18, 2023 @02:01PM (#37179630)
> I think it's a bit out of line to call this malice.

I apologise, but note (warning: weasel words follow) I was careful to say it looks like malice to me, not that I have any indication that it actually was malice.

> I'm not convinced implementation-defined behavior is really any better, though. Packagers and end-users should not have to ensure that the code their compiling is "compatible" with the compiler or hardware architecture they want to use.

I think it is better, purely because then the resulting code can't simply be omitted when an integer may overflow, the code still has to be emitted by the compiler.

Right now all the worst bits of UB has to do with the compiler optimising out code. With IB replacing UB, the implementation will have to pick a behaviour and then stick with it. Much safer.