<br> Tom,<br><br> The way to improve performance is to break the combinatorial problem by using one or both of the following:<br><br>* Write patterns against different classes: instead of writing the rule as A() and A(), use different classes to represent each component and write A() and B().<br>
<br>* Write alpha constraints (constraints against constant values) besides the beta constraints (join). Example:<br><br>$a1: A( type == "source" )<br>$a2: A( type == "destination", someAttr > $a1.someAttr )<br>
<br> Both of the above tricks help to break the worst case scenario ( O(n^p), where n is the number of facts and p is the number of patterns).<br><br> Edson<br><br><div class="gmail_quote">2010/1/21 Tom Carchrae <span dir="ltr"><<a href="mailto:tom@carchrae.net">tom@carchrae.net</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Please don't confuse my message with bashing Drools for not being as<br>
fast as Java. While I am not very familiar with Rete, I'm aware of<br>
what goes on behind the scenes; I've been programming declarative<br>
optimization systems for many years. My query relates to how to<br>
perform such a comparison with reasonable efficiency in Drools.<br>
<font color="#888888"><br>
Tom<br>
</font><div><div></div><div class="h5"><br>
<br>
On Thu, Jan 21, 2010 at 7:16 PM, Greg Barton <<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>> wrote:<br>
> pit->out<br>
><br>
> That falls into a rare class: two letter typos that pass a spell checker. :)<br>
><br>
> --- On Thu, 1/21/10, Greg Barton <<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>> wrote:<br>
><br>
>> From: Greg Barton <<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>><br>
>> Subject: Re: [rules-users] drools performance with <<br>
>> To: "Rules Users List" <<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>><br>
>> Date: Thursday, January 21, 2010, 9:04 PM<br>
>> This is not at all surprising.<br>
>> To set up and run the rules there's quite a bit of code<br>
>> running behind the scenes besides just the comparison test.<br>
>> (For instance, there's object creation just to track the<br>
>> fact that two objects matched the rule, and in this case<br>
>> those objects are created for every possible combination of<br>
>> TestObject instances before the first rule ever fires, then<br>
>> added to lists, and so on... So in the case of 1000<br>
>> TestObject instances, there's 1,000,000 object creations and<br>
>> list additions just for that.)<br>
>><br>
>> So, as has been said many times on this list, and probably<br>
>> in a few blogs, for any given algorithm a hard coded java<br>
>> version will invariably be faster than rules. What<br>
>> rules gives you is a way to express solutions to complex<br>
>> problems in a flexible and comprehensible way. It<br>
>> shines when the problems are so complex that coding them<br>
>> procedurally is pit of the question.<br>
>><br>
>> The same relationship is there between C and java.<br>
>> Java gives you managed, portable code, but for that you give<br>
>> up the raw speed of C.<br>
>><br>
>> --- On Thu, 1/21/10, Tom Carchrae <<a href="mailto:carchrae@gmail.com">carchrae@gmail.com</a>><br>
>> wrote:<br>
>><br>
>> > From: Tom Carchrae <<a href="mailto:carchrae@gmail.com">carchrae@gmail.com</a>><br>
>> > Subject: [rules-users] drools performance with <<br>
>> > To: <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
>> > Date: Thursday, January 21, 2010, 6:40 PM<br>
>> > Hello,<br>
>> ><br>
>> > I'm new to Drools and am trying it out. I've found<br>
>> it<br>
>> > easy enough so<br>
>> > far, but quickly ran into a performance bottleneck.<br>
>> > (perhaps an all<br>
>> > too familiar story! :) ). So, I made a simple<br>
>> example<br>
>> > to try and<br>
>> > track down the behavior. Here it is.<br>
>> ><br>
>> > I wanted to create a rule that uses an <<br>
>> comparison<br>
>> > between pairs of<br>
>> > objects. The following are the performance results:<br>
>> ><br>
>> > 10 objects. Java comparison took 0 ms<br>
>> > 10 objects. Rule firing took 2516 ms<br>
>> > 100 objects. Java comparison took 0 ms<br>
>> > 100 objects. Rule firing took 1984 ms<br>
>> > 1000 objects. Java comparison took 47 ms<br>
>> > 1000 objects. Rule firing took 132500 ms<br>
>> > 10000 objects. Java comparison took 4641 ms<br>
>> > 10000 objects. Rule firing took .... (please wait...)<br>
>> ><br>
>> > Here is the rule<br>
>> ><br>
>> > rule "A < B"<br>
>> > when<br>
>> > a : TestObject(<br>
>> > )<br>
>> > b : TestObject(<br>
>> > valueDouble < a.valueDouble )<br>
>> > then<br>
>> ><br>
>> > b.setValueint(b.getValueint()+1);<br>
>> > end<br>
>> ><br>
>> > Now, I do understand that this (worst-case) is a<br>
>> comparison<br>
>> > of N x N<br>
>> > objects... so, 1000 = 1000000 comparisons. However,<br>
>> > ~100ms per<br>
>> > comparison seems relatively expensive for such a<br>
>> simple<br>
>> > check. I am<br>
>> > using StatefulKnowledgeSession (I tried Stateless but<br>
>> this<br>
>> > gave only a<br>
>> > mild performance improvement - and I would also like<br>
>> to use<br>
>> > insert in<br>
>> > some of my rules).<br>
>> ><br>
>> > How would one speed up such a query? I know part of<br>
>> > the problem is<br>
>> > the < comparison. The results with == are much<br>
>> > better (presumably, it<br>
>> > can do lots of pruning of the space)<br>
>> ><br>
>> > Model creation 0<br>
>> > 10 objects. Rule firing took 2829 ms<br>
>> > 100 objects. Rule firing took 93 ms<br>
>> > 1000 objects. Rule firing took 657 ms<br>
>> > 10000 objects. Rule firing took 3375 ms<br>
>> > 100000 objects. Rule firing took 33266 ms<br>
>> ><br>
>> > I have thought about clever tricks that I could do to<br>
>> try<br>
>> > and speed<br>
>> > things up, like sorting into groups, etc. But it<br>
>> all<br>
>> > seems relatively<br>
>> > convoluted. Am I correct in coming to the<br>
>> conclusion<br>
>> > that a <<br>
>> > comparison is not the kind of thing that you should do<br>
>> in<br>
>> > Drools?<br>
>> ><br>
>> > How would you go about this? I would prefer to keep<br>
>> > all the logic in<br>
>> > the DRL files. I'm guessing you do a Java based<br>
>> > comparison and use a<br>
>> > fire-once rule to invoke this code.<br>
>> ><br>
>> > Thanks in advance for any insight,<br>
>> ><br>
>> > Tom<br>
>> > _______________________________________________<br>
>> > rules-users mailing list<br>
>> > <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
>> > <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
>> ><br>
>><br>
>><br>
>><br>
>><br>
>> _______________________________________________<br>
>> rules-users mailing list<br>
>> <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
>> <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
>><br>
><br>
><br>
><br>
><br>
> _______________________________________________<br>
> rules-users mailing list<br>
> <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
> <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
><br>
<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br> Edson Tirelli<br> JBoss Drools Core Development<br> JBoss by Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>