Package com.saxonica.ee.optim
Class OptimizerEE
java.lang.Object
net.sf.saxon.expr.parser.Optimizer
com.saxonica.ee.optim.OptimizerEE
This class performs a number of optimizations that are specific to the Saxon-EE product.
The relevant methods override dummy methods (that generally perform no optimization) in the
superclass, which is used by Saxon-HE.
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
assessFunctionStreamability
(XSLFunction sourceFunction, UserFunction compiledFunction) Assess whether a declared-streamable function is actually streamable.void
checkStreamability
(XSLTemplate sourceTemplate, TemplateRule compiledTemplate) Check the streamability of a templateconvertPathExpressionToKey
(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].Eliminate common subexpressions.evaluateStreamingArgument
(Expression expr, XPathContext context) Evaluate the streaming (first) argument of a streamable stylesheet functionint
gatherOrExpressions
(int commonParts, ExpressionVisitor visitor, ContextItemStaticInfo contextItemType, HashMap<Expression, List<Expression>> map, List<Expression> otherExps, Expression expr) Try to convert a collection of comparisons connected by "or" operators into a single GeneralComparisongenerateMultithreadedInstruction
(Expression instruction) Generate a multi-threaded version of an instruction.int
isIndexableFilter
(Expression filter) Test whether a filter predicate is indexable.makeConditionalDocumentSorter
(DocumentSorter sorter, SlashExpression path) Make a conditional document sorter.void
makeCopyOperationsExplicit
(Expression parent, Operand childOp) In streaming mode, make the copy operation applied to subexpressions of a complex-content sequence constructor into explicit copy-of operations.Create an indexed valueIn streaming mode, optimize a ForExpression for streaming.optimizeGeneralComparison
(ExpressionVisitor visitor, GeneralComparison gc, boolean backwardsCompatible, ContextItemStaticInfo contextItemType) Optimize a GeneralComparison expression.void
optimizeNodeSetPattern
(NodeSetPattern pattern) optimizeNumberInstruction
(NumberInstruction ni, ContextItemStaticInfo contextInfo) In streaming mode, optimize a QuantifiedExpression for streaming.optimizeSaxonStreamFunction
(ExpressionVisitor visitor, ContextItemStaticInfo cisi, Expression select) Attempt to optimize a copy operation.optimizeValueComparison
(ValueComparison vc, ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) Optimize a value comparisonvoid
Prepare an expression for streamingpromoteExpressionsToGlobal
(Expression expr, GlobalVariableManager globalVariableManager, ExpressionVisitor visitor) Identify expressions within a function or template body that can be promoted to be evaluated as global variables.reorderPredicates
(FilterExpression f, ExpressionVisitor visitor, ContextItemStaticInfo cisi) Consider reordering the predicates in a filter expression based on cost estimates and other criteriatryGeneralComparison
(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType, OrExpression orExpr) Try to convert an Or expression into a comparison with Literal sequencetryIndexedFilter
(FilterExpression f, ExpressionVisitor visitor, boolean indexFirstOperand, boolean contextIsDoc) Try converting a filter expression to a call on the key function.tryInlineFunctionCall
(UserFunctionCall functionCall, ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) Replace a function call by the body of the function, assuming all conditions for inlining the function are satisfiedtrySwitch
(Choose choose, ExpressionVisitor visitor) Try to convert a Choose expression (or an XQuery 3.0 switch) into an optimized switchMethods inherited from class net.sf.saxon.expr.parser.Optimizer
getConfiguration, getOptimizerOptions, isOptionSet, isVariableReplaceableByDot, makeInversion, setOptimizerOptions, trace, trace
-
Constructor Details
-
OptimizerEE
Create the Saxon-EE optimizer- Parameters:
config
- the Saxon-EE configuration
-
-
Method Details
-
optimizeValueComparison
public Expression optimizeValueComparison(ValueComparison vc, ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException Optimize a value comparison- Overrides:
optimizeValueComparison
in classOptimizer
- Parameters:
vc
- the value comparison expressionvisitor
- the expression visitorcontextInfo
- static context item information- Returns:
- either the original value comparison, or an optimized equivalent
- Throws:
XPathException
- if things go wrong
-
optimizeGeneralComparison
public Expression optimizeGeneralComparison(ExpressionVisitor visitor, GeneralComparison gc, boolean backwardsCompatible, ContextItemStaticInfo contextItemType) Optimize a GeneralComparison expression. Saxon-EE has a number of strategies for handling these; for example when comparing against a constant set of values, the expression is turned into a call on map:contains().- Overrides:
optimizeGeneralComparison
in classOptimizer
- Parameters:
visitor
- the expression visitorgc
- the GeneralComparison to be optimizedbackwardsCompatible
- true if in 1.0 compatibility modecontextItemType
- the static type of the context item- Returns:
- the simplified expression
-
optimizeSaxonStreamFunction
public Expression optimizeSaxonStreamFunction(ExpressionVisitor visitor, ContextItemStaticInfo cisi, Expression select) throws XPathException Attempt to optimize a copy operation. Return null if no optimization is possible.- Overrides:
optimizeSaxonStreamFunction
in classOptimizer
- Parameters:
visitor
- the expression visitorcisi
- Static information about the context itemselect
- 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
- if any error occurs
-
makeCopyOperationsExplicit
In streaming mode, make the copy operation applied to subexpressions of a complex-content sequence constructor into explicit copy-of operations. This makes constructs such as<e><xsl:sequence select="y"/></e>
streamable, by using the same run-time logic as if xsl:copy-of had been used.- Overrides:
makeCopyOperationsExplicit
in classOptimizer
- Parameters:
parent
- The parent expression of the expression that is to be wrapped in an implicit copy-of.childOp
- The operand that is to be wrapped in an implicit copy-of, if it is suitable.
-
checkStreamability
public void checkStreamability(XSLTemplate sourceTemplate, TemplateRule compiledTemplate) throws XPathException Check the streamability of a template- Overrides:
checkStreamability
in classOptimizer
- Parameters:
sourceTemplate
- the source of the template in the stylesheet treecompiledTemplate
- the compiled template- Throws:
XPathException
- if the template is declared streamable but does not satisfy the straming rules
-
optimizeForExpressionForStreaming
In streaming mode, optimize a ForExpression for streaming. This is possible if the "in" part of the for expression does a downward selection of the streamed input, while the "return" part does not access the streamed input at all (specifically, if it has no dependencies on the context node). The expression is converted to streamable form by rewriting "for $t in S return f($t)" as "for-each S return f(.)". This depends on the generated for-each expression being streamable, which will not always be the case, for example if the return expression makes two downward selections from the range variable, or a non-downward selection.- Overrides:
optimizeForExpressionForStreaming
in classOptimizer
- Parameters:
expr
- the expression to be optimized- Returns:
- the optimized expression, or the original expression if no optimization was possible.
-
optimizeQuantifiedExpressionForStreaming
public Expression optimizeQuantifiedExpressionForStreaming(QuantifiedExpression expr) throws XPathException In streaming mode, optimize a QuantifiedExpression for streaming. This is possible if the "select" part of the quantified expression does a downward selection of the streamed input, while the "satisfies" part does not access the streamed input at all (specifically, if it has no dependencies on the context node). The expression is converted to streamable form by rewriting "some $t in S satisfies f($t)" as "exists(for-each S return boolean(f(.)[.])", and likewise mutatis mutandis for "every $t....". This depends on the generated for-each expression being streamable, which will not always be the case, for example if the satisfies expression makes two downward selections from the range variable, or a non-downward selection.- Overrides:
optimizeQuantifiedExpressionForStreaming
in classOptimizer
- Parameters:
expr
- the expression to be optimized- Returns:
- the optimized expression, or the original expression if no optimization was possible.
- Throws:
XPathException
- if any error occurs
-
convertPathExpressionToKey
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.- Overrides:
convertPathExpressionToKey
in classOptimizer
- Parameters:
pathExp
- The path expression to be converted.visitor
- The expression visitor- Returns:
- the optimized expression, or null if no optimization is possible
-
tryIndexedFilter
public Expression tryIndexedFilter(FilterExpression f, ExpressionVisitor visitor, boolean indexFirstOperand, boolean contextIsDoc) Try converting a filter expression to a call on the key function. Return the supplied expression unchanged if not possible- Overrides:
tryIndexedFilter
in classOptimizer
- 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;contextIsDoc
- true if the context item is known to be a document node- Returns:
- the optimized expression, or the unchanged expression f if no optimization is possible
-
reorderPredicates
public FilterExpression reorderPredicates(FilterExpression f, ExpressionVisitor visitor, ContextItemStaticInfo cisi) throws XPathException Consider reordering the predicates in a filter expression based on cost estimates and other criteria- Overrides:
reorderPredicates
in classOptimizer
- Parameters:
f
- the filter expressionvisitor
- expression visitorcisi
- information about the context item type- Returns:
- either the original expression, or an optimized replacement
- Throws:
XPathException
- if things go wrong
-
convertToFilterExpression
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.- Overrides:
convertToFilterExpression
in classOptimizer
- Parameters:
pathExp
- the path expression to be convertedth
- the type hierarchy cache- Returns:
- the resulting filterexpression if conversion is possible, or null if not
-
isIndexableFilter
Test whether a filter predicate is indexable.- Overrides:
isIndexableFilter
in classOptimizer
- 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- Overrides:
makeIndexedValue
in classOptimizer
- Parameters:
iter
- the iterator that delivers the sequence of values to be indexed- Returns:
- the indexed value
- Throws:
XPathException
- if an error occurs
-
optimizeNodeSetPattern
- Overrides:
optimizeNodeSetPattern
in classOptimizer
-
prepareForStreaming
Prepare an expression for streaming- Overrides:
prepareForStreaming
in classOptimizer
- Parameters:
exp
- the expression to be prepared- Throws:
XPathException
- if any error occurs
-
evaluateStreamingArgument
public Sequence evaluateStreamingArgument(Expression expr, XPathContext context) throws XPathException Evaluate the streaming (first) argument of a streamable stylesheet function- Overrides:
evaluateStreamingArgument
in classOptimizer
- Parameters:
expr
- the expression supplied for the value of the streaming argumentcontext
- the XPath evaluation context of the caller- Returns:
- the (nominal) result of the evaluation
- Throws:
XPathException
- if any error occurs
-
makeConditionalDocumentSorter
Make a conditional document sorter. This optimization is attempted when a DocumentSorter is wrapped around a path expression- Overrides:
makeConditionalDocumentSorter
in classOptimizer
- Parameters:
sorter
- an expression that sorts and deduplicates its operandpath
- the slash expression that is the operand of the sorter- Returns:
- either the original sorter, if no optimization is possible; or a ConditionalSorter which first tests a condition, and performs the sort if the condition is true, or returns the unsorted operand if not. In practice the condition is always a test whether the first item in the path expression contains more than one item
-
tryInlineFunctionCall
public Expression tryInlineFunctionCall(UserFunctionCall functionCall, ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) Replace a function call by the body of the function, assuming all conditions for inlining the function are satisfied- Overrides:
tryInlineFunctionCall
in classOptimizer
- Parameters:
functionCall
- the functionCall expressionvisitor
- the expression visitorcontextItemType
- the static type of the context item- Returns:
- either the original expression unchanged, or an expression that consists of the inlined function body, with all function parameters bound as required.
-
trySwitch
Try to convert a Choose expression (or an XQuery 3.0 switch) into an optimized switch -
gatherOrExpressions
public int gatherOrExpressions(int commonParts, ExpressionVisitor visitor, ContextItemStaticInfo contextItemType, HashMap<Expression, List<Expression>> map, List<Expression> otherExps, Expression expr) Try to convert a collection of comparisons connected by "or" operators into a single GeneralComparison- Parameters:
commonParts
- the number of common subexpressions found at this levelvisitor
- the expression visitorcontextItemType
- static information about the context item typemap
- a map to be populated with comparison expressions, keyed on the common operandotherExps
- a list to be populated with expressions in the "or" tree that are not comparison expressions, so they are retained "as is"expr
- the Or expression to be optimized- Returns:
- a tally of the common expressions so far
-
tryGeneralComparison
public Expression tryGeneralComparison(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType, OrExpression orExpr) throws XPathException Try to convert an Or expression into a comparison with Literal sequence- Overrides:
tryGeneralComparison
in classOptimizer
- Parameters:
visitor
- the expression visitorcontextItemType
- static information about the context item typeorExpr
- the Choose expression- Returns:
- the result of optimizing the Or expression (the original expression if no optimization was possible)
- Throws:
XPathException
- if any error occurs
-
promoteExpressionsToGlobal
public Expression promoteExpressionsToGlobal(Expression expr, GlobalVariableManager globalVariableManager, ExpressionVisitor visitor) throws XPathException Identify expressions within a function or template body that can be promoted to be evaluated as global variables.- Overrides:
promoteExpressionsToGlobal
in classOptimizer
- Parameters:
expr
- the expression; either this expression or any subexpression might be promotedglobalVariableManager
- the global variable managervisitor
- the expression visitor- Returns:
- the expression after subexpressions have been promoted to global variables; or null if no action was taken
- Throws:
XPathException
- if any error occurs
-
eliminateCommonSubexpressions
Description copied from class:Optimizer
Eliminate common subexpressions. Rewrites (contains(X, 'x') and contains(X, 'y')) as (let $vv:C := X return (contains($vv:C, 'x') and contains($vv:C, 'y'))). Dummy method; the optimization happens only in Saxon-EE.- Overrides:
eliminateCommonSubexpressions
in classOptimizer
- Parameters:
in
- the expression to be optimized- Returns:
- out the optimized expression (possibly the same as the input).
-
generateMultithreadedInstruction
Generate a multi-threaded version of an instruction. Supported in Saxon-EE only; ignored with no action in Saxon-HE and Saxon-PE- Overrides:
generateMultithreadedInstruction
in classOptimizer
- Parameters:
instruction
- the instruction to be multi-threaded- Returns:
- the multi-threaded version of the instruction
-
optimizeNumberInstruction
public Expression optimizeNumberInstruction(NumberInstruction ni, ContextItemStaticInfo contextInfo) - Overrides:
optimizeNumberInstruction
in classOptimizer
-
assessFunctionStreamability
Assess whether a declared-streamable function is actually streamable. This analysis takes no account of the actual function call, which is assessed later in the streaming adjunct for the UserFunctioncall class- Overrides:
assessFunctionStreamability
in classOptimizer
-