3.5.2 How Can I Make My Code Smaller?
There are a number of ways that this can be done, but results vary from one project to the next. Use the assembly list file to observe the assembly code produced by the compiler to verify that the following tips are relevant to your code. For information on the list file, see the MPLAB® XC32 Assembler, Linker and Utilities User’s Guide (DS50002186).
Use the smallest data types possible as less code is needed to access these (this also reduces RAM usage). For examples, a short integer type exists for this compiler. See 9 Supported Data Types and Variables for all data types and sizes.
There are two sizes of floating-point type, which are discussed in the same section. Replace floating-point variables with integer variables wherever possible. For many applications, scaling a variable's value makes eliminating floating-point operations possible.
Use unsigned types, if possible, instead of signed types, particularly if they are used in expressions with a mix of types and sizes. Try to avoid an operator acting on operands with mixed sizes whenever possible.
Whenever you have a loop or condition code, use a “strong” stop condition, that is, the following:
for(i=0; i!=10; i++)
is preferable to:
for(i=0; i<10; i++)
A check for equality (==
or !=
) is usually more efficient to implement than the weaker < comparison.
In some situations, using a loop counter that decrements to zero is more efficient than one that starts at zero and counts up by the same number of iterations. So you might be able to rewrite the above as:
for(i=10; i!=0; i--)
Ensure that you enable all the optimizations allowed for the edition of your compiler (see 21 Optimizations). If you have a PRO edition, you can use the -Os
option (see 5.7.7 Options for Controlling Optimization) to optimize for size. Otherwise, pick the highest optimization available.
Consider also the PRO-edition standard link-time optimizer, controlled by the -flto
option. This optimization adds internal bytecode representations of the code to special sections in the object file that are then processed as if they had been part of the same translation unit. This can allow the compiler to make certain assumptions about the program code and improved the effectively of some optimizations.
Consider using the a compressed ISA mode such as MIPS16 or microMIPS if it is supported on your device. Use the -mips16
or -mmicromips
option for your project to make the compiler default to these modes. Use the mips16 or micromips function attributes to change the mode at the function level. You may also choose the optimized and compressed variants of the libraries in the linker options. Be aware of what optimizations the compiler performs so you can take advantage of them and don’t waste your time manually performing optimizations in C code that the compiler already handles, for example, don’t turn a multiply-by-4 operation into a shift-by-2 operation as this sort of optimization is already detected.