Monday, September 26, 2011

Small efforts, big improvements

Some days ago, following some suggestions of Mark Proctor, I spent just a few hours trying to reduce both the memory footprint and the time requested to create an instance of the WorkingMemory, one of the most used core class of Drools. What I basically did was to lazy initialize all the non-strictly necessary data structures of that class, moving their creation away from the constructor, and rewrite a private method, also invoked during the initialization phase, that used some of those data-structure in a sub-optimal way.

Now I just had time to measure the actual improvements brought by my rework and I must say I was quite positively surprised of the results, especially if compared with the amount of time I spent to implement them. In particular I just tried to create a StatefulSession (that of course uses a WorkingMemory internally) starting from a KnowledgeBase with only one simple rule.

What I found is that my enhancements reduced the memory occupation of that StatefulSession from the initial 11,336 bytes to only 8,610. Even more impressive, the number of instances of StatefulSession I could create in a second, on my machine, increased from the initial 4,668 to an amazing 12,304.

I hope this demonstrates that, even when it seems unlikely, there could be almost always room for important improvements and, at the same time, that not all those improvements comes with a cost, in terms of effort, related with the real benefits they can bring.