Inlining functions #62
Replies: 3 comments 1 reply
-
Without good benchmarking it's hard to say in any case what costs and benefits manual inlining has on the execution size and time of our library. Here are my thoughts in a somewhat unorganised fashion. Thoughts about inliningInlining a function within a single translation unit is nice because you know in advance how many times it could be repeated. But for public inline functions, we can only guess what the end-user will do. For concerns about speed, it's worth thinking about what operations might be called once or twice in a high-frequency control loop, and which operations will be called many times across the code base. The former usage (high-frequency in a few locations) would probably benefit from inlining. The latter case (low-frequency in time but high-frequency in the logic) would probably not benefit from inlining. But, because the compiler is always free to ignore the inlining directive, I suppose neither case can ever be worse as a result of inlining. Conversely, the compiler is also free to inline a function that is not inlined, provided its definition is available outside of the translation unit. So, the question really is about where potential gains would warrant putting a function definition in the header file, and that would be in the first case (high-frequency usage). This leads into getters and setters. I agree that simple calls would likely be optimised inline by the compiler if they are available. For pointers and parameters, I agree that Assuming that we can maybe get some marginal performance gains from inlining some implementations in the header, this leads to the next question about "header bloat". More files?I tried to think of some reasons why it's good reduce the size of header files:
There is something nice about proposing a new file type for this case. There are some additional counterpoints to the above reasons:
So if Some kind of conclusionI think the main step is to re-evaluate and reduce public methods, starting with simplifying getters/setters. This will already reduce header size and the amount of inlining keywords. Beyond that we might want some performance benchmarks (not to be run on every PR but as an occasional option, perhaps on each release) so we can actually see where efforts to optimise and "outsmart" the compiler are actually warranted. But probably the consequences to moving some of the function implementations into the class source files are not going to be so significant. |
Beta Was this translation helpful? Give feedback.
-
Interesting thoughts as always, I'm following this discussion closely but I can't add something valuable because I have no experience with that 😄 |
Beta Was this translation helpful? Give feedback.
-
I found another potential issue with inlining functions - see #75 The problem may be unrelated to the inline, I will need to do some more tests, but it seems plausible to me |
Beta Was this translation helpful? Give feedback.
-
There was a discussion regarding inlining functions in PR #52. I checked a bit more and, indeed, functions that are defined in a separate source file are never inlined. So the question becomes then do we think inlining might be useful or not. I can see for getter and setters but it seems performances are not that much improved.
If we want to inline functions but do not want to cluter the actual header file, one practice seems to be the usage of a
.inl
file https://www.oreilly.com/library/view/c-cookbook/0596007612/ch02s06.html. This could also be used for template implementations. What are your opinions on that?Beta Was this translation helpful? Give feedback.
All reactions