[ 
https://issues.apache.org/jira/browse/JENA-630?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13964130#comment-13964130
 ] 

Andy Seaborne commented on JENA-630:
------------------------------------

Fixed example - I messed up as I refined the example.

The main point is that subexpressions get rewrittern multiple times - once per 
depth into the experssion and the work is not used.  

The output shows (+ 1 3) being rewrittern twice. {{ExprFunction2: ( 1 + 3 )}} 
and the NodeValues are visitied twice.  A deeper expression will larger numbers 
of copies.

A sub-point is that expressions are copied even on no change, and that causes 
he next level up to be copied, (ExprTransformCopy). This is a certain amount of 
object churn which might be reduced.  That's what i started looking for - the 
fact that there is repeated unnecessary copy going on was a later discovery.

{noformat}
        Expr expr = SSE.parseExpr("(+ 1 (* 2 3))") ;
        Expr expr2 = ExprTransformer.transform(new ExprTransformConstantFold(), 
expr) ;
        System.out.println("DONE") ;
{noformat}
{noformat}
ExprFunction2: ( 2 * 3 )
NodeValue: 2
NodeValue: 3
ExprFunction2: ( 1 + ( 2 * 3 ) )
NodeValue: 1
ExprFunction2: ( 2 * 3 )
NodeValue: 2
NodeValue: 3
DONE
{noformat}

{{ExprFunction2: ( 2 * 3 )}} is called twice 

{{"(+ 1 (* (+ 5 6 ) (+ 8 9)))"}} causes three calls to each of {{( 8 + 9 )}} 
and {{( 5 + 6 )}}, two calls to {{( ( 5 + 6 ) * ( 8 + 9 ) )}} and one call to 
{{( 1 + ( ( 5 + 6 ) * ( 8 + 9 ) ) )}}

Contrast with:
{noformat}
Expr expr3 = expr.copySubstitute(BindingFactory.root(), true) ;
{noformat}
which calls the sub-expressions once.

To do partial constant collapse will require bottom up evaluation. At the 
moment, calling copySubstitute at ever level causes sub-expressions to be 
folded, the results not used then called again when the next level up is 
handled.

> Do constant folding as part of query optimisation
> -------------------------------------------------
>
>                 Key: JENA-630
>                 URL: https://issues.apache.org/jira/browse/JENA-630
>             Project: Apache Jena
>          Issue Type: Improvement
>          Components: ARQ
>    Affects Versions: Jena 2.11.1
>            Reporter: Rob Vesse
>            Assignee: Rob Vesse
>             Fix For: Jena 2.11.2
>
>
> Currently Jena does not automatically simplify expressions by constant 
> folding even though the function API actually has support for this already 
> baked into it.
> This issue will track work to integrate a transform for this into the 
> standard optimiser.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to