CanReadProperty performance heads-up

CanReadProperty performance heads-up

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


DansDreams posted on Thursday, January 25, 2007

I have a case where I load 25,000 records from a database into a ReadOnlyListBase list, then about 10,000 times have to go to the list to get specific matching items (usually 2-3).

This is what I have in the collection:

public List<OwnerSummary> SelectForAccount(int accountID)
{
   List<OwnerSummary> results = new List<OwnerSummary>();
   foreach (OwnerSummary owner in this)
   {
   if (owner.AccountID == accountID)
      {
      results.Add(owner);
      }
   }
   return results;
   }

This part of the code was running dog slow.  I eventually tracked it down to the CanReadProperty call every time I accessed owner.AccountID.  I was using the reflection stack-trace version in the OwnerSummary class.  When I switched to CanReadProperty("AccountID", true) the execution time for the above code went from 3-4 seconds to the expected negligible time.

SonOfPirate replied on Thursday, January 25, 2007

Thanks for the post.  Guess the moral of the story is to use the explicit overload when performance matters and/or for frequently accessed fields.  Great to have true bench-marking to substantiate this "guideline".

 

tbim92 replied on Friday, August 03, 2007

Thank you for posting this. Saved my hide.

I couldn't find anything that helped me on slow-loading lists for a couple of days until I finally found this post. After reading it I vaguely remember reading about this in Rocky's book or in a post a long time ago. Wasted a couple of days trying to figure it out and narrowed it down to the "obj = Me.GetItemByID(id)" call which of course zipped through the objects in the list to find the correct object, referencing the ID property which used the CanReadProperty method with no property name specified.

Here are the load times for loading 450 objects and then finding those 450 objects one at a time to load a child list in each (time includes two database calls total, returning 450 and then 1785 records):


What a difference. Thanks again for posting!

kcharney replied on Thursday, March 27, 2008

WOW !!!!

This was EXACTLY what I was looking for.
It just saved my hide.

I could not figure out why in the world my form was taking so long to load.
I have a Parent with 6 children - 3 core and 3 summary.
Each child has several hundred items in them and it was taking forever to load up.

Commenting out the CanReadProperty - it loads almost instantly.

Thanks again for this post -wouldn't have thought to look there. Big Smile [:D]

Kurt

ajj3085 replied on Thursday, March 27, 2008

Well, you don't need to comment it.  Just use the overload where you explicitly pass the property name.  Of course if your really don't need it, commenting it would be fine.

RockfordLhotka replied on Thursday, March 27, 2008

In CSLA 3.5 there are two things:

1. The CanReadProperty() overload (and similar methods) that infer the property name are marked obsolete, so you can't use them anymore (unless you like compiler warnings)

2. The new ReadProperty<T>() and LoadProperty<T>() methods bypass authorization checks and validation, loading the property value directly - and so you do avoid this overhead entirely.

Copyright (c) Marimer LLC