Skip to main content
GameDev.net gamedev.net
🔒 Locked

Inline functions

Started by FrigidHelix Jan 20, 2001 at 4:20 PM 8 replies 650+ views
Original Post
FrigidHelix
FrigidHelix
Are inline functions worth it, or am i better off not using the inline keyword for small functions. I have about 10-40 1 line functions (most are just an if/else statement, or return data from a class) which are called each loop through my game. Would it be beneficial to give them the inline keyword?
mhkrause
mhkrause
How much of the total execution time does the profiler say they are taking up?
FrigidHelix
FrigidHelix
I dont know. I have never before used a profiler.
mhkrause
mhkrause
Learn to use a profiler. See how much time they take to execute, see if there is a benefit to inlining them.
ziplux
ziplux
Aren''t profilers expensive? Are there any free ones?


My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t X
R- tv+ b++ DI+(+++) D- G e* h!"
Decode my geekcode!
Geekcode.com


Visit our web site:
Asylum Entertainment
evaclear
evaclear
Inline functions have some great features. For C++ programmers they are realy ment to replace Macros. Macros have some common problems for instance a macro doesn''t type check the input. This could lead to large problems if you make assumptions about the types of data you send into the macro. Inline functions do check the type data to make sure it matches what you want. One of the other major problems with macros is the more extensive the macro is the harder it is to debug, and this often leads to major software errors. Inline functions can suffer from the same problems however if you comment them well it''s easier to deal with.

Now on another note, the keyword inline is only a suggestion to the compiler. If your inline function calls a normal function the compiler will not inline it. This is because your calling a non inline function from with in an inline function which offers little to no speed improvement. Other cases may exhist for the compiler rejecting the inline suggestion but I don''t know of them right off the top of my head.

So where are inline functions useful? Well they are pretty useful for your class get & set functions, and just about anything that doesn''t need to call functions like tan, cos, ect.. Granted your probably not going to notice a major speed increase however it will give you more CPU play time to deal with other things like your rendering and the like. This is an early optimization that doesn''t need to be done however it''s a good train of thought to get into.
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
Tooon
Tooon
A agree to the above post, inline functions are great but you will have to remember that the keyword "inline" is just a suggestion, or a hint, to the compiler.

And. of course you can benefit from them, you will get rid of at least 4 stack operations and 1 functioncall per function.

So, lets learn about them
An inline method has to follow some "rules".
1. It shouldn''t contain anyloops
2. It should be kept small (1-5 lines +- 4)
3. No recursive calls inside function ie) not call itself
... and more (these are the main rules to follow)

If you break 1 of these rules, the chance of the compiler not to inline your function will increase.

These rules all make sence. Say that you for example tries
to inline a 150lineMegaLargeMethod. Will the code be any faster if the code was pasted instead of called?
NO! this will just blow the cache.
Recursive calls - thinks it talks for it self...

But, as stated earlier, even if you follow these rules - the compiler CAN choose not to inline you function.

So, what can we say?
If you absolutely needs to make sure your snippet of code is inlined, use a MACRO. Otherwise, trust your compiler, and pray

/ Tooon
jonnyfish
jonnyfish
Is __forceinline just a suggestion?
Quantum
Quantum
quote:
Original post by Tooon
These rules all make sence. Say that you for example tries
to inline a 150lineMegaLargeMethod. Will the code be any faster if the code was pasted instead of called?
NO! this will just blow the cache.



How does it blow the cache to inline large functions or to have a lot of code in one function?
mhkrause
mhkrause
quote:

How does it blow the cache to inline large functions or to have a lot of code in one function?


Because if it were in one place (a function called from various places) that code could stay in the cache; each call refers to the same cache blocks.

If it''s inlined, each time the function is called, it''s different code, and may wind up kicking itself out of the cache.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.