logo
Optimizations < XTMPath, Manipulating Topic Map Data Structures < < Home 

PrevUpNext

Optimizations

Working with XTM::Path one soon realizes that it adds some overhead and that it should be used with care. Among other tricks mentioned in the man page one is to use variables, similar to the way SQL APIs allow you to do.

Let us assume that we would look for topics having a particular base name:

foreach my $name (...list of names...) {
   my @bn = $xtmp->find (qq{topic[baseNameString = "$name"]}, $tm);
   ...
}
For every iteration of the loop the XTMPath processor will first parse the expression which contains a (possibly different) base names every time. Then it will execute the query.

As parsing an XTMPath expression is an expensive operation we can introduce a parameterized version of the expression which remains the same throughout all iterations:

foreach my $name (...list of names...) {
   my @bn = $xtmp->find ('topic[baseNameString = ?n]', 
                         $tm,
                         { n => $name});
   ...
}
Having introduced a variable n, the expression is parsed only the first time (and then remains cached in the $xtmp object). In a separate hash we provide the mapping between variable names and their values for one particular query. Simple tests show that this can have significant speedups if you deal with many different names as above.