Inline Functions in C Language: Why and When They Are Used In ADAS Software

July 11, 2017 Tasking

behind the scenes on a movie set
 

 

I love watching “behind the scenes” clips of movies. It’s fascinating how the actors, cameramen, and all the other crew work together to make a film. It’s great to see the hilarious outtakes too. Seeing the little mistakes and unglamorous side of movies helps me to better appreciate them on the big screen. While compilers are not as entertaining as films, they also have a lot going on behind the scenes. It’s easy to use compiler optimizations while having little understanding of what the compiler is actually doing to your code. However, understanding how these tools work can help you use them more effectively. Tools like function inlining can help speed up your program but may use extra space. That’s why you need to carefully choose which sections of code to inline. If you don’t, you might end up using extra space without increasing your performance.

 

What are Inline Functions?

If a movie ends up being too long after filming, scenes might get trimmed or replaced in post production. In the same way, function inlining allows you to cut down the time it takes to call and return functions after you’ve already written the code. Inlining functions can take up more space than calling functions, but sometimes the size difference is minimal.

 

So what exactly is function inlining? Normally you call functions. The program then goes to that function, performs its operations, and returns a value or values. When functions are inlined, the compiler substitutes the function itself for the call of the function. This can speed up your software because you don’t waste time calling a function and then returning its values. You simply run the function in line with your code. The way compilers do this is a bit complex, as your software should not be able to tell whether the function was inlined or not. Another important thing to know is that tagging something with “inline” does not necessarily mean the compiler will actually inline it. You just have to do your best to “suggest” to the compiler what needs to be inlined.

 

Software Optimization
Function inlining can help you optimize your software.

 

Just like loop unrolling, function inlining brings up the classic time vs. space conundrum. You need to find the delicate balance between the two when programming for advanced driver assistance systems (ADAS). You need things like automatic braking to work quickly, but you also need to keep your program small enough to fit on your hardware. When your compiler inlines a function, it actually replaces your call lines with the lines of the function. If the function itself is longer than the call, it will take up more space. Sometimes functions are approximately the same size as their call, meaning they don’t take up much space when inlined. Other times functions are much larger and will significantly increase the size of your code. If the function is called multiple times, your compiler might inline multiple copies of it, wasting space. That’s why you always need to carefully choose which sections of code you want to optimize to make sure you don’t trade too much space for speed.

 

Which Functions to Inline

There are two main types of functions you should try to inline if possible: short functions and static functions. In the opposite direction, you should probably not inline long functions or ones that are repeated often.

 

Short functions are the low hanging fruit of inlining. If a function is about as many lines long as its call, you should definitely inline it. The compiler will replace the call with a function that’s relatively the same size but gives you a speed boost. Static functions are also great to inline. They are especially great to inline when they have only one call. There are several other more complex advantages to inlining functions, but they are beyond the scope of this article. Inlining short and static functions will help your program execute operations in less time, without adding too much size to your code.

 

The primary disadvantage of inlining functions is that it takes up more space. Especially if the function you’re inlining is quite long. Remember that you’re putting the function directly into your code. If the function is extremely lengthy, you could be adding more lines than is helpful. Functions that are called very often can also be a problem. You don’t want to inline those functions because you’re essentially copying the same function over and over into your code. Space is limited in cars and on their microprocessors, so you don’t want to waste space making copies of copies.

 

ADAS illustration
ADAS systems need optimized programs to operate efficiently.

 

Function inlining comes into the complex problem of code optimization. You can execute some sections of your program faster, but those sections will also be larger. That’s why it’s important that you accurately hint to your compiler which parts should be inlined. You should try to target short functions and static functions for inlining, while avoiding long functions or functions are used lots of times.

 

The thing about function inlining is that it should be done by a compiler. I would suggest using TASKING’s compiler. TASKING also makes a wide variety of other tools that can help speed up your work.

 

Have more questions about function inlining? Call an expert at TASKING.

 
Previous Article
 Why Connected Cars and Big Data Use NAND Memory
Why Connected Cars and Big Data Use NAND Memory

Cars are becoming more connected and thus need more onboard memory. NAND is quickly becoming a memory of ch...

Next Article
 Pros and Cons of Loop Unrolling Optimization in ADAS Vehicle Code
Pros and Cons of Loop Unrolling Optimization in ADAS Vehicle Code

ADAS enabled vehicle software has to find a balance between size and speed. Careful loop unrolling can help...

Get Your TASKING Free Trial Today.

Free Download