net.sf.saxon.expr
Class Optimizer

java.lang.Object
  extended by net.sf.saxon.expr.Optimizer
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
OptimizerEE

public class Optimizer
extends Object
implements Serializable

This class performs optimizations that vary between different versions of the Saxon product. The optimizer is obtained from the Saxon Configuration. This class is the version used in Saxon-B, which in most cases does no optimization at all: the methods are provided so that they can be overridden in Saxon-EE.

See Also:
Serialized Form

Field Summary
protected  Configuration config
           
static int FULL_OPTIMIZATION
           
static int NO_OPTIMIZATION
           
 
Constructor Summary
Optimizer(Configuration config)
          Create an Optimizer.
 
Method Summary
 Expression convertPathExpressionToKey(PathExpression pathExp, ExpressionVisitor visitor)
          Examine a path expression to see whether it can be replaced by a call on the key() function; if so, generate an appropriate key definition and return the call on key().
 FilterExpression convertToFilterExpression(PathExpression pathExp, TypeHierarchy th)
          Convert a path expression such as a/b/c[predicate] into a filter expression of the form (a/b/c)[predicate].
 Expression extractGlobalVariables(Expression body, ExpressionVisitor visitor)
          Extract subexpressions from the body of a function that can be evaluated as global variables
 Configuration getConfiguration()
          Get the Saxon configuration object
 int getOptimizationLevel()
          Get the optimization level
 int isIndexableFilter(Expression filter)
          Test whether a filter predicate is indexable.
 boolean isVariableReplaceableByDot(Expression exp, Binding[] binding)
          Determine whether it is possible to rearrange an expression so that all references to a given variable are replaced by a reference to ".".
 Value makeClosure(Expression expression, int ref, XPathContext context)
          Make a Closure, given the expected reference count
 Expression makeConditionalDocumentSorter(DocumentSorter sorter, PathExpression path)
          Make a conditional document sorter.
 BinaryExpression makeGeneralComparison(Expression p0, int op, Expression p1, boolean backwardsCompatible)
          Create a GeneralComparison expression
 ValueRepresentation makeIndexedValue(SequenceIterator iter)
          Create an indexed value
 RuleTarget makeInversion(Template template, NodeTest nodeTest)
          Generate the inversion of the expression comprising the body of a template rules.
 ValueRepresentation makeSequenceExtent(Expression expression, int ref, XPathContext context)
          Make a SequenceExtent, given the expected reference count
 Expression makeStreamingApplyTemplates(ApplyTemplates inst)
          Make a streaming applyTemplates instruction.
 Expression optimizeCopy(Expression select)
          Attempt to optimize a copy operation.
 Expression promoteExpressionsToGlobal(Expression body, ExpressionVisitor visitor, boolean notTopLevel)
          Identify expressions within a function or template body that can be promoted to be evaluated as global variables.
 void setOptimizationLevel(int level)
          Set the optimization level
 void trace(String message)
          Trace optimization actions
 void trace(String message, Expression exp)
          Trace optimization actions
 Expression tryIndexedFilter(FilterExpression f, ExpressionVisitor visitor, boolean indexFirstOperand)
          Try converting a filter expression to a call on the key function.
 Expression tryInlineFunctionCall(UserFunctionCall functionCall, ExpressionVisitor visitor, ItemType contextItemType)
          Replace a function call by the body of the function, assuming all conditions for inlining the function are satisfied
 Expression trySwitch(Choose choose, StaticContext env)
          Try to convert a Choose expression into a switch
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_OPTIMIZATION

public static final int NO_OPTIMIZATION
See Also:
Constant Field Values

FULL_OPTIMIZATION

public static final int FULL_OPTIMIZATION
See Also:
Constant Field Values

config

protected Configuration config
Constructor Detail

Optimizer

public Optimizer(Configuration config)
Create an Optimizer.

Parameters:
config - the Saxon configuration
Method Detail

getConfiguration

public Configuration getConfiguration()
Get the Saxon configuration object

Returns:
the configuration

setOptimizationLevel

public void setOptimizationLevel(int level)
Set the optimization level

Parameters:
level - the optimization level, between 0 (no optimization) and 10 (full optimization). Currently all values greater than zero have the same effect as full optimization

getOptimizationLevel

public int getOptimizationLevel()
Get the optimization level

Returns:
the optimization level, between 0 (no optimization) and 10 (full optimization). Currently all values greater than zero have the same effect as full optimization

makeGeneralComparison

public BinaryExpression makeGeneralComparison(Expression p0,
                                              int op,
                                              Expression p1,
                                              boolean backwardsCompatible)
Create a GeneralComparison expression

Parameters:
p0 - the first operand
op - the operator
p1 - the second operand
backwardsCompatible - true if XPath 1.0 backwards compatibility is in force
Returns:
the constructed expression

optimizeCopy

public Expression optimizeCopy(Expression select)
                        throws XPathException
Attempt to optimize a copy operation. Return null if no optimization is possible.

Parameters:
select - the expression that selects the items to be copied
Returns:
null if no optimization is possible, or an expression that does an optimized copy of these items otherwise
Throws:
XPathException

makeClosure

public Value makeClosure(Expression expression,
                         int ref,
                         XPathContext context)
                  throws XPathException
Make a Closure, given the expected reference count

Parameters:
expression - the expression to be evaluated
ref - the (nominal) number of times the value of the expression is required
context - the XPath dynamic evaluation context
Returns:
the constructed Closure
Throws:
XPathException

makeSequenceExtent

public ValueRepresentation makeSequenceExtent(Expression expression,
                                              int ref,
                                              XPathContext context)
                                       throws XPathException
Make a SequenceExtent, given the expected reference count

Parameters:
expression - the expression to be evaluated
ref - the (nominal) number of times the value of the expression is required
context - the XPath dynamic evaluation context
Returns:
the constructed Closure
Throws:
XPathException

convertPathExpressionToKey

public Expression convertPathExpressionToKey(PathExpression pathExp,
                                             ExpressionVisitor visitor)
                                      throws XPathException
Examine a path expression to see whether it can be replaced by a call on the key() function; if so, generate an appropriate key definition and return the call on key(). If not, return null.

Parameters:
pathExp - The path expression to be converted.
visitor - The expression visitor
Returns:
the optimized expression, or null if no optimization is possible
Throws:
XPathException

tryIndexedFilter

public Expression tryIndexedFilter(FilterExpression f,
                                   ExpressionVisitor visitor,
                                   boolean indexFirstOperand)
Try converting a filter expression to a call on the key function. Return the supplied expression unchanged if not possible

Parameters:
f - the filter expression to be converted
visitor - the expression visitor, which must be currently visiting the filter expression f
indexFirstOperand - true if the first operand of the filter comparison is to be indexed; false if it is the second operand
Returns:
the optimized expression, or the unchanged expression f if no optimization is possible

convertToFilterExpression

public FilterExpression convertToFilterExpression(PathExpression pathExp,
                                                  TypeHierarchy th)
                                           throws XPathException
Convert a path expression such as a/b/c[predicate] into a filter expression of the form (a/b/c)[predicate]. This is possible whenever the predicate is non-positional. The conversion is useful in the case where the path expression appears inside a loop, where the predicate depends on the loop variable but a/b/c does not.

Parameters:
pathExp - the path expression to be converted
th - the type hierarchy cache
Returns:
the resulting filterexpression if conversion is possible, or null if not
Throws:
XPathException

isIndexableFilter

public int isIndexableFilter(Expression filter)
Test whether a filter predicate is indexable.

Parameters:
filter - the predicate expression
Returns:
0 if not indexable; +1 if the predicate is in the form expression=value; -1 if it is in the form value=expression

makeIndexedValue

public ValueRepresentation makeIndexedValue(SequenceIterator iter)
                                     throws XPathException
Create an indexed value

Parameters:
iter - the iterator that delivers the sequence of values to be indexed
Returns:
the indexed value
Throws:
UnsupportedOperationException: - this method should not be called in Saxon-B
XPathException

isVariableReplaceableByDot

public boolean isVariableReplaceableByDot(Expression exp,
                                          Binding[] binding)
Determine whether it is possible to rearrange an expression so that all references to a given variable are replaced by a reference to ".". This is true of there are no references to the variable within a filter predicate or on the rhs of a "/" operator.

Parameters:
exp - the expression in question
binding - an array of bindings defining range variables; the method tests that there are no references to any of these variables within a predicate or on the rhs of "/"
Returns:
true if the variable reference can be replaced

makeConditionalDocumentSorter

public Expression makeConditionalDocumentSorter(DocumentSorter sorter,
                                                PathExpression path)
Make a conditional document sorter. This optimization is attempted when a DocumentSorter is wrapped around a path expression

Parameters:
sorter - the document sorter
path - the path expression
Returns:
the original sorter unchanged when no optimization is possible, which is always the case in Saxon-B

tryInlineFunctionCall

public Expression tryInlineFunctionCall(UserFunctionCall functionCall,
                                        ExpressionVisitor visitor,
                                        ItemType contextItemType)
Replace a function call by the body of the function, assuming all conditions for inlining the function are satisfied

Parameters:
functionCall - the functionCall expression
visitor - the expression visitor
contextItemType - the context item type
Returns:
either the original expression unchanged, or an expression that consists of the inlined function body, with all function parameters bound as required. In Saxon-B, function inlining is not supported, so the original functionCall is always returned unchanged

promoteExpressionsToGlobal

public Expression promoteExpressionsToGlobal(Expression body,
                                             ExpressionVisitor visitor,
                                             boolean notTopLevel)
                                      throws XPathException
Identify expressions within a function or template body that can be promoted to be evaluated as global variables.

Parameters:
body - the body of the template or function
visitor - the expression visitor
notTopLevel - if true, only promote the children of the supplied body expression, not the entire expression
Returns:
the expression after subexpressions have been promoted to global variables
Throws:
XPathException

trySwitch

public Expression trySwitch(Choose choose,
                            StaticContext env)
Try to convert a Choose expression into a switch

Parameters:
choose - the Choose expression
env - the static context
Returns:
the result of optimizing this (the original expression if no optimization was possible)

extractGlobalVariables

public Expression extractGlobalVariables(Expression body,
                                         ExpressionVisitor visitor)
                                  throws XPathException
Extract subexpressions from the body of a function that can be evaluated as global variables

Parameters:
body - the body of the function
Returns:
a reference to the new global variable if a variable has been created, or null if not
Throws:
XPathException

makeStreamingApplyTemplates

public Expression makeStreamingApplyTemplates(ApplyTemplates inst)
                                       throws XPathException
Make a streaming applyTemplates instruction. Supported in Saxon-EE only

Parameters:
inst - the unoptimized applyTemplates instruction
Throws:
XPathException

makeInversion

public RuleTarget makeInversion(Template template,
                                NodeTest nodeTest)
                         throws XPathException
Generate the inversion of the expression comprising the body of a template rules. Supported in Saxon-EE only

Parameters:
template - the template to be inverted
nodeTest - the static item type of the context node of the template
Throws:
XPathException

trace

public void trace(String message,
                  Expression exp)
Trace optimization actions

Parameters:
message - the message to be displayed
exp - the expression after being rewritten

trace

public void trace(String message)
Trace optimization actions

Parameters:
message - the message to be displayed


Copyright (c) Saxonica Limited. All rights reserved.