Global Sources
EE Times-India
Stay in touch with EE Times India
EE Times-India > Embedded

Using inline code in C and C++

Posted: 12 Nov 2013     Print Version  Bookmark and Share

Keywords:optimisation  inlining  C  compile  assembly language code 

The replacement of a function call by a copy of the code of the function can be an effective optimisation, particularly if execution speed is the main concern. This article takes a look at how inlining works, when it can be effective, and how it may happen automatically in C and C++.

Inlining basics
To the first approximation, all desktop computers are the same. It is straightforward to write acceptable applications that will run on anyone's machine. Also, the broad expectations of users are the same. But embedded systems are all different – the hardware and software environment varies widely and the expectations of users are just as diverse. In many ways, this is what is particularly interesting about embedded software development.

An embedded compiler is likely to have a great many options to control optimisation. Sometimes that fine-grain control is vital; on other occasions, it can come down to a simple choice between optimisation for speed or size. This choice is curious, but it is simply an empirical observation that small code is often slower and fast code tends to need more memory.

An obvious example is function inlining. A small function can be optimised so that its actual code is placed in line at each call site. This executes faster because the call/return sequence is eliminated. Also stack usage may be reduced. But this method has the potential to use more memory, as there may be multiple copies of identical code. Sometimes you can get lucky and an optimisation which yields faster code is also light on memory, but this is quite unusual.

Automatic inlining
It is reasonable to expect a good C or C++ compiler, when told to compile for speed, to perform inlining automatically. There are two situations when this may occur: small functions and static functions.

Small Functions: If a function is small, the size overhead in replacing calls with code may be very small and the speed benefit useful. In some cases, the code may be as small as, or smaller than, a call/return sequence, so there is a win-win – smaller and faster code.

Static functions: If a function is declared as static, the compiler knows that the function cannot be used outside of the module in hand, so it can make some smart decisions. Specifically, if there is only a single call to the function, the code should be inlined automatically without further ado, as the result will always be faster and smaller—a win-win situation. It may be argued that doing this automatically is wrong, because the programmer explicitly specified a call. However, I would counter with the assertion that a compiler's job is not to convert code in C to assembly language; it is required to translate an algorithm, expressed in C, to assembly language code with identical functionality.

C++ inlining
Although modern C compilers support inlining, it initially appeared as a language feature in C++. There are two ways to tell the compiler that inlining a function might be desirable: through inline keywords and Class Member Functions.

Inline keyword: C++ has a number of keywords additional to those in C. Among those is 'inline'. This is a directive to the compiler requesting that the function be inlined. The compiler is at liberty to ignore the request if the function is too big or inlining conflicts with optimisation settings (i.e., if switches request small code instead of fast code). The inline keyword is also implemented in many modern C compilers as a language extension, and works in the same way.

1 • 2 Next Page Last Page

Comment on "Using inline code in C and C+..."
*  You can enter [0] more charecters.
*Verify code:


Visit Asia Webinars to learn about the latest in technology and get practical design tips.


Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

Back to Top