Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
MPLAB
®
 XC8 C Compiler User’s Guide
DS52053B-page 62
 2012 Microchip Technology Inc.
3.6.3
How Can I Reduce RAM Usage?
Use the smallest data types possible. (This also reduces code size as less code is 
needed to access these.) Note that a bit type and non-standard 24-bit integer type 
(short long) exists for this compiler. SeSection 5.4 “Supported Data Types and 
Variables”
 f
or all data types and sizes. There are two sizes of floating-point type, as 
well, and these are discussed in the same section.
Consider using auto variables over global or static variables as there is the poten-
tial that these may share memory allocated to other auto variables that are not active 
at the same time. Memory allocation of auto variables is made on a compiled stack, 
described in Section 5.5.2.2 “Auto Variable Allocation and access”.
Rather than pass large objects to, or from, functions, pass pointers which reference 
these objects. This is particularly true when larger structures are being passed, but 
there might be RAM savings to be made even when passing long variables.
Objects that do not need to change throughout the program can be located in program 
memory using the const qualifier, see Section 5.4.7.1 “Const Type Qualifier” and 
Section 5.5.3 “Variables in Program Space”. This frees up precious RAM, but slows 
execution.
Ensure that all optimizations are enabled, seSection 4.8.42 “--OPT: Invoke Com-
piler Optimizations”
. Be aware of which optimizations the compiler performs (see 
Section 5.13 “Optimizations”) so that you can take advantage of them and don’t 
waste your time manually performing optimizations in C code that the compiler already 
handles.
3.6.4
How Can I Make My Code Faster?
To a large degree, smaller code is faster code, so efforts to reduce code size often 
decrease execution time, see Section 3.6.2 “How Can I Make My Code Smaller?”
See alsoSection 3.6.6 “How Can I Make My Interrupt Routine Faster?”. However, 
there are ways some sequences can be sped up at the expense of increased code size.
One of the compiler optimization settings is for speed (the alternate setting is for 
space), so ensure this is selected, see Section 4.8.42 “--OPT: Invoke Compiler 
Optimizations”
. Th
is will use alternate output in some instances that is faster, but 
larger.
Generally, the biggest gains to be made in terms of speed of execution come from the 
algorithm used in a project. Identify which sections of your program need to be fast. 
Look for loops that might be linearly searching arrays and choose an alternate search 
method such as a hash table and function. Where results are being recalculated, con-
sider if they can be cached.
3.6.5
How Does the Compiler Place Everything in Memory?
In most situations, assembly instructions and directives associated with both code and 
data are grouped into sections, called psects, and these are then positioned into con-
tainers which represent the device memory. An introductory explanation into this pro-
cess is given in Section 5.15.1 “Program Sections”. The exception is for absolute 
variables (see Section 5.5.4 “Absolute Variables”), which are placed at a specific 
address when they are defined and which are not placed in a psect.