Shared/Static rules, are they really that intensive?

Shared/Static rules, are they really that intensive?

Old forum URL: forums.lhotka.net/forums/t/7650.aspx


c_manboy posted on Thursday, September 17, 2009

I'm reading the vb 2008 business object book and in it Rockford recommends the usage of shared/static rules to avoid a performance hit.  But i'm really curious, how many rules and/or objects does it take to become a noticeable hit?

While I recognize the advantage of using delegates for the rules, it seems that many of the rules I'd implement are small, less than 10 line methods.

JonnyBee replied on Friday, September 18, 2009

Hi,
Instance rules was the first implementation - static rules was added (I believe in 2.x) and has become the recommended approach.

Per type rules are initialized one time only - instance ruleas has to be registered for each instance of your object and that requires more memory.

And I believe the static rules is a better approach and makes your code more readable and have a better performance.

/jonnybee

IanK replied on Friday, September 18, 2009

You may not notice difference on a single business object but you may where the object references a Bussiness List object which could potentially have hundreds of items with each child object in the list having its own rules.

I have to say when I moved from earlier versions to around 2.1.4 when this change was made I think I noticed an overall improvement in the performance of CSLA.

c_manboy replied on Friday, September 18, 2009

I guess I'm just surprised that four busines rule methods would have an impact on an object that already has several dozen methods. 

Thanks for the feedback.

ajj3085 replied on Friday, September 18, 2009

Its not the number of methods, its that Csla maintains a RuleList which stores references to the rule methods. In the case of instance rules, this RulesList is per object, and thus each object requires more memory. The static rules are only every listed once, in a static RuleList... so no matter how many instances of a class you have, there's only one list. And in most cases, the rules apply to every instance of a class, so it doesn't make sense to have 100s of references to the same method when one will do.

RockfordLhotka replied on Friday, September 18, 2009

Andy is correct.

Consider the case where you load a list of 100 child objects. If each child instance has to initialize its own list of rules (and the objects that list contains) you can imagine that the time and memory consuption becomes measurable very rapidly.

The static rule approach means the rules are initialized and managed once per type and so it doesn't matter if you create 1 or 1000 instances, the overhead is the same.

Copyright (c) Marimer LLC