25

I am looking at the Collection classes in MSDN for the .Net framework. I ran into the HybridDictionary and it states (http://msdn.microsoft.com/en-us/library/system.collections.specialized.hybriddictionary.aspx):

Implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.

So I wondered about the ListDictionary which states (http://msdn.microsoft.com/en-us/library/system.collections.specialized.listdictionary.aspx)

Recommended for collections that typically include fewer than 10 items.

Now that seems like an arbitrary number (of items) to me. I can't find in the documentation what the mechanism behind this would be, I suspected the boundary of performance would have been related to a number of items like 2^N (2 to the power of N).

Now I do use the collection type of Dictionary often, and the collections might contain 10 to 30 items, 50 tops, depending on the 'page size'.

But HybridDictionary and ListDictionary requires unboxing and there are no generic type contructors for them.

I can't find a comparison anywhere about the performance of a HybridDictionary vs Dictionary.

So when to actually use this HybridDictionary over other Dictonary types?

P.S. And if HybridDictionary switches to ListDictionary or HashTable when the number of items grow to optimize its functioning. Why ever use a ListDictionary? If some requirements in the software change, and suddenly a maximum of 20 items must be put in the ListDictionary, instead of a maximum number of 10 items, the code must be re-factored to HybridDictionary to maintain performance?

unor
  • 92,415
  • 26
  • 211
  • 360
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
  • 2
    There is a good article here that includes benchmarking and comparison: http://www.dotnetperls.com/hybriddictionary – sa_ddam213 Sep 13 '13 at 06:20
  • @sa_ddam213 Seeing the conclusion that a `HybridDictionary` only shows performance increase with less than 5 items vs other Dictionaryies, it counters the statement on MSDN that a `ListDictionary` performs better than a `HashTable` with fewer than 10 items (well strictly not, but you get my point). So this `HybridDictionary` seems totally redundant to me. – Mike de Klerk Sep 13 '13 at 06:34
  • I don't see how its redundant, `ListDictionary` is recommended to be used with less than 10 items, the `HybridDictionary` is for when you don't exactly know the size and it will automatic switch implementations when the size is greater than 10 items – sa_ddam213 Sep 13 '13 at 06:41
  • @sa_ddam213 Correction: "The usage of `ListDictionary` in the code of your projects seems totally redundant", why not always use a `HybridDictionary`. – Mike de Klerk Sep 13 '13 at 07:27
  • `Specialized.Collections` are `Specialized.Collections` I guess, LOL – sa_ddam213 Sep 13 '13 at 07:40

1 Answers1

39

When to use a HybridDictionary over other Dictionary types?

You would use the ListDictionary when you are certain the collection size will be less than 10 items.

The HybridDictionary is pretty much the same as Dictionary but will take advantage of the performance of ListDictionary when the collection size is less that 10 items. Once the collection grows above 10 the HybridDictionary will switch from using ListDictionary internally to using a HashTable like an ordinary Dictionary.

So when to use one, well if your collection is usually under 10 items but at times could grow larger, then HybridDictionary will be the one to use.

For example, we use HybridDictionary in our mobile device applications comms layer, the comms message queue will pretty much always be under 10 items, but if there is a backend server outage the comms messages will build up into the 100's or 1000's depending on how long the server is down, a ListDictionary in this scenario would be awful, and in that case HybridDictionary will switch to a HashTable to keep performance up and still give us maximum performance when it is under 10.

So its used in specialized places, hence the namespace it belongs to System.Collections.Specialized :)

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • 1
    But what are the criteria to choose `HybridDictionary` over `Dictionary`? `Dictionary` is generic, and easier to use in code as it doesn't requires unboxing. I guess it doesn't matter all that much... – Mike de Klerk Sep 13 '13 at 07:26
  • If you are doing a lot of work with the `Dictionary` and it will be under 10 items but for other reasons the collection could explode well above 10 the you would use one, because you will have max performance when its under 10, but if it grows your performance will not be negatively impacted by the `ListDictionary`, My comms example above is a perfect scenario to use a `HybridDictonary`. even with the unboxing the `ListDictonary` will perform extremely well against a `Dictionary` with small amounts of items Its a very specialized use :) – sa_ddam213 Sep 13 '13 at 07:31
  • Yes, performance on mobile devices are more noticeably affected than today's desktop computers. Well servers with a lot of request might benefit from it as well. But as long as its a 'regular' user application on a desktop machine (that doesn't process tons of data) I guess it doesn't matter that much. Thanks for clarifying the usage of `HybridDictionary`. – Mike de Klerk Sep 13 '13 at 07:34
  • I would not rule it out on desktop apps, in low count queuing/caching scenarios with an unknown item count you may see quite surprising performance gains using `ListDictionary` and `HybridDictonary`, the Hybrid basically just has a safety catch if you end up over 10 items to ensure best performance. – sa_ddam213 Sep 13 '13 at 07:38
  • A narrower use case but there is `KeyedCollection` in ObjectModel namespace, which is a hybrid type and indeed generic. – nawfal Sep 28 '22 at 07:30