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 Context Cont we have units P and Q that do not depend on each other, and Cont does not contain any third unit depending on both P and Q. This means that P and Q cannot 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 q.adt.

  • Processing of information from dependent units. A unit U may 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 Proc depending on a library package Pack, and in the set of trees making up our Context we have trees pack.adt and proc.adt. Suppose we have some Element representing a component of Pack, when pack.adt was 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 Element representing some component of Pack and obtained from pack.adt. Even though the unit Pack is 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 Element are valid only for pack.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:


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 use gnatmake to create a suitable set of tree files covering an Ada program for processing by an ASIS application.

  • Choose the Context definition 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 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’ Context definition, 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’ Context is a natural extension of ‘one tree’ Context for applications that know in advance which units will be processed, but opening a Context takes longer, and ASIS may switch among different tree files during an application run. Use ‘all trees’ Context only 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 Element from 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 -dt debug flag when initializing ASIS (Asis.Implementation.Initialize ("-dt")). The information returned may give you some hints on how to avoid tree swapping.