Why business list base? Why not business hashtable base?

Why business list base? Why not business hashtable base?

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


kucing posted on Monday, July 31, 2006

Currently, I am doing a development with a business object that has a list containing a few thousand of items in it. Everything loads very well and working flawlessly.

However, recently I noticed a constant slowdown when trying to add items. This is because when adding an item, function "contains" is called. Say I am trying to add 100 items and the list already contains 1000 items, I will have to perform 100 * 1000 checks, which is far too many. To counter this, I added my own hash table variable that has the keys of the items in the list so it does not have to iterate through all of the items in the list. Instead of 100 * 1000, it only does 100 checks.

It all sounds good but then it came to my mind, instead of storing the item inside the business list, why not store it inside hashtable to make it faster? Then again, why is business list is implemented as a list that does not scale well if it contains a large amount of items? Why not make it hash table instead? It may prove to be an overkill for a small list but for large lists it will perform extremely well.

Thoughts?

RockfordLhotka replied on Tuesday, August 01, 2006

BusinessListBase doesn't call Contains() itself, so it must be your code that is making that call - probably to ensure uniqueness? You might consider adding the ability to skip the Contains() call during the load process.

The reason I went with a list is data binding. You can't use data binding against a hashtable or dictionary.

However, if data binding is not a concern for you, there's no reason you couldn't create your own base class that is derived from a Dictionary<> or something along that line.

kucing replied on Wednesday, August 02, 2006

Thanks for clearing that one out. I actually need to bind the data so I will have to use the list. I will need to do something extra to make it faster though.

skagen00 replied on Wednesday, August 02, 2006

Would it be possible to construct a parallel internal dictionary/hashtable upon fetching that would respond to additions & removals?

In my "NameValueListBase" (I replaced it with my own hierarchical coding mechanism) I do this very thing. My list is read-only, however, so I haven't accomodated the removal/addition of items - but it shouldn't be particularly tricky I wouldn't think.

 

Shazam replied on Wednesday, August 02, 2006

I had a similar problem.  To keep the Dictionary object and the collection in sync, you can override the InsertItem and RemoveItem methods.

tetranz replied on Thursday, August 03, 2006

Shazam:
I had a similar problem.  To keep the Dictionary object and the collection in sync, you can override the InsertItem and RemoveItem methods.
Yes I've done the same on some lists where I needed quick frequently lookups. But ... I guess if its an editable list then you need to update all the references in the Dictionary whenever the list and child objects are serialized. Perhaps someone can confirm that.

An alternative to holding references in the Dictionary is to just hold the integer index. I think that's fine and perhaps a little cleaner if its a readonly list or if it only ever has items added at the end of the list. I think you then avoid the need to do anything on serialization. It gets complicated if you remove items from the list because everything after that item changes its index.

Cheers
Ross

Copyright (c) Marimer LLC