4 ms·
Discussed a couple years ago here: https://news.ycombinator.com/item?id=5037089 https://news.ycombinator.com/item?id=5037089 And the followup discussed here: h
by nkurz 12y ago
Discussed a couple years ago here: https://news.ycombinator.com/item?id=5037089 https://news.ycombinator.com/item?id=5037089
And the followup discussed here:
https://news.ycombinator.com/item?id=5075370 https://news.ycombinator.com/item?id=5075370
I repost because I'm becoming more and more frustrated with C, and wondered if others feel the same. Not because it's too low-level, rather because it seems to be trying (with limited success) to move to being a higher level language. There are times when I really do want a "thin layer over assembly", and C seems to have evolved to a place where that is no longer possible. I explain my distress more completely in the comments of Yann's article here: http://fastcompression.blogspot.com/2014/11/portability-woes-endianess-and.html http://fastcompression.blogspot.com/2014/11/portability-woes...
- pgeorgi 12y agoBefore reading the comments on that article I knew that it's about C compiler authors (ab)using properties of the C language for optimizations (and other code-breaking behavior). Properties that some language lawyers cooked up back in the stone age to keep all compilers on all architectures compliant without further effort for developers, which is typical design by committee cruft to get people to buy in on the standard.
- kazinator 12y agoIf C was ever a thin layer over assembly, it stopped being that sometime in the 1980's, not recently. C has not radically changed since the 1989 ANSI C standard. The library is bigger and there are some bells and whistles in there like designated initializers, structure literals, complex numbers and (now optional again) variable-length local arrays. The trick of abusing C as a higher level assembler that are undefined behavior today were already undefined behavior already in 1980-something, and before that there was no standard at all, other than the K&R1 book.
- alayne 12y agoThe best optimizing compilers are C and C++ compilers (as far as I know), perhaps Fortran but even that is getting more esoteric. I think that's why the assembly idea persists.
- andrewchambers 12y agoLanguages which don't have pointer aliasing, but use gcc back ends are probably the fastest around. So I think gcc based fortran. C can work around this using restrict keyword though.
- kazinator 12y agoRestrict is a big hammer. You can work around aliasing suspicions simply by imagining you are programming a load-store machine whereby local variables are registers. You have some algorithm that works with several different objects, such as structures and arrays. You can help the compiler (and the reader!) by writing code that isn't repeatedly dereferencing pointers, but which loads values into local variables. Concrete example, doubly-linked list deletion, bad: victim->prev->next = victim->next; victim->next->prev = victim->prev; Here, the compiler doesn't know whether the assignment to victim->prev->next clobbers victim->next, which is evaluated again in the second line. If you write it like this, you eliminate that concern: node *prev = victim->prev, *next = victim->next; next->prev = prev; prev->next = next; The assignments here do not interfere; the first line assigns to a "prev" structure member, and the next line doesn't access any such thing; it uses the "prev" local variable which isn't a structure member. Caching in local variables is preferrable to using some C99 "restrict" voodoo that introduces undefined behavior. In the case of the above linked list, where are you even going to stick "restrict"? There is only one incoming pointer, victim. Making that "restrict" doesn't help. You need it in the actual structure declaration on the next and prev pointers. Yuck, what?
- dagw 12y agoThe gcc Fortran compiler is unfortunately pretty far behind the state of the art (or at least was a couple of years ago). To really realize the true potential of Fortran you unfortunately need to get a commercial compiler.
- andrewchambers 12y agoOh ok, I haven't ever used Fortran. I'm just going from what I have read.
- nkurz 12y agoIf tracking changes in the specs, you'd be right, but I'm referring more to the mindset. And I don't mean to imply this is anything sudden --- if pressed, I'd probably roughly gesture in the direction of 1998-2000, possibly related to a slow "changing of the guard". In this incredibly long but fantastic thread, Anton Ertl points to GCC 2.9.5 as the turning point. http://compgroups.net/comp.arch/if-it-were-easy/2993157 http://compgroups.net/comp.arch/if-it-were-easy/2993157 (search for Ertl). His rhetoric might be excessive, but he makes a good point that in practical terms, his research on "threaded interpreters" went from being possible to do in C to impossible. I'm also not referring to syntax --- I think many of the syntactical improvements are fantastic and have no downside. And the reliability of modern compilers is much better, and by-and-large the optimizations produce smaller and faster code. But what I think we're losing is the transparency of being able to reason about code execution by looking at source code. It's still the norm for academic papers to offer source for two approaches to a problem, and use execution time to prove that one is faster than the other because "it has more operations" or "it has more memory accesses", without ever looking at the generated assembling and noticing that one approach has been vectorized and unrolled, and the other has substituted a completely different algorithm that drops all the conditionals because the compiler realized they were never being exercised. And I don't think it's simply a matter of programmers exploiting undefined behavior without properly considering the consequences. The inclusion of the standard library into the spec for the language creates "spooky actions at a distance" that never existed before, where passing a variable to "print_debug(a)" allows the compiler to remove the later check for "if (a == NULL)" if print_debug() is observed to pass the variable to printf() and if whole program optimization is being used. Whereas if print_debug() is in a compiled shared library, this won't happen, and the null-check will work as intended. I see the reasons compiler writers want this flexibility, but it sure makes development and debugging much harder than it used to be.
- kazinator 12y agoSummary: about a decade ago or so, Ertl ran into some regressions in GCC uncovered by benchmarking his Forth implementation, logged bugs for them, and they were fixed. Some of them were in obscure GCC extensions, like notably computed goto. The compiler started generating code involving branches to a common instruction that does the actual indirect jump, instead of just doing that indirect jump. It's easy to understand how a performance regression affecting such a feature can slip through, if the behavior is otherwise correct. It doesn't mean that the GCC sky is falling, as all the trolling in that newsgroup would have you believe.
- cpeterso 12y ago> If C was ever a thin layer over assembly, it stopped being that sometime in the 1980's, not recently. Yes, if C were still a "thin layer over assembly", then cc -O1 and -O3 would not generate such different object code. :)
- twelvechairs 12y agoNot so long ago C was the 'high level language' to assembly. Now few people use assembly and C is 'low level'. Its a weird transition to make. There's still plenty of people around who want to use C for high level tasks.
- userbinator 12y agoYou might be interested in this: http://blog.regehr.org/archives/1180 http://blog.regehr.org/archives/1180 (discussed here: https://news.ycombinator.com/item?id=8233484 https://news.ycombinator.com/item?id=8233484 )
- deleted 12y ago[deleted]
- throwawayaway 12y agoIf I follow exactly what you are saying, you should say it's C coders who rely on undefined behaviour and compiler writers you are frustrated with, not the language itself.