Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a .net generic dictionary so big

I am serializing a generic dictionary in VB.net and I am very surprised that it is about 1.3kb with a single item. Am I doing something wrong, or is there something else I should be doing? I have a large number of dictionaries and it is killing me to send them all across the wire. The code I use for serialization is

    Dim dictionary As New Dictionary(Of Integer, Integer)
    Dim stream As New MemoryStream
    Dim bformatter As New BinaryFormatter()

    dictionary.Add(1, 1)

    bformatter.Serialize(stream, dictionary)

    Dim len As Long = stream.Length
like image 560
thefroatgt Avatar asked Apr 30 '10 15:04

thefroatgt


People also ask

Why dictionary is so fast C#?

So, dictionary is faster because you used a better algorithm. Show activity on this post. The reason is because a dictionary is a lookup, while a list is an iteration. Dictionary uses a hash lookup, while your list requires walking through the list until it finds the result from beginning to the result each time.

Are dictionaries faster than lists C#?

Of course the Dictionary in principle has a faster lookup with O(1) while the lookup performance of a List is an O(n) operation. The Dictionary map a key to a value and cannot have duplicate keys, whereas a list just contains a collection of values. Also Lists allow duplicate items and support linear traversal.

Why are dictionaries so fast?

The reason is dictionaries are very fast, implemented using a technique called hashing, which allows us to access a value very quickly. By contrast, the list of tuples implementation is slow. If we wanted to find a value associated with a key, we would have to iterate over every tuple, checking the 0th element.

Which is faster dictionary or list for lookup?

A dictionary is 6.6 times faster than a list when we lookup in 100 items.


2 Answers

The default serialization for a dictionary has to include type information for the type of the dictionary, the comparer used, and for the types of each of the items (both key and value) because they might in general be subtypes. This overhead has to be added for each dictionary. If you print the data as a string you can see that there are a lot of fully qualified types taking up a lot of bytes:

\0\0\0\0????\0\0\0\0\0\0\0\0\0\0?System.Collections.Generic.Dictionary2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0\aVersion\bComparer\bHashSize\rKeyValuePairs\0\0\b?System.Collections.Generic.GenericEqualityComparer1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\b?System.Collections.Generic.KeyValuePair2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]][]\0\0\0\t\0\0\0\0\0\0\t\0\0\0\0\0\0?System.Collections.Generic.GenericEqualityComparer1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0\0\a\0\0\0\0\0\0\0\0\0\0?System.Collections.Generic.KeyValuePair2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]?????System.Collections.Generic.KeyValuePair2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0keyvalue\0\0\b\b\0\0\0\0\v

You might prefer to use a custom format for serialization, or else a standard format that is slightly lighter such as JSON.

like image 197
Mark Byers Avatar answered Oct 24 '22 22:10

Mark Byers


There is a lot of overhead involved in setting up the dictionary for serialization (obviously, roughly 1.3kb ;) ). However, you'll find that the size doesn't grow dramatically as more elements are added to your collection, provided you're using primitive types for keys and values.

This overhead is mainly a one time, up front cost - so once you get the Dictionary class serialized, the contained members don't add as much size.

like image 35
Reed Copsey Avatar answered Oct 24 '22 22:10

Reed Copsey