3 ms·
> The nested functions usually get inlined by the compiler, so there is no cost to them. This kind of code typically gets written when ‘usually’ isn’t good eno
by Someone 4y ago
> The nested functions usually get inlined by the compiler, so there is no cost to them.
This kind of code typically gets written when ‘usually’ isn’t good enough (of course, once you use a compiler, in theory, there are no guarantees; the compiler could compile the inlined-function one with a goto or vice versa, but programmers typically are more concerned about what happens in practice)
The inlined functions also may increase code size and instruction cache pressure.
On the other hand, having a branch less may be beneficial.
- Groxx 4y agoI agree that programmers care more about what actually happens (and they should, when performance matters!), but this kind of analysis also involves future changes to the compiler unless it's a one-time job. Which sometimes exists, so check the assembly there and do whatever you need. Straightforward and limited-scope code like nested functions tends to improve in performance over time, because it restricts possibilities better than goto. And it's more error-resistant to future changes for similar reasons. If your code has to last a while, you're probably better off having the safer one. Or maintain both, and use the safer one to validate the unsafe one, and choose based on benchmarks of the week - what was true when it was written could change with any version.
- WalterBright 4y agoWhen it isn't good enough, inserting `pragma(inline, true)` will force it. > The inlined functions also may increase code size and instruction cache pressure. tail merging optimization takes care of that