4 ms·
I wonder why they use goto statements instead of just returning q1 like the statement evaluates to.
by gigatexal 10y ago
I wonder why they use goto statements instead of just returning q1 like the statement evaluates to.
- gakada 10y agoIt's more DRY.
- Pica_soO 10y agoNo function call overhead. Makes sense as long as you stay in the same state-machine. This thing doesn't have to be pretty. It has to be fast. Who cares for any oo-written implementation that takes half a hour to do the same job?
- nodesocket 10y agoBut there is no function call overhead right? Replacing goto OUT_FULL; with return q1; would seem to be faster right? No need for a goto, just invoke return. What am I missing?
- valarauca1 10y agoThere is some. A few register need to be assigned, and everything else gets pushed/copied onto the stack. Then you jump to a location and copy everything back off the stack. This doesn't take many cycles, but it does take some. While a GOTO is just a jump.
- xxs 10y agothe compilers are free to optimize. Creating a stack frame for each function is trivially avoided by inlining (esp. trivial for non-polymorph call sites)
- valarauca1 10y agoIt depends. In GCC/MSVC will only (attempt to) inline what you mark as inline. Then MSVC has a keyword which forces inlining. Unless you set a flag which tells the compiler to inline what ever it wants. But that being said Microsoft has a non-POSIX x64 ABI designed to allow better in-lining. How inlining works starts to dive pretty deep into the compiler rabbit hole.
- lorenzhs 10y agoAre you sure? This does not match my experience. Rather, compilers inline stuff if they think it's a good idea, and treat the "inline" keyword more like a hint.
- honkhonkpants 10y agoThis is not true. Gcc will inline everything it damn well wants to. It will definitely inline any function that is smaller than a function call, like small C++ accessors, and any other small function available at compile time like trivial constructors and the like. The "inline" keyword mainly serves to avoid ODR violations, not to inform the optimizer.
- xxs 10y agoExported functions are entirely different than vast majority of the code, the exported ones do need call conventions, so they can be linked. But it's entirely not true that ONLY functions marked as 'inline' would be inlined. You can also direct GCC to try to integrate all “simple enough” functions into their callers with the option -finline-functions (includeded in -O3).[0]. O2 includes inline small functions [1]: [0]: https://gcc.gnu.org/onlinedocs/gcc/Inline.html https://gcc.gnu.org/onlinedocs/gcc/Inline.html [1]: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
- astrange 10y agoFunctions don't need to follow the ABI unless they're externally visible. If you use -fvisibility=hidden GCC will do whatever it wants for each function as it sees fit. Interprocedural register allocation can work better than inlining too, because it keeps the code size smaller, and direct calls have almost no speed penalty.
- eru 10y agoWhat does return have to do with OO? (If you want to be glib, complain about misplaced FP perhaps?)
- eru 10y agoThey seem to like a style that has rather constrained use of `return' (but uses deliberate amounts of goto instead). Not sure why.
- rusanu 10y agoI assume is "one return only per function" rule http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from http://programmers.stackexchange.com/questions/118703/where-...
- eru 10y agoNot quite, there are multiple returns, still. But might be something related, eg like they are talking about a state machine in the description somewhere, and give at most one return per state, or something like that?