Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The 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.


Languages 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.


Restrict 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?


The 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.


Oh ok, I haven't ever used Fortran. I'm just going from what I have read.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: