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
Contwe have units
Qthat do not depend on each other, and
Contdoes not contain any third unit depending on both
Q. This means that
Qcannot be represented by the same tree. To obtain information about
P, ASIS needs to access the tree
p.adt, and to get some information about
Q, ASIS needs
q.adt. Therefore, if an application retrieves some information from
P, and then starts processing
Q, ASIS has to read
Processing of information from dependent units. A unit
Umay be present not only in the tree created for
U, but also in all the trees created for units which semantically depend upon
U. Suppose we have a library procedure
Procdepending on a library package
Pack, and in the set of trees making up our
Contextwe have trees
proc.adt. Suppose we have some
Elementrepresenting a component of
pack.adtwas accessed by ASIS, and suppose that because of some other actions undertaken by an application ASIS changed the tree being accessed to
proc.adt. Suppose that now the application wants to do something with the
Elementrepresenting some component of
Packand obtained from
pack.adt. Even though the unit
Packis represented by the currently accessed tree
proc.adt, ASIS has to switch back to
pack.adt, because all the references into the tree structure kept as a part of the value of this
Elementare valid only for
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
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
Post_Operation may cause additional tree
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 use
gnatmaketo create a suitable set of tree files covering an Ada program for processing by an ASIS application.
Contextdefinition appropriate to your application. For example, use ‘one tree’
-C1) for applications that are limited to processing single units (such as a pretty printer or
gnatstub). 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 a
Context, 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 a
Contexttakes 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.