Slides from Felix von Leitner's talk on C and compiler optimizations(linux-kongress.org)
linux-kongress.org
Slides from Felix von Leitner's talk on C and compiler optimizations
http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf
3 comments
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.
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.
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!
http://www.fefe.de/know-your-compiler.pdf http://dl.fefe.de/optimizer-isec.pdf
Regardless, it's a good read!
(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.")