简体   繁体   English

为什么.net泛型字典如此之大

[英]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. 我在VB.net中序列化一个通用字典,我很惊讶它是一个单项的大约1.3kb。 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

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.Dictionary 2[[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.GenericEqualityComparer 1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\\b?System.Collections.Generic.KeyValuePair 2[[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.GenericEqualityComparer 1[[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.KeyValuePair 2[[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.KeyValuePair \\ 0 \\ 0 \\ 0 \\ 0 ???? \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0 \\ 0?System.Collections.Generic.Dictionary 2[[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.GenericEqualityComparer 1 [[System.Int32,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]] \\ b?System.Collections.Generic.KeyValuePair 2[[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.GenericEqualityComparer 1 [[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.KeyValuePair 2[[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.KeyValuePair 2[[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.KeyValuePair 2[[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 2[[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.KeyValuePair 2 [[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 . 您可能更喜欢使用自定义格式进行序列化,或者使用稍微轻一些的标准格式(如JSON)

There is a lot of overhead involved in setting up the dictionary for serialization (obviously, roughly 1.3kb ;) ). 设置字典用于序列化涉及很多开销(显然,大约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. 这种开销主要是一次性的前期成本 - 所以一旦你将Dictionary类序列化,所包含的成员就不会增加那么多的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM