Why does the order of how we specify the variables in a '==' comparison matter?

后端 未结 1 481
萌比男神i
萌比男神i 2021-01-07 02:44

What i noticed is that there is a big performance difference by just changing the order of the variables that are compared with the \'==\' operator. For example $variable ==

1条回答
  •  情话喂你
    2021-01-07 03:27

    I think what happens is that

    Class1(... , $var : var)
    Class2(var == $var, ...)
    

    produces a network where all Class1 facts are taken, and then the Cartesian product with all Class2 facts with identical var field is created.

    In contrast,

    Class1(... , $var : var)
    Class2($var == var, ...)
    

    "rewritten" by the compiler as

    Class1(... , $var : var)
    $c2: Class2(...)
    eval( $var == $c2.var )
    

    creates the Cartesian product of all Class1 facts and all (!) Class2 facts and only thereafter filters all where the eval is false.

    The traditional syntax (Drools 5 and earlier) forced you to have the field name on the left-hand side; only later on (late 5.x, 6.x), any logical expression was permitted.

    After speaking to s.o. from the Drools team, a more accurate description might be this:- It is likely that where an attribute is compared to something else an optimization is triggered. Someone from the Drools team will take a look and possibly improve it by checking also the reversed expression.

    0 讨论(0)
提交回复
热议问题