Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

inline functions and macro functions

This topic is 3763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Do small inline functions eg:
inline unsigned char ColourGetA(unsigned Col)
{return (Col & 0xFF000000) >> 24;}

work as well as macro functions in terms of speed since I prefew them with them obeying namespace scope etc? also can the same be said for using contants rather than defines for things like Flag values?

Share this post


Link to post
Share on other sites
Advertisement
Actually they might even work better. Modern compilers run several passes at different times and different scopes to elimiate constant/invariant expressions before and after inlining, loop unrolling etc.

The whole block of a constant function such as the one you posted can be eliminated early, whereas a macro is simply a text replacement that inserts some "dumb text" which is fed to the parser and which then translates to some statements.
A good compiler will 99% likely detect and eliminate all the individual statements of a constant expression in one of its optimization passes, but it's more efficient and possibly better to discard entire blocks right away. When discarding whole blocks, small-scope optimizations can sometimes work in addition.

Also, a compiler might in theory dedicate only a limited amount of time to the optimizer pass. I am not aware of any real compiler doing this, but there might be such a thing.

Share this post


Link to post
Share on other sites
Inline functions are usually more efficient actually as the compiler can make the decision *not to* inline certain things, thus saving cache space if the inline code happened to be have hidden complexities or be rarely executed.

Unfortunately compilers aren't perfect and sometimes you'll end up switching to a macro anyway after studying the disassembly. That is compilers have been known to make poor inlining decision (especially when not using profiler-guided optimizations), and if your functions happens to fall just beyond the event horizon and would simplify a long chain of template magic then it can have disastrous consequences. Weirder still is that even when it is inlined the code isn't necessarily seen as equivalent by the compiler, I've had both VC6 and GCC generate worse code without macros.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!