Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There's a cost to loading more instructions, especially if you have more types of instructions.

The main advantages to inlining are (1) avoiding a jump and other function call overhead, (2) the ability to push down optimizations.

If you execute the "same" code (same instructions, different location) in many places that can cause cache evictions and other slowdowns. It's worse if some minor optimizations were applied by the inlining, so you have more types of instructions to unpack.

The question, roughly, is whether the gains exceed the costs. This can be a bit hard to determine because it can depend on the size of the whole program and other non-local parameters, leading to performance cliffs at various stages of complexity. Microbenchmarks will tend to suggest inlining is better in more cases that it actually is.

Over time you get a feel for which functions should be inlined. E.g., very often you'll have guard clauses or whatnot around a trivial amount of work when the caller is expected to be able to prove the guarded information at compile-time. A function call takes space in the generated assembly too, and if you're only guarding a few instructions it's usually worth forcing an inline (even in places where the compiler's heuristics would choose not to because the guard clauses take up too much space), regardless of the potential cache costs.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: