Package net.sf.saxon.expr.parser
Class Optimizer
java.lang.Object
net.sf.saxon.expr.parser.Optimizer
- All Implemented Interfaces:
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:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncompileToByteCode
(Expression expr, String objectName, int evaluationMethods) Generate Java byte code for an expressionconvertPathExpressionToKey
(SlashExpression 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().convertToFilterExpression
(SlashExpression pathExp, TypeHierarchy th) Convert a path expression such as a/b/c[predicate] into a filter expression of the form (a/b/c)[predicate].extractGlobalVariables
(Expression body, ExpressionVisitor visitor, PromotionOffer offer) Extract subexpressions from the body of a function that can be evaluated as global variablesgenerateMultithreadedInstruction
(Expression instruction) Generate a multi-threaded version of an instruction.Get the Saxon configuration objectint
Get the optimization levelint
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 ".".makeCastableToList
(Expression operand, SchemaType targetType, boolean allowsEmpty) Make an expression castable check to a union type.makeCastableToUnion
(Expression operand, SchemaType targetType, boolean allowsEmpty) Make an expression castable check to a union type.makeCastToList
(Expression operand, SchemaType targetType, boolean allowsEmpty) Make an expression that casts to a list type.makeCastToUnion
(Expression operand, SchemaType targetType, boolean allowsEmpty) Make an expression that casts to a union type.makeConditionalDocumentSorter
(DocumentSorter sorter, SlashExpression path) Make a conditional document sorter.void
makeCopyOperationsExplicit
(Expression parent, Expression child) In streaming mode, make the copy operation applied to subexpressions of a complex-content sequence constructor into explicit copy-of operations.Create an indexed valuemakeInversion
(Pattern pattern, Template template, NodeTest nodeTest) Generate the inversion of the expression comprising the body of a template rules.Make an OuterForExpression (Saxon-EE only)Make a streaming applyTemplates instruction.optimizeCopy
(Expression select) Attempt to optimize a copy operation.In streaming mode, optimizer a ForExpression for streamingoptimizeGeneralComparison
(GeneralComparison gc, boolean backwardsCompatible) Simplify a GeneralComparison expressionIn streaming mode, optimizer a QuantifiedExpression for streamingpromoteExpressionsToGlobal
(Expression body, ExpressionVisitor visitor) 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 levelvoid
Trace optimization actionsvoid
trace
(String message, Expression exp) Trace optimization actionstryIndexedFilter
(FilterExpression f, ExpressionVisitor visitor, boolean indexFirstOperand) Try converting a filter expression to a call on the key function.tryInlineFunctionCall
(UserFunctionCall functionCall, ExpressionVisitor visitor, ExpressionVisitor.ContextItemType contextItemType) Replace a function call by the body of the function, assuming all conditions for inlining the function are satisfiedtrySwitch
(Choose choose, StaticContext env) Try to convert a Choose expression into a switch
-
Field Details
-
NO_OPTIMIZATION
public static final int NO_OPTIMIZATION- See Also:
-
FULL_OPTIMIZATION
public static final int FULL_OPTIMIZATION- See Also:
-
config
-
-
Constructor Details
-
Optimizer
Create an Optimizer.- Parameters:
config
- the Saxon configuration
-
-
Method Details
-
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
-
optimizeGeneralComparison
public BinaryExpression optimizeGeneralComparison(GeneralComparison gc, boolean backwardsCompatible) Simplify a GeneralComparison expression- Parameters:
gc
- the GeneralComparison to be simplifiedbackwardsCompatible
- true if in 1.0 compatibility mode- Returns:
- the simplified expression
-
optimizeCopy
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
-
convertPathExpressionToKey
public Expression convertPathExpressionToKey(SlashExpression 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 convertedvisitor
- the expression visitor, which must be currently visiting the filter expression findexFirstOperand
- 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(SlashExpression 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 convertedth
- the type hierarchy cache- Returns:
- the resulting filterexpression if conversion is possible, or null if not
- Throws:
XPathException
-
isIndexableFilter
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
Create an indexed value- Parameters:
iter
- the iterator that delivers the sequence of values to be indexed- Returns:
- the indexed value
- Throws:
XPathException
-
isVariableReplaceableByDot
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 questionbinding
- 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
Make a conditional document sorter. This optimization is attempted when a DocumentSorter is wrapped around a path expression- Parameters:
sorter
- the document sorterpath
- 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, ExpressionVisitor.ContextItemType contextItemType) Replace a function call by the body of the function, assuming all conditions for inlining the function are satisfied- Parameters:
functionCall
- the functionCall expressionvisitor
- the expression visitorcontextItemType
- 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) 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 functionvisitor
- the expression visitor- Returns:
- the expression after subexpressions have been promoted to global variables; or null if nothing has changed
- Throws:
XPathException
-
trySwitch
Try to convert a Choose expression into a switch- Parameters:
choose
- the Choose expressionenv
- 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, PromotionOffer offer) throws XPathException Extract subexpressions from the body of a function that can be evaluated as global variables- Parameters:
body
- the body of the functionoffer
- The PromotionOffer. Will be marked to indicate whether any action was taken- Returns:
- a reference to the new global variable if a variable has been created, or null if not
- Throws:
XPathException
-
makeStreamingApplyTemplates
Make a streaming applyTemplates instruction. Supported in Saxon-EE only- Parameters:
inst
- the unoptimized applyTemplates instruction- Throws:
XPathException
-
makeInversion
public RuleTarget makeInversion(Pattern pattern, 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:
pattern
- the match pattern of this template ruletemplate
- the template to be invertednodeTest
- the static item type of the context node of the template- Throws:
XPathException
-
makeCopyOperationsExplicit
In streaming mode, make the copy operation applied to subexpressions of a complex-content sequence constructor into explicit copy-of operations.- Throws:
XPathException
-
makeCastToUnion
Make an expression that casts to a union type. Not available in Saxon-HE -
makeCastToList
Make an expression that casts to a list type. Not available in Saxon-HE -
makeCastableToUnion
public Expression makeCastableToUnion(Expression operand, SchemaType targetType, boolean allowsEmpty) Make an expression castable check to a union type. Not available in Saxon-HE -
makeCastableToList
public Expression makeCastableToList(Expression operand, SchemaType targetType, boolean allowsEmpty) Make an expression castable check to a union type. Not available in Saxon-HE -
optimizeForExpressionForStreaming
In streaming mode, optimizer a ForExpression for streaming- Parameters:
expr
- the expression to be optimized- Returns:
- the optimized expression
- Throws:
XPathException
-
makeOuterForExpression
Make an OuterForExpression (Saxon-EE only)- Returns:
- the new expression
-
optimizeQuantifiedExpressionForStreaming
public Expression optimizeQuantifiedExpressionForStreaming(QuantifiedExpression expr) throws XPathException In streaming mode, optimizer a QuantifiedExpression for streaming- Parameters:
expr
- the expression to be optimized- Returns:
- the optimized expression
- Throws:
XPathException
-
generateMultithreadedInstruction
Generate a multi-threaded version of an instruction. Supported in Saxon-EE only; ignored with no action in Saxon-HE and Saxon-PE- Parameters:
instruction
- the instruction to be multi-threaded- Returns:
- the multi-threaded version of the instruction
-
compileToByteCode
Generate Java byte code for an expression- Parameters:
expr
- the expression to be compiledobjectName
- the name of the object (e.g. function) being compiledevaluationMethods
- The evaluation modes for which code is generated. Currently a subset ofExpression.PROCESS_METHOD
,Expression.ITERATE_METHOD
. If no code is generated for a given evaluation method, the fallback implementation from the superclass is used.- Returns:
- the compiled expression if possible, or null if compilation is not possible
-
trace
Trace optimization actions- Parameters:
message
- the message to be displayedexp
- the expression after being rewritten
-
trace
Trace optimization actions- Parameters:
message
- the message to be displayed
-