Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

ExtensionFunctionDefinition and hasSideEffect

2012-05-04
2012-10-08
  • Barry Demchak
    Barry Demchak
    2012-05-04

    Hi ...

    I'm calling a small extension function (written by me, extending Saxon's
    ExtensionFunctionDefinition class) and am calling it from a small XQuery
    function. My extension function call (call-service) appears to be optimized
    out by Saxon, depending on how I write the XQuery code. My impression is that
    if I override call-service's hasSideEffects() function and always return
    "true", the function call should never be optimized out. Where could I be
    making a mistake?

    Here is the XQuery call that always calls call-service ($Message is a global
    variable):

    declare function palms:audit() as node() {
        let $audit := <blah></blah>
        return if (xf:call-service("vm://Audit.AuditRecord.queue", $audit)) then $Message else $Message   
    };
    

    Here is the XQuery call that never calls call-service:

    declare function palms:audit() as node() {
        let $audit := <blah></blah>
        let $dontCare := xf:call-service("vm://Audit.AuditRecord.queue", $audit)
        return $Message 
    };
    

    Thanks!

     
  • Michael Kay
    Michael Kay
    2012-05-04

    This is what the Javadoc for hasSideEffects() says:

    If the function does have side-effects, the optimizer will be less
    aggressive in moving or removing calls to the function. However, calls on
    functions with side-effects can never be guaranteed.

    In practice, this flag prevents some optimizations but it doesn't prevent
    others. In particular, it doesn't force a variable to be evaluated if the
    variable's value is never used. You need to "pretend" to make use of the
    result, for example by doing

    return concat($dontCare, $Message)