c++ - Are macros the only way to force inline -
i have class member function critical path in application. must fast possible application deliver expected (read: rather hoped) overall performance.
the function rather complex have identical sections repeated several times. like:
if (condition) { //... code } if (another condition} { //... identical code may change condition } if (condition) { //... code (same above) } // , on
in order make code easier read, understand , maintain break down , use function calls instead. like:
if (condition) { some_function(some_param); } if (another condition} { some_function(some_other_param); } if (condition) { some_function(some_param); }
i can't afford overhead calling functions make sure some_function
inlined - always.
i searched , read several post discussing similar issues not same. these post indicates way macro
.
i hate use macros
on other hand hate complexity of current function. choosing between 2 evils.
is so? macros way achieve this?
update
i got lot feedback (thanks).
nearly answers suggest shall not worry/care forcing function calls inline compiler the best me anyway.
so decided go , make test. rewrote code use function calls whenever code snippets reused , ended more readable (and maintainable) version.
then tested new code , old code 100 test runs , calculated performance average. in average performance of new version ~1% (a little less .. 0.88%) lower old code. there performance hit. on other hand - performance hit not high expected.
my conclusion prefer new code easier understand. means easier maintain, debug , hand-over others. i'll have find lost performance gain in other code block.
oh, last thing - answer accept? don't know. answers have been helpful input. 2 answers address original question. me seem equally i'll take 1 posted first.
well, macro guaranteed way inlined code.
you declare c++ function using inline
keyword. has implications in terms of scoping, compilers it's hint compiler should try generate the function's code inline caller.
whether or not compiler that, separate issue. may or may not require enabling compilation optimization options.
you should try compiling inline
functions, examining resulting code see if compiler inlined function calls you.
Comments
Post a Comment