4 ms·
Inlining is what can mostly mitigate this. You'd write: foo() { // do something return foo0(); } foo0() { // do the rest of the things }
by amalcon 3y ago
Inlining is what can mostly mitigate this. You'd write:
foo() {
// do something
return foo0();
}
foo0() {
// do the rest of the things
}
If you can convince the compiler to inline foo0 into foo, then you get almost what you want. The compiler technically could even use the same code for both (saving some binary size and thus RAM, thus getting the exact same result), though AFAIK this sort of optimization is unusual.
- kccqzy 3y agoNot inlining, but what I've seen in the real world is that the function foo ends with something like a "jmp foo0" so everything is good. (This jmp is almost free with 100% branch prediction.) No need to inline. Just do a proper tail call optimization. Without symbols you can't tell whether they are two functions or just basic blocks in a single function.
- yencabulator 3y agoAn extra gimmick is that it is often better to inline foo into its caller than foo0 into foo. For example, in the following that saves a function call cost on every nil call. fn foo(bar) { if bar != nil { foo0(bar) } }