Skip to content

Achieving More with Less : Program Code Optimization

October 26, 2013

In embedded system development, one of the techniques that is useful would be optimization on program code. By enabling a smaller footprint, a lower cost of microcontroller can be selected. In a mass production environment, this may save thousands of dollar.

Another scenario would be the current design has been in mass production for some times. Additional features has to be added, or a critical bug is discovered, and you are running out of program code size.

I have come across such scenario when I was taken over a project(product has been launched for few years). The target microcontroller is from Zilog, Encore series.  The program code space left is 3 bytes, from a total flash size of 64KBytes! 3 bytes of space is not sufficient for any development, either for bug fixing or features adding. During the maintenance of this code, I had performed few times of code optimization into the source code, in order to fix some bug, as well as including new features into it. Instead of stopping the development due to insufficient code, this project manage to move on from base version of 2.05 until current version of 2.23 (of cause, version progression did not said much on how much code is changed).  Refer below on how much code save is being recorded: first reduction:0x17A, second reduction:0x80A. And the size is excluding the additional code being added into the code. On current latest version of 2.23, it have code size of 0xFC3E, with enough space for code maintenance and some small features.

before-code-reduction

[The original code size with only 3 bytes space left]

1st-reduction

[First time ROM reduction from size 0xFFFC to 0xFE82: 0x17A)

2st-reduction

[Second time ROM reduction from 0xFFE2 to 0xF7d8: 0x80A]

latest-code-size

[The current maintained version, with ROM size of 0xFC3E]

There are a few points worth taking note before starting code optimization.

Start from the smallest changes with biggest result

It is a smart way to achieve the biggest result with smallest effort. Thus, review all techniques available and prioritize the actions base on this.

Reduce changes as much as possible

Any changes to the code may introduce new bugs into the code. Thus, reduce changes whenever possible.

Constraint changes to a specific area

Try to constraint changes into a specific piece of code, rather than changes that spread across the code. It would be helpful if it can constraint to a specific function of the code. All of these action will definitely make the job of debugging easier.

Understand microcontroller architecture

A good understanding of MCU is useful in program code reduction. On example I given above, some of the variable declaration has been move from external memory into internal block of memory. This enable compiler to generate a smaller code size.

Understand compiler

Understanding the compiler capability to explore available option in code optimization. For example, compiler usually have options to enable optimization on space(smaller code size) or time(faster execution time).

In next post, I will be discussing techniques on code optimization.

Advertisements

From → Embedded System

One Comment

Trackbacks & Pingbacks

  1. Code Optimization | tkcheng

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: