Docstoc

Optimizations of XSLT

Document Sample
Optimizations of XSLT Powered By Docstoc
					             Optimizations in XSLT

                  http://www-sato.cc.u-
             tokyo.ac.jp/schuko/XSLT-opt.ppt
                        24/June/04

24/06/2004           Programming Language Design and   1
                             Implementation
             Evaluation of Stylesheets
• 1. Find the template which matches ‘/’
  2. Evaluate the template
      Evaluate each child in order
      if (child is LiteralResultElement) {
            Make the same node, and
            Evaluate Children as its children
                  (Depends on Recursive Structure of TREEs)
             } else
24/06/2004             Programming Language Design and        2
                               Implementation
              Need for Optimization
• Example: Matrix Multiplication
  (Itanium2 1.5GHz 6MB 3rd Cache:
      Intel Fortran Ver. 8.0)
             Option    -O0         -O1          -O2     -O3

             MFLOPS 8.53           94.8         140.1 3762.2



24/06/2004            Programming Language Design and          3
                              Implementation
             List of Optimizations
• -O2
     – Architecture Specific Optimizations such as
       Global code scheduling, software pipelining,
       predication, speculation.
     – Inlining of intrinsics.
     – Architecture Independent Optimizations such as



24/06/2004        Programming Language Design and   4
                          Implementation
             List of Optimizations (2)
• Higher Level Optimization
     – Constant propagation, copy propagation, dead-code
       elimination, global register allocation, global
       instruction scheduling, control speculation, loop
       unrolling, code selection, partial redundancy
       elimination, strength reduction, induction variable
       simplification, variable renaming, exception
       optimization, tail recursion elimination, peephole
       optimization, structure assignment lowering, dead store
       elimination

24/06/2004           Programming Language Design and             5
                             Implementation
             List of Optimizations (3)
• -O3
     – Prefetching, scalar replacement
     – Loop transformations
              Source of Performance Gain in most Technical
              Computing.




24/06/2004              Programming Language Design and       6
                                Implementation
             Points of Optimizations
• They are NEVER magic or ad-hoc
  technologies.
     – Program Analysis
     – Dataflow Equation based Global Analysis
     – Symbolic Evaluation/Partial Evaluation

     Semantics Based Optimizations.

24/06/2004         Programming Language Design and   7
                           Implementation
         Points of Optimizations(2)
• Architecture Specific Optimizations
     – New Features of Architectures
             •   SuperScalar/VLIW
             •   Vector Processing
             •   Speculation
             •   Prefetching
             •   …



24/06/2004                Programming Language Design and   8
                                  Implementation
         Points of Optimizations(3)
• Source-to-source conversion
• Accelerator + meta instruction
• Algorithm transformation




24/06/2004      Programming Language Design and   9
                        Implementation
    Source-to-Source Conversion
do I                                do J
do J                                do K
do K                                do I
 c(I,j) = c(I,j) +                   c(I,j)=c(I,j)+
            a(I,k)*b(k,j)                      a(I,k)*b(k,j)
end do                              end do
end do                              end do
end do                              end do
24/06/2004         Programming Language Design and             10
                           Implementation
   Accelerator + meta instruction
do I                                !$omp parallel do
do J                                do I
                                    do J
do K
                                    do K
 c(I,j) = c(I,j) +                   c(I,j) = c(I,j) +
            a(I,k)*b(k,j)                       a(I,k)*b(k,j)
end do                              end do
end do                              end do
end do                              end do

24/06/2004         Programming Language Design and              11
                           Implementation
        Algorithm Transformation
Call Bubblesort(a)                Call quicksort(a)



                                  Needs to Ensure the
                                     transformation
                                     Preserves program
                                     semantics


24/06/2004       Programming Language Design and         12
                         Implementation
             Optimization and Tuning
• Tune – Adjust an engine to run smoothly
     – Performance tuning
     – Human side Job
• Optimize – make the most effective use of
     – Performance or other metrics
     – Complicated in general – Computer side Job
     – A Kind of (automatic) Program transformation
• They are Very Similar.

• Profiling is Critical for Tuning
24/06/2004          Programming Language Design and   13
                            Implementation
                       Tuning
• Find HOT SPOT
     – Most resource consuming part
• Profiling Tools
  profiling by sampling
     – cc –p       gcc –pg
     – prof         gprof


24/06/2004        Programming Language Design and   14
                          Implementation
                             Tuning(2)
• Profiling with hardware support
     – Most modern processors
             •   instruction counts
             •   CPU time
             •   Cache miss rate/cache hit rate
             •   Hardware utilization (vector unit etc.)




24/06/2004                  Programming Language Design and   15
                                    Implementation
  HOT SPOTS in XSLT Evaluation

• Tuning of XSLT Engine + Stylesheet
  Optimization  Performance Improvement

• HOT SPOTS in XSLT Evaluation =
     – Evaluation of XPATH Expression
     – Template Instantiation
       (IN GENERAL, WHERE LOOP EXISTS)

24/06/2004     Programming Language Design and   16
                       Implementation
             Template Instantiation
• Evaluation of <xsl:apply-templates/>
     For each (target node)
           1. select matching template
             (query required)
           2. make frame
           3. call template


24/06/2004        Programming Language Design and   17
                          Implementation
     Procedure Call Optimization
• Interprocedural Optimization
     – Dataflow across Calls
• Inlining (Inline Expansion, Procedure
  Integration)
     – Save Call Overhead
• Tail Call Elimination
     – Save Frame Overhead

24/06/2004        Programming Language Design and   18
                          Implementation
                       Inlining
void a()                            Void a()
{                                   {
  b(2);                               printf(“%d\n”, 2+1);
}                                   }

void b(int x)                       Void b(int x)
{                                   {
  printf(“%d\n”, x+1);                printf(“%d\n”, x+1);
}                                   }

24/06/2004         Programming Language Design and           19
                           Implementation
                      Effect of Inlining
• Call Overhead Reduction
     – Execution of Call:
                                                               Heavy
             •   Arguments  Stack
             •   Return address  Stack
             •   Address of subroutine  Program Counter
             •   Make frame
             •   Save registers
             •   Execute
             •   Destroy frame (stack unreel)
             •   Return address  Program Counter
             •   Destroy arguments

24/06/2004                   Programming Language Design and           20
                                     Implementation
             Effect of Inlining(2)
• Very Effective for small functions
     – Methods
     – Inline prefix in C++
• Similar to macros




24/06/2004         Programming Language Design and   21
                           Implementation
             Effect of Inlining(3)
• Further Optimization across Calls
     – Most optimizations are done within a procedure.




• Code Size Increase (Drawback)
• Harder Program Analysis (Drawback)

24/06/2004        Programming Language Design and   22
                          Implementation
             Effect of Inlining(4)
• Alias problem in Fortran
     – Fortran does not assume aliases among
       arguments (exists in reality, though)
     – If inlined, Compiler must check if there is not
       any alias among arguments (often fails)
     – Then, poor code may be generated.



24/06/2004         Programming Language Design and       23
                           Implementation
             Note on Inlining


• Note that You must not do manual inlining.
  Be sure to write a program for inlining.




24/06/2004     Programming Language Design and   24
                       Implementation
                   Inlining in XSLT
…                                          …
<xsl:call-template name=“a”/>              <path>
<xsl:with-param name=“prefix”              <xsl:value-of select=“..”/>
   select=“..”/>
                                           /
…
                                           <xsl:value-of select=“.”/>
                                           </path>
<xsl:template name=“a”>
 <xsl:param name=“prefix”/>                …
<path>
<xsl:value-of select=“$prefix”/>
 /
  <xsl:value-of select=“.”/>
</path>
24/06/2004               Programming Language Design and                 25
</xsl:template>                  Implementation
               Tail Call Elimination
• Observation:
    <xsl:template name=“x”>
    <xsl:param name=“n”/>
    <xsl:choose>
           <xsl:when test=“…”>
                     <xsl:call-template name=“a”/>
                     <xsl:with-param name=“$n”/>
           </xsl:when>
           <xsl:otherwise>
                     <xsl:call-template name=“x”/>
                     <xsl:with-param name=“$n – 1”/>
           </xsl:otherwise>
      </xsl:choose>
24/06/2004                  Programming Language Design and   26
      </xsl:template>               Implementation
             Tail Call Elimination(2)
• Return from template a  immediate return
     – Tail Call
• Return from template x  immediate return
     – Tail Recursion
     – Used as LOOP.

• In this case, caller’s frame can be destroyed at
  calls of a or x.
• However, Call is done, and a new frame is
  allocated for a and x.
24/06/2004         Programming Language Design and   27
                           Implementation
             Tail Call Elimination(3)
• Ordinary…
  …                              destroy frame of x
  call of a                      jump to a
  make frame of a                make frame of a
  execute                        execute
  destroy frame of a             destroy frame of a
  return to x                     return from a
  destroy frame of x                  = return from x.
  return from x.
24/06/2004         Programming Language Design and       28
                           Implementation
             Tail Call Elimination(4)
Before Elimination                                After Elimination

             frame

       Ret address     call

             frame
                                        jump             frame

       Ret address                                     Ret address

24/06/2004           Programming Language Design and                  29
                             Implementation
             Tail Call Elimination(5)
• This Optimization Requires a Jump to a
  procedure
     – Low Level Stack Manipulation such as
             • Frame Destruction
             • Return Address Identification
     – Are Required.



24/06/2004               Programming Language Design and   30
                                 Implementation
      Tail Recursion Elimination
• Tail Recursion ⊆ Tail Call
  Tail Call to Self.

• Most Programming Languages have goto
  construct
     Can do Source-to-source conversion
• Frame creation/destruction =
             rewrite local variables

24/06/2004             Programming Language Design and   31
                               Implementation
   Tail Recursion Elimination(2)
• Observation:

int f(int n)                int ff(int n)      Jump
{                           {top:
   if (n==0) return 0;                if (n==0) return 0;
   else f(n-1);                  else {n=n-1;
}                                       goto top;
                                       }
                                             Frame destruction/
                              }              creation
24/06/2004        Programming Language Design and           32
                          Implementation
   Tail Recursion Elimination(3)
Before Optimization                              After Optimization


             n=…
                              call
             Ret address


              n=…                                            n=n-1
                                call
             Ret address                                     Ret address

24/06/2004                 Programming Language Design and                 33
                                   Implementation
   Effect of Tail Call Elimination
• Save Frame Creation/Destruction Cost

• Save Space for Frame Creation
     – Significant when LOOP is implemented as Tail
       Recursion (XSLT, most Functional Languages).




24/06/2004        Programming Language Design and   34
                          Implementation
   Tail Call Elimination in XSLT
• No GOTO Construct
     – × Source-to-source conversion
• Optimization of XSLT Engine
     – Recognition of Tail Call/Recursion.
     – Frame Adjustment, and Jump.




24/06/2004         Programming Language Design and   35
                           Implementation
 Tail Recursion Elimination in General

int fact(int n)
{
  if (n == 0) return 1;
  else return n * fact(n-1);
}

Not Tail Recursion
24/06/2004      Programming Language Design and   36
                        Implementation
   Tail Recursion Elimination in General(2)

int fact2(int n, int res)
{
  if (n==0) return res;
  else return fact2(n-1, n * res);
}

Tail Recursion
24/06/2004       Programming Language Design and   37
                         Implementation
   Tail Recursion Elimination in General(3)

int fact2(int n, int res)             int fact2(int n, int res)
{                                     {top:
   if (n==0) return res;                 if (n==0) return res;
   else return fact2(n-1,                else {
   n*res);                                    n = n-1;
}                                             res = n * res;
                                              goto top;
                                         }
int fact(int n)
{ return fact2(n, 1);}                int fact(int n)
                                      { return fact2(n,1);}
24/06/2004           Programming Language Design and              38
                             Implementation
   Tail Recursion Elimination in General(4)

• How to Rewrite non tail-recursion to tail-
  recursion.
     – Commutative, associative operations
     – Some Linearity in Call
     – Introduction of Intermediate Variables.




24/06/2004         Programming Language Design and   39
                           Implementation
             Accumulator type
fun f(n)                          fun f(n)
…                                 top:
call f(n-1);                           …
some-instructions(n);              r *=some-instruction(n)
return;                            n  n-1;
                                   goto top;

24/06/2004     Programming Language Design and          40
                       Implementation
             Accumulator type(2)
• If call graph is of the type
  f  f  f  f  … (linear),
  then, we can use r as Accumulator:

    r insn(n); r r*insn(n-1);r r*insn(n-2);
    …


24/06/2004       Programming Language Design and   41
                         Implementation
   XPATH Expression Optimization

• Loop is a major source for improving
  performance.
• In XSLT, We have Loop in Recursion and
  Xpath Expressions.




24/06/2004   Programming Language Design and   42
                     Implementation
                 XML Tree Structure
• Not the same as Unix file system.

                  x


                          a
             a



    b            b                 b

24/06/2004            Programming Language Design and   43
                              Implementation
             Simple Evaluator
• Evaluate(current, a/b/c): Loop
  S  φ;
  for-each x (child-node of current) {
      if (name(x) == a) {
            S  S ∪ Evaluate (x, b/c);
      }
  }
24/06/2004     Programming Language Design and   44
                       Implementation
              Menu of Optimizations
• Partial Evaluation/Symbolic Evaluation
     – Statically Obtain Result before Evaluation.


• Dataflow Equation Based Optimization
     – Solve Equation for Optimality in Dataflow
             • Redundancy Elimination



24/06/2004              Programming Language Design and   45
                                Implementation
         Menu of Optimizations(2)
• Loop Optimization
• Memory Hierarchy Optimization
• Hardware Resource Utilization

• Semantics Based Optimization



24/06/2004     Programming Language Design and   46
                       Implementation
              Partial Evaluation/
             Symbolic Evaluation
• Definition:
     Specialize Code by Replacing a Part of
    Code by Statically Evaluated Code.

• Static Evaluation and Specialization are
  Essential.


24/06/2004       Programming Language Design and   47
                         Implementation
       Example of Specialization
f(n, t)                                  p(n)
{                                        {
   if (t == 0)                              return f0(n);
         return g(n);                    }
   else
         return h(n);
                                         f0(n)
}
                                         {
p(n)                                        h(n);
{                                        }
    return f(n, 0);
24/06/2004              Programming Language Design and     48
}                               Implementation
    Partial Evaluation in General
• Strictly, Partial Evaluation is a
  Specialization.
• However, together with Symbolic
  Evaluation, constant propagation and
  constant folding are also classified as Partial
  Evaluation.


24/06/2004      Programming Language Design and   49
                        Implementation
   Constant Propagation/Folding
a = 1;                           a=1;
if (a+1 == 1)                    if (1+1==1)
   return 1;                        return 1;
else                             else
   return 2;                        return 2;
                                 
                                    return 2;

24/06/2004      Programming Language Design and   50
                        Implementation
               PE/SE in XSLT
• Evaluation of Variables, or Predicates
     – a/b/[position() > = 0]
     – <xsl:variable name=“a” select=“1”/>

         … $a…  “1”




24/06/2004        Programming Language Design and   51
                          Implementation
             Redundancy Elimination
• Eliminate Redundant Computation in
  Dataflow.
                           A=x+3+y;




                            B=x+3+y;


24/06/2004         Programming Language Design and   52
                           Implementation
       Redundancy Elimination(2)
• Redundancy:

• Dataflow Analysis Required.

     – Compute the Same Expression
     – Compute the Known Value


24/06/2004       Programming Language Design and   53
                         Implementation
       Redundancy Elimination in
               XPATH
• Common SubExpression Elimination
     – A/B|A/C  A/(B|C)
     – Interpreted in operational semantics:
             • for-each x (node ∈current)
                 If (name()==A) {
                    for-each y (node ∈child of x) {
                       if (name() == B or C) {
                          Sol = Sol ∪ {y}
                       }
                 }


24/06/2004                   Programming Language Design and   54
                                     Implementation
       Redundancy Elimination in
             XPATH(2)
• Loop Invariant Hoisting
     – A/B[../@category = ‘fiction’]
        A[@category = ‘fiction’]/B
     – Interpreted in operational semantics:
             for-each x (node=current)
               if (name() = A) {
                      for-each y (node ∈child of x) {
                       if (name() = B) {
                          if (parent(y).@category = ‘fiction’) {
                                Sol = Sol∪{y}
                          }
                       }}}
24/06/2004                   Programming Language Design and       55
                                     Implementation
       Redundancy Elimination in
             XPATH(3)
             for-each x (node=current)
               if (name() = A) {
                     if (x.@category = ‘fiction’) {
                     for-each y (node ∈child of x) {
                       if (name() = B) {
                          Sol = Sol∪{y}
                          }
                       }}}


24/06/2004                Programming Language Design and   56
                                  Implementation
       Redundancy Elimination in
             XPATH(4)
• Value Number
     – DAG representation of expressions:
             • a + a * (b – c) + (b – c) * a * c
                                   +

                        +
                                                     *
                               *               *
                        a              -

24/06/2004                     b           c
                            Programming Language Design and   57
                                    Implementation
             DAG  Instructions
1  mknode(id, a)                 11  mknode(id, a) = 1
2  mknode(id, a) = 1             12  mknode(*,10=5,11=1)
3  mknode(id, b)
                                  13  mknode(id, c) = 4
4  mknode(id, c)
                                  14  mknode(*,12,13=4)
5  mknode(-, 3, 4)
6  mknode(*, 2=1, 5)             15  mknode(+,7, 14)
                                           +
7  mknode(+, 1, 6)
8  mknode(id, b) = 3                     +
                                                                   *
9  mknode(id, c) = 4                              *           *
10  mknode(-, 8=3,9=4)=5
                                         a             -
24/06/2004       Programming Language Design and                       58
                         Implementation
                                                   b       c
 Instructions  Register Transfer
                                     1  mknode(id, a)
Load a, r1                           2  mknode(id, a) = 1
Load b, r3                           3  mknode(id, b)
                                     4  mknode(id, c)
Load c, r4                           5  mknode(-, 3, 4)
iSub r3, r4, r5                      6  mknode(*, 2=1, 5)
                                     7  mknode(+, 1, 6)
Imul r1, r5, r6                      8  mknode(id, b) = 3
                                     9  mknode(id, c) = 4
Iadd r1, r6, r7                      10  mknode(-, 8=3,9=4)=5
Imul r5, r1, r12                     11  mknode(id, a) = 1
                                     12  mknode(*,10=5,11=1)
Imul r12,r4, r14                     13  mknode(id, c) = 4
                                     14  mknode(*,12,13=4)
Iadd r7, r14, r15                    15  mknode(+,7, 14)

24/06/2004          Programming Language Design and              59
                            Implementation
        Redundancy Elimination in
                XPATH
• Dead Code Elimination




24/06/2004     Programming Language Design and   60
                       Implementation
       Redundancy Elimination in
             XPATH(4)
• There can be many other optimizations of
  Evaluation of Xpath Expressions.

• Node-set calculation includes loops, which
  are major source for performance
  improvement.


24/06/2004     Programming Language Design and   61
                       Implementation
             Other Optimizations
• Dataflow Equations

• Type Checking

• Semantics Based Optimizations



24/06/2004       Programming Language Design and   62
                         Implementation

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:4
posted:7/28/2012
language:English
pages:62