October 5, 2020

Simple or optimized code

During this week, I worked on an old system that still uses a C++98 compiler (apparently, popular in some industry like spaces, airport…).
During a code review, a colleague tried to share a pointer between several classes. Unfortunately, without smart pointers, it is hard to protect this single pointer against race conditions.
After reviewing several solutions, equivalents of shared pointers, etc… I asked myself “Am I going on the correct way? Why are we protecting this one only for performance matter?”. Indeed, the simplest way was to copy this data, and I suggested to go with this simple solution.

Even if the performances are not optimized, the application is still efficient. It is why I am writing about this subject right now. What is more important in a code? Code performance or simplicity?

Optimization is not the goal.

Some developers, especially the fresh ones, think that the optimization and performance are the goal of a software. No, the primary goal of software is to be efficient!

I encountered a strange situation about an embedded system software: I saw some old code using a single “short” to store a set of Boolean. Thanks to a binary operation, we can get the values and then treat them:

C++ - A strange optimization

void connect_usb1(short system_status)
{
    if (system_status & 0x4)
    {
        power_on_usb();
    }
}

Pretty hard to read… For weeks I reviewed those kinds of code developed in the name of optimization. Every developer tried to read this, treat those strange set of bits, but every time we got difficulties. One day, I reviewed this code and with a colleague, we remove this kind of optimization to make the code simpler. I think you can easily understand the goal with this second example:

C++ - Simplicity

void connect_usb1(system_status current_system_status)
{
    if (current_system_status.is_usb1_powered_on)
    {
        power_on_usb();
    }
}

Sure, we are using a bigger data set… But the code became readable, easy to debug code, and easy to understand.

This is why optimization is a trap. The optimization can destroy the readability of your code. More of that, the “optimization” details are… Useless.

The compiler optimizes your code!

Did you see my post about ++i vs i++? If it is the case, you should remember how the compiler optimized my code with i++.

Read More

This example is true for any kind of instruction. In reality, the optimization is done in the majority by the compiler without any actions from you.

More of that, our systems today are so powerful that a compiler can extremely optimize your software.

Today, our systems are so powerful that we can sacrifice some performance for simplicity.

Is it useless to optimize my Software?

No. The question Simplicity vs Performance doesn’t have a universal answer. I got several experiences where the optimization was primordial. A great example is when I worked in the car engines. With important real-time restrictions, we optimized perfectly the software by using mapping and fixed points.

Another example when I worked for a hardware logger framework. As the logger was regularly used in this Software, I optimized this one too.

I cannot count the number of projects falling because a database was heavy to execute. Like the simplicity, the lack of optimization can kill a project. Simplicity and Optimization are not enemies, they work together.

You could optimize your code if you are working on a “core concept”, “critical part” or “a piece of code which is regularly called”. Indeed, only 20% of your code is used 80% during run-time. (Some say even 10% of the code used in 90% run-time!). It is those core concepts to optimize! But above all, don’t sacrifice the readability for a simple optimization!

If a colleague says that his solution is 12% more performant. Say no!

We saw the followings:

  • Optimization doesn’t mean “efficient” and “simplicity”.
  • The compiler optimizes your code.
  • The optimization question is valuable for critical parts.
  • Only a few parts of your code are regularly used.

If during a code review, a colleague proposes a homemade system, ask if the game is worth the candle. If it reduces the readability, if it doesn’t concern a core concept, say no!

One day, another developer will work on your code. If the code is not simple and easy to read, the risk of regression is higher.

The priority of Software should be efficiency and maintainability. The question of optimization is valuable only about some critical parts.

About the author 

Axel Fortun

​Developer specialized in Linux environment and embedded systems.
​Knowledge in multiple languages as C/C++, Java, Python​ and AngularJs.
​Working as Software developer since 2014 with a beginning in the car industry​ and then in ​medical systems.