When you were a child, did you imagine what the future would be like? Of course, I knew we would have flying cars by now. Unless teleportation had already made vehicles obsolete. I’m still waiting for those things to happen, but in the meantime, cars with advanced driver assistance systems (ADAS) are revolutionizing the transportation industry. While ADAS features make driving simple for people, the code running those innovations is a bit more complex. Software engineers are often trying to balance speed and size to make sure everything runs smoothly. Compiler optimizations, like loop unrolling, can help programmers speed up their code at the cost of space. The key is to choose which areas need to be optimized for size or speed so that everything works together well.
The Need for Speed...and Space
The field of embedded systems has always been tricky. Devices have to operate in varying locations, with or without power supplies, with very limited resources. However, the requirements of ADAS enabled cars are even more strict than the average embedded system. These restrictions are pushing developers to make their code faster and smaller.
In general, timing doesn’t matter very much in many embedded systems. For example, if I have several ambient temperature sensors they only have to sample every so often. Even if they start measuring at the wrong times, nothing will explode. If an ADAS system, like automatic braking, takes too long the consequences can be fatal. Programs have to execute within a certain time frame to ensure that an automatic lane change or braking system doesn’t cause an accident. To that end, lots of compilers come with optimizations to make things run faster, but they also often make things larger.
Space is still an important requirement for ADAS cars. Boards only have so much memory, so at some point you either have to slim things down or buy some more memory. Usually upgrading for more room isn’t an option, so you have to go back to the seesaw of size vs speed.
ADAS features require careful coding.
Loop unrolling is a technique that epitomizes the space vs time tradeoff. It’s an optimization that expands loops to make them execute more efficiently. Many loops operate inefficiently because the loop counter has to be updated after each execution. An unrolled loop allows a program to perform multiple operations before updating the counter. Depending on how many times the loop is iterated, this can significantly increase the speed of your program.
It’s possible to manually unroll loops, but I would recommend letting your compiler take care of most of the work. Many compilers come with loop unrolling optimizations that can generally help speed up your program. You will need to be careful, though, as your compiler may “optimize” sections of code that will have minimal effect. For example, That’s why you need to be sure to check over the optimizations and strike the perfect balance between size and speed.
When to Unroll
I keep talking about balancing timing space, but how do you actually create an optimal program? If your compiler simply unrolls every loop in your program, it will be too large.That’s why you need to keep an eye on it and only unroll certain parts.
The question you should ask yourself when optimizing is which functions will benefit from unrolling the most. A startup sequence with a loop that runs only once when the car turns on probably doesn’t need to be optimized for speed. Whereas a part of the code that is continually checking for other vehicles in the car’s blind spot needs to operate as efficiently as possible. If you can keep things like a start-up sequence slow, they won’t take up as much space. That extra area can then be used to optimize critical parts for speed. There’s no one right way to do this, and it can be time-consuming if you want to do it right. If you are able to strike a balance, though, you’ll speed up your software without having to upgrade your memory.
Optimize your code for both size and speed.
ADAS is becoming more common, and will even be required for new cars in the United States. As advanced features enter more cars, there will be a greater need for software than can run quickly in the limited space of onboard PCBs. Lots of ADAS functions require excellent and reliable timing, which means code must be optimized for speed. Loop unrolling can help provide some of this speed, but there’s always a balance. You’ll need to decide which parts of your code should be optimized for size, or for timing, to make sure your software is the best it can be.
In order to make great code, you’ll need an excellent compiler. TASKING’s compilers are made for ADAS enabled cars. Their optimizations will help your code run as fast as possible and meet your size requirements.
Have more questions about size vs speed optimization? Call an expert at TASKING.