Hacker Newsnew | comments | leaders | jobs | submitlogin
Slides from Felix von Leitner's talk on C and compiler optimizations [scribd] (linux-kongress.org)
32 points by nrr 95 days ago | 6 comments


5 points by yan 95 days ago | link

Hm, regarding the tail-optimization slides, the example used doesn't look like a tail-recursive call. I just tried compiling the function he provided using gcc's -O1, 2 and 3, and neither produced a tail optimized call. I rewrote it to match what my idea of a tail-recursive call is, and sure enough, under -O2 and -O3, gcc tail-optimized it. It didn't however with no optimization settings or -O1. I rewrote it as follows:

  long
  fact(long x, long acc)
  {
     if (x <= 0) return acc;
     return fact(x-1, acc*x);
  }
(edit: the whole slide deck is summarized well on the last slide: "If you do an optimization, test it on real world data. If it’s not drastically faster but makes the code less readable: undo it.")

-----

3 points by pauldino 95 days ago | link

I think it depends on the version of gcc; I tried compiling his function with 3.4.5 and it didn't look tail-call-optimized, but with -Os on 4.2.3 it did.

-----

1 point by keosak 95 days ago | link

You are right that it does not look like what is usually called "tail call", ie. call to 'fact' is not the last evaluated expression. However, this doesn't mean that the compiler can't optimize it, provided that the last expression is simple enough. GCC can probably optimize arithmetic expressions. I have read somewhere (can't find it though) that Erlang bytecode compiler also optimizes prepending to a list and some other primitive operations. This means that in these cases you don't have to explicitly use accumulator variable because the compiler will create it for you.

-----

2 points by logic 95 days ago | link

It appears that he's been updating this presentation over the years:

http://www.fefe.de/know-your-compiler.pdf http://dl.fefe.de/optimizer-isec.pdf

Regardless, it's a good read!

-----

1 point by aw3c2 95 days ago | link

Are video/audio recordings available too?

-----

1 point by nrr 95 days ago | link

... not that I've been able to tell. At least, they aren't posted yet.

-----




Lists | RSS | Bookmarklet | Guidelines | FAQ | News News | Feature Requests | Y Combinator | Apply | Library

Analytics by Mixpanel