【发布时间】:2014-01-21 19:59:15
【问题描述】:
我们有一个哈希表(特别是 C# Dictionary 类),它包含数千/数百万个 (Key,Value) 对,用于接近 O(1) 的搜索命中/未命中。
我们希望能够将此数据结构刷新到磁盘(序列化)并稍后再次加载(反序列化),以便保留字典的内部哈希表。
我们现在做什么:
- 从磁盘加载 =>
List<KVEntity>。 (KVEntity是可序列化的。我们使用 Avro 进行序列化 - 如果需要,可以删除 Avro) - 从数组 => 字典中读取每个
KVEntity。这重新生成字典/哈希表内部状态。 - 保存时,从字典中读入数组(通过
myKVDict.Values.SelectMany(x => x)读入新的List<KVEntity>) - 我们将数组 (
List<KVEntity>) 序列化到磁盘以保存原始数据
请注意,在我们的保存/恢复过程中,我们会丢失内部 tashtable/字典状态,并且每次都必须重建它。
我们希望直接序列化到字典(包括它的内部“实时”状态),而不是只为磁盘 i/o 使用中间数组。我们该怎么做?
一些伪代码:
// The actual "node" that has information. Both myKey and myValue have actual data work storing
public class KVEntity
{
public string myKey {get;set;}
public DataClass myValue {get;set;}
}
// unit of disk IO/serialization
public List<KVEntity> myKVList {get;set;}
// unit of run time processing. The string key is KVEntity.myKey
public Dictionary<string,KVEntity> myKVDict {get;set;}
【问题讨论】:
-
我使用哈希表获得的最快加速是选择超出预期大小的容量。哈希表的重新平衡是非常昂贵的,所以如果可以避免,事情就会快很多。
标签: c# serialization dictionary