PowerAda Coding for Space Efficiency
These techniques allow you to create programs that are smaller and require less storage as they run.
Instantiation of Generic Packages
For each instantiation of a generic body, the compiler inserts a copy of the generic code at the point of instantiation. Instantiating the same generic body multiple times with the same formal parameters wastes space.
Multiple instantiations occur most frequently when you use the generic I/O packages. For example, instead of instantiating TEXT_IO. INTEGER_IO for type INTEGER at numerous places throughout the program, the following two line compilation unit can be compiled into the library once. You can incorporate that unit into the other packages using a context clause (with statement) as necessary.
with Text_IO; package Int_IO is new Text_IO.Integer_IO( Integer );
Using the 'IMAGE, 'VALUE, or 'WIDTH attribute requires the compiler to create image and index tables. These tables are potentially very long: there are 4 bytes per enumeration literal in the index table, plus image of each enumeration literal identifier in the image table. To control the creation and allocation of these tables for a specified enumeration type, the implementation-dependent pragma IMAGES is provided. The syntax of this pragma is
pragma IMAGES(Enumeration_Type, immediate | deferred);
The DEFERRED option does not creates the image and index tables in the read-only section of each compilation unit in which one of the attributes appears. The IMMEDIATE option (the default) generates the image and index tables once in the compilation unit in which the enumeration type is declared. Thus DEFERRED saves space in the program only if you do not expect to use these attributes at all.
Although you can write a program as a single procedure, designing the program as a number of smaller packages and subprograms has several advantages:
- Program size affects the time and space required for compilation. Generally, compilation time increases more than linearly with program size, especially as more libraries must be searched and more symbols are defined, and disk space must be used to store the compiler's symbol tables. Also, the process of adding code to a program and then recompiling it leads to wasteful multiple compilation of existing text.
- Compilation units designed to perform limited functions need to contain only the data areas for those functions. Such units can also be replaced more easily by different versions and may be useful in several different applications.
- The storage for all the parameters and local variables is allocated in memory when a procedure or function is invoked. Reduce the number of variables and parameters used by a procedure. This reduction allows the compiler to generate faster, more efficient code.
- A compiled Ada subprogram will execute more efficiently if the code generated for that subprogram is less than 32K bytes.