9. How to Build Efficient ASIS Applications¶
This chapter identifies some potential performance issues with ASIS applications and offers some advice on how to address these issues.
9.1. Tree Swapping as a Performance Issue¶
If an ASIS Context comprises more then one tree, then ASIS may need to switch
between different trees during an ASIS application run. Switching between
trees may require ASIS to repeatedly read in the same set of trees, and this may slow
down an application considerably.
Basically, there are two causes for tree swapping:
- Processing of semantically independent units. Suppose in
ContextContwe have unitsPandQthat do not depend on each other, andContdoes not contain any third unit depending on bothPandQ. This means thatPandQcannot be represented by the same tree. To obtain information aboutP, ASIS needs to access the treep.adt, and to get some information aboutQ, ASIS needsq.adt. Therefore, if an application retrieves some information fromP, and then starts processingQ, ASIS has to readq.adt. - Processing of information from dependent units.
A unit
Umay be present not only in the tree created forU, but also in all the trees created for units which semantically depend uponU. Suppose we have a library procedureProcdepending on a library packagePack, and in the set of trees making up ourContextwe have treespack.adtandproc.adt. Suppose we have someElementrepresenting a component ofPack, whenpack.adtwas accessed by ASIS, and suppose that because of some other actions undertaken by an application ASIS changed the tree being accessed toproc.adt. Suppose that now the application wants to do something with theElementrepresenting some component ofPackand obtained frompack.adt. Even though the unitPackis represented by the currently accessed treeproc.adt, ASIS has to switch back topack.adt, because all the references into the tree structure kept as a part of the value of thisElementare valid only forpack.adt.
9.2. Queries That Can Cause Tree Swapping¶
In ASIS-for-GNAT, tree swapping can currently take place only when processing queries defined in:
Asis.Elements
Asis.Declarations
Asis.Definitions
Asis.Statements
Asis.Clauses
Asis.Expressions
Asis.Text
but not for those queries in the above packages that return enumeration or boolean results.
For any instantiation of Asis.Iterator.Traverse_Element,
the traversal itself
can cause at most one tree read to get the tree appropriate for processing the
Element to be traversed, but procedures provided as actuals for
Pre_Operation and Post_Operation may cause additional tree
swappings.
9.3. How to Avoid Unnecessary Tree Swapping¶
To speed up your application, try to avoid unnecessary tree swapping. The following guidelines may help:
Try to minimize the set of tree files processed by your application. In particular, try to avoid having separate trees created for subunits.
Minimizing the set of tree files processed by the application also cuts down the time needed for opening a
Context. Try to usegnatmaketo create a suitable set of tree files covering an Ada program for processing by an ASIS application.Choose the
Contextdefinition appropriate to your application. For example, use ‘one tree’Context(-C1) for applications that are limited to processing single units (such as a pretty printer orgnatstub). By processing the tree file created for this unit, ASIS can get all the syntactic and semantic information about this unit. Using the ‘one tree’Contextdefinition, an application has only one tree file to read when opening aContext, and no other tree file will be read during the application run. An ‘N-trees’Contextis a natural extension of ‘one tree’Contextfor applications that know in advance which units will be processed, but opening aContexttakes longer, and ASIS may switch among different tree files during an application run. Use ‘all trees’Contextonly for applications which are not targeted at processing a specific unit or a specific set of units, but are supposed to process all the available units, or when an application has to process a large system consisting of a many units. When using an application based on an ‘all trees’Context, use the approach for creating tree files described above to minimize a set of tree files to be processed.In your ASIS application, try to avoid switching between processing units or sets of units with no dependencies among them; such a switching will cause tree swapping.
If you are going to analyze a library unit having both a spec and a body, start by obtaining an
Elementfrom the body of this unit. This will set the tree created for the body as the tree accessed by ASIS, and this tree will allow both the spec and the body of this unit to be processed without tree swapping.To see a ‘tree swapping profile’ of your application use the
-dtdebug flag when initializing ASIS (Asis.Implementation.Initialize ("-dt")). The information returned may give you some hints on how to avoid tree swapping.
9.4. Using gnatmake to Create Tree Files¶
To create a suitable set of tree files, you may use gnatmake. GNAT
creates an ALI file for every successful compilation, whether or not
code has been generated. Therefore, it is possible to run gnatmake with
the -gnatct option;
this will create the set of
tree files for all the compilation units needed in the resulting program.
Below we will use
gnatmake to create a set of tree files for a complete Ada program
(partition). You may adapt this approach to an incomplete program or to a
partition without a main subprogram, applying gnatmake to some of its
components.
Using gnatmake for creating tree files has another advantage: it will
keep tree files consistent among themselves and with the sources.
There are two different ways to use gnatmake to create a set of tree
files.
First, suppose you have object, ALI and tree files for your program in the same
directory, and main_subprogram.adb contains the body of the main
subprogram. If you run gnatmake as
$ gnatmake -f -c -gnatct ... main_subprogram.adb
this will create the trees representing the full program for which
main_subprogram is the main procedure. The trees will be created ‘from scratch’;
that is, if some tree files already exist, they will be recreated. This is
because gnatmake is being called with the -f option
(which means ‘force recompilation’).
Usng gnatmake without the -f option for creating tree files is not reliable
if your tree files are in the same directory as the object files, because
object and tree files ‘share’ the same set of ALI files.
If the
object files exist and are consistent with the ALI and source
files, the source will not be recompiled for creating a tree file unless the -f
option is set.
A different approach is to combine the tree files and the associated ALI files
in a separate directory, and to use this directory only for keeping the tree
files and maintaining their consistency with source files. Thus, the object
files and their associated ALI files should be in another directory.
In this case, by invoking gnatmake through:
$ gnatmake -c -gnatct ... main_subprogram.adb
(that is, without forcing recompilation) you will still obtain a full and consistent set of tree files representing your program, but in this case the existing tree files will be reused.
See the next chapter for specific details related to Ada compilation units belonging to precompiled Ada libraries.