• 对改善Dictionary时间性能的思考及一个线程安全的Dictionary实现


    在某些场景下, 对于内存数据结构, 当需要用多个键值来唯一确定一个值的时候, 我们经常会面临这样一个选择:

    • 组合键: 将多个键组合成一个组合键, 在一个词典中存储和定位数据;
    • 多级词典: 使用多级词典, 在多级词典中依次保存键-词典的键值对, 定位数据时由键依次确定下一级词典, 最终确定所在的数据;

    抛开内存数据的空间效率不谈, 以上这两类做法的主要时间效率影响在于:

    • 使用多级词典可以降低散列冲突及计算的时间消耗, 由于人为地添加了数据的分类, 数据散列的冲突概率也被大大降低.
    • 使用多级词典在创建子Dictionary的时候带来额外的时间消耗,
    • 使用多级词典可能会影响散列的数据均匀度, 这方面的影响类似于桶算法中的均匀度. 不均匀的桶长度会降低散列的效率.
    • 使用组合键时所采用的组合键生成算法的生成效率和算法对散列值分布的影响.
    • 在线程安全的要求对性能的影响. 在加锁的情况下, 锁的申请和释放对时间性能也有比较大的影响.

    那么, 综合这些因素, 最终哪种做法会有比较好的性能呢?

    1. 一个线程安全的词典实现

    为了做这个测试, 我们使用如下的线程安全词典. 它借助于ReadWriteLock来保证在多线程访问情况下数据的安全读写.

    A Threadsafe Dictionary
    1.  public class SynchronisedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
    2. {
    3.      private Dictionary<TKey, TValue> innerDict;
    4.      private ReaderWriterLockSlim readWriteLock;
    5.  
    6.      public SynchronisedDictionary()
    7.     {
    8.          this.readWriteLock = new ReaderWriterLockSlim();
    9.          this.innerDict = new Dictionary<TKey, TValue>();
    10.     }
    11.  
    12.      public void Add(KeyValuePair<TKey, TValue> item)
    13.     {
    14.          using (new AcquireWriteLock(this.readWriteLock))
    15.         {
    16.              this.innerDict[item.Key] = item.Value;
    17.         }
    18.     }
    19.  
    20.      public void Add(TKey key, TValue value)
    21.     {
    22.          using (new AcquireWriteLock(this.readWriteLock))
    23.         {
    24.              this.innerDict[key] = value;
    25.         }
    26.     }
    27.  
    28.      public void Clear()
    29.     {
    30.          using (new AcquireWriteLock(this.readWriteLock))
    31.         {
    32.              this.innerDict.Clear();
    33.         }
    34.     }
    35.  
    36.      public bool Contains(KeyValuePair<TKey, TValue> item)
    37.     {
    38.          using (new AcquireReadLock(this.readWriteLock))
    39.         {
    40.             return this.innerDict.Contains<KeyValuePair<TKey, TValue>>(item);
    41.         }
    42.     }
    43.  
    44.     public bool ContainsKey(TKey key)
    45.     {
    46.         using (new AcquireReadLock(this.readWriteLock))
    47.         {
    48.             return this.innerDict.ContainsKey(key);
    49.         }
    50.     }
    51.  
    52.     public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    53.     {
    54.         using (new AcquireReadLock(this.readWriteLock))
    55.         {
    56.             this.innerDict.ToArray<KeyValuePair<TKey, TValue>>().CopyTo(array, arrayIndex);
    57.         }
    58.     }
    59.  
    60.     public IEnumerator GetEnumerator()
    61.     {
    62.         using (new AcquireReadLock(this.readWriteLock))
    63.         {
    64.             return this.innerDict.GetEnumerator();
    65.         }
    66.     }
    67.  
    68.     IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
    69.     {
    70.         using (new AcquireReadLock(this.readWriteLock))
    71.         {
    72.             return this.innerDict.GetEnumerator();
    73.         }
    74.     }
    75.  
    76.     public bool Remove(TKey key)
    77.     {
    78.         bool isRemoved;
    79.         using (new AcquireWriteLock(this.readWriteLock))
    80.         {
    81.             isRemoved = this.innerDict.Remove(key);
    82.         }
    83.         return isRemoved;
    84.     }
    85.  
    86.     public bool Remove(KeyValuePair<TKey, TValue> item)
    87.     {
    88.         using (new AcquireWriteLock(this.readWriteLock))
    89.         {
    90.             return this.innerDict.Remove(item.Key);
    91.         }
    92.     }
    93.  
    94.     public bool TryGetValue(TKey key, out TValue value)
    95.     {
    96.         using (new AcquireReadLock(this.readWriteLock))
    97.         {
    98.             return this.innerDict.TryGetValue(key, out value);
    99.         }
    100.     }
    101.  
    102.     public int Count
    103.     {
    104.         get
    105.         {
    106.             using (new AcquireReadLock(this.readWriteLock))
    107.             {
    108.                 return this.innerDict.Count;
    109.             }
    110.         }
    111.     }
    112.  
    113.     public bool IsReadOnly
    114.     {
    115.         get
    116.         {
    117.             return false;
    118.         }
    119.     }
    120.  
    121.     public TValue this[TKey key]
    122.     {
    123.         get
    124.         {
    125.             using (new AcquireReadLock(this.readWriteLock))
    126.             {
    127.                 return this.innerDict[key];
    128.             }
    129.         }
    130.         set
    131.         {
    132.             using (new AcquireWriteLock(this.readWriteLock))
    133.             {
    134.                 this.innerDict[key] = value;
    135.             }
    136.         }
    137.     }
    138.  
    139.     public ICollection<TKey> Keys
    140.     {
    141.         get
    142.         {
    143.             using (new AcquireReadLock(this.readWriteLock))
    144.             {
    145.                 return this.innerDict.Keys;
    146.             }
    147.         }
    148.     }
    149.  
    150.     public ICollection<TValue> Values
    151.     {
    152.         get
    153.         {
    154.             using (new AcquireReadLock(this.readWriteLock))
    155.             {
    156.                 return this.innerDict.Values;
    157.             }
    158.         }
    159.     }
    160.  
    161.     private class AcquireReadLock : IDisposable
    162.     {
    163.         private ReaderWriterLockSlim rwLock;
    164.         private bool disposedValue;
    165.  
    166.         public AcquireReadLock(ReaderWriterLockSlim rwLock)
    167.         {
    168.             this.rwLock = new ReaderWriterLockSlim();
    169.             this.disposedValue = false;
    170.             this.rwLock = rwLock;
    171.             this.rwLock.EnterReadLock();
    172.         }
    173.  
    174.         public void Dispose()
    175.         {
    176.             this.Dispose(true);
    177.             GC.SuppressFinalize(this);
    178.         }
    179.  
    180.         protected virtual void Dispose(bool disposing)
    181.         {
    182.             if (!this.disposedValue && disposing)
    183.             {
    184.                 this.rwLock.ExitReadLock();
    185.             }
    186.             this.disposedValue = true;
    187.         }
    188.     }
    189.  
    190.     private class AcquireWriteLock : IDisposable
    191.     {
    192.         private ReaderWriterLockSlim rwLock;
    193.         private bool disposedValue;
    194.  
    195.         public AcquireWriteLock(ReaderWriterLockSlim rwLock)
    196.         {
    197.             this.rwLock = new ReaderWriterLockSlim();
    198.             this.disposedValue = false;
    199.             this.rwLock = rwLock;
    200.             this.rwLock.EnterWriteLock();
    201.         }
    202.  
    203.         public void Dispose()
    204.         {
    205.             this.Dispose(true);
    206.             GC.SuppressFinalize(this);
    207.         }
    208.  
    209.         protected virtual void Dispose(bool disposing)
    210.         {
    211.             if (!this.disposedValue && disposing)
    212.             {
    213.                 this.rwLock.ExitWriteLock();
    214.             }
    215.             this.disposedValue = true;
    216.         }
    217.     }
    218. }

    2. 试验流程说明:

    基本的实验步骤是:

    1.创建不同规模的样本数据, 测试对于全部的样本数据, 词典的读写时间.

    2.创建相同规模的样本数据, 改变两级级词典的键重叠率(键重叠率越高, 键的生成范围越小, 父级词典中的键值对越少, 每个子级词典中的键值对越多), 测试词典的读写时间. 键重叠率指的是父级词典中键的生成范围对样本数据规模的比值.
    1.  

    测试程序如下:

    组合键方式读写
    1. public void Add(TKey key, TSubKey subKey, TValue value)
    2. {
    3.     string compositeKey = key.ToString() + "+" + subKey.ToString();
    4.     this.innerDictionary.Add(compositeKey, value);
    5. }
    6.  
    7. public bool TryGetValue(TKey key, TSubKey subKey, out TValue value)
    8. {
    9.     value = default(TValue);
    10.     string compositeKey = key.ToString() + subKey.ToString();
    11.  
    12.     return this.innerDictionary.TryGetValue(compositeKey, out value);
    13. }

        作为测试, 我们使用string作为主键, 用string的联接作为联合主键. 字符串的联接既不会浪费太多时间从而干扰我们的测试, 又不会降低散列值的分布区间范围.

    级联式词典读写
    1. public void Add(TKey key, TSubKey subKey, TValue value)
    2. {
    3.     Dictionary<TSubKey, TValue> subDictionary;
    4.     if (this.rootDictionary.TryGetValue(key, out subDictionary))
    5.     {
    6.         subDictionary.Add(subKey, value);
    7.     }
    8.     else
    9.     {
    10.         subDictionary = new Dictionary<TSubKey, TValue>();
    11.         subDictionary.Add(subKey, value);
    12.         this.rootDictionary.Add(key, subDictionary);
    13.     }
    14. }
    15.  
    16. public bool TryGetValue(TKey key, TSubKey subKey, out TValue value)
    17. {
    18.     value = default(TValue);
    19.  
    20.     Dictionary<TSubKey, TValue> subDictionary;
    21.     if (this.rootDictionary.TryGetValue(key, out subDictionary))
    22.     {
    23.         return subDictionary.TryGetValue(subKey, out value);
    24.     }
    25.     else
    26.     {
    27.         return false;
    28.     }
    29. }

        在不确定数据是否存在的前提下, TryGetValue比ContainsKey+下标索引方式有更好的性能. 我们使用这种方法来读写数据.

    生成样本数据
    1.  
    2. public Dictionary<string, string> InitializeTestData(int sampleRecordsCount, int subKeyRange)
    3. {
    4.     Random rd = new Random();
    5.     Random subRd = new Random();
    6.     string key;
    7.     string subKey;
    8.  
    9.     Dictionary<string, string> sampleStore = new Dictionary<string, string>();
    10.     for (int i = 0; i < sampleRecordsCount; i++)
    11.     {
    12.         key = rd.Next(0, sampleRecordsCount).ToString();
    13.         subKey = subRd.Next(0, subKeyRange).ToString();
    14.  
    15.         if (!sampleStore.ContainsKey(key))
    16.         {
    17.             sampleStore.Add(key, subKey);
    18.         }
    19.     }
    20.  
    21.     return sampleStore;
    22. }

        我们把样本数据(两级主键)存储在一个样例词典中, 词典的键用作级联词典中的子级词典的键, 词典中的值用作级联词典中父级词典的键, 实现简单的一对多关系.

    3次性能测试取平均值
    1. List<long> addMilliseconds = new List<long>();
    2. List<long> readMilliseconds = new List<long>();
    3.  
    4. for (int i = 0; i < 3; i++)
    5. {
    6.     long add, read;
    7.     recordsCount = RunDictionaryPerformanceTest(dict, sampleStore, out add, out read);
    8.  
    9.     dict.Clear();
    10.     GC.Collect(2);
    11.  
    12.     addMilliseconds.Add(add);
    13.     readMilliseconds.Add(read);
    14. }
    15.  
    16. addAvg = (long)addMilliseconds.Average();
    17. readAvg = (long)readMilliseconds.Average();

    对应的测试函数如下:

    性能测试函数
    1. public int RunDictionaryPerformanceTest(ITestDictionary<string, string, object> dictToTest,
    2.     Dictionary<string,string> sampleStore, out long addingMilliseconds, out long readingMilliseconds)
    3. {
    4.     object obj = new object();
    5.     Stopwatch swAdd = new Stopwatch();
    6.     foreach (KeyValuePair<string, string> keyPair in sampleStore)
    7.     {
    8.         swAdd.Start();
    9.         dictToTest.Add(keyPair.Value, keyPair.Key, obj);
    10.         swAdd.Stop();
    11.     }
    12.  
    13.     addingMilliseconds = swAdd.ElapsedMilliseconds;
    14.  
    15.     Stopwatch swRead = new Stopwatch();
    16.     foreach (KeyValuePair<string, string> keyPair in sampleStore)
    17.     {
    18.         object ob;
    19.         swRead.Start();
    20.         dictToTest.TryGetValue(keyPair.Value, keyPair.Key, out ob);
    21.         swRead.Stop();
    22.     }
    23.  
    24.     readingMilliseconds = swRead.ElapsedMilliseconds;
    25.  
    26.     return sampleStore.Count;
    27. }

     

    3.实验结果及分析

    Samples Count - Add Performance

    Samples Count - Read Performance

        这是随着样本数量的上升, 向待测试的词典中添加新项(对应图一)/读取所有项(对应图二)所耗费的时间对应图. 从图中可以看出, 在十万的样本数量级上, 非线程安全的词典(包括组合键和级联), 时间性能差别微乎其微: Flatten对应的组合键方式有微弱的优势, 不过和Casade方式差别很小, 两者的读写时间从十几微妙到一百多微妙, 读写速度极快,  可以预见很少有可能在这上面成为系统瓶颈.

        在线程安全的词典中, 这两种方案的性能差别就比较明显了.

        在添加新项方面, 数据规模较小时, 组合键方式有比较好的性能; 随着数据规模的增大, 级联方式在性能上逐渐超越组合键方式; 级联的原理决定了它比组合键方式有更快捷更直接的定位数据方式(级别分类), 比组合键方式更直接; 不过当数据规模较小时, 这种原理上带来的好处不足以抵消因为创建子级词典对象所带来的额外的时间消耗.

        在读取所有项方面, 级联方式的行为表现比较奇怪. 我们期待它有比组合键方式更好的读取性能. 但是在Windows 7操作系统上, 2GB RAM, 整个测试过程无内存换页发生的情况下的多次测试, 均指向这一结果 - 组合键方式的读取速度几乎是级联方式的两倍!. 但是在Windows XP操作系统上, 2G RAM, 无内存换页上的测试, 却完全相反 - 级联方式是组合键方式的两倍!

        - 我向上帝保证我没在代码上犯低级错误. 查看词典的源代码, 应该是FindEntry的时候决定了读取效率:

    1.         private int FindEntry(TKey key) {
    2.             if( key == null) {
    3.                 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
    4.             }
    5.             if (buckets != null) {
    6.                 int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
    7.                 for (int i = buckets[hashCode % buckets.Length]; i >= 0; i = entries[i].next) {
    8.                     if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) return i;
    9.                 }
    10.             }
    11.             return -1;
    12.         }

        看起来很普通的一段桶映射关系查找, 依然不知道为什么, 猜测是操作系统调度的问题...各位谁有兴趣可以试一下 :)

    Samples Overlap - Add Performance 

    Samples Overlap - Read Performance

        上面两图是指在一定的数据规模下, 父级词典容量对读写性能的影响; 读写均指向同一趋势: 父级词典的容量越大, 读写时间越长. 这是因为在性能指向上, 父级词典越大, 每个子级词典存储的项越少, 存储效率越低, 因而额外的创建多级词典所带来的时间消耗比重越来越大.

    4. 结论

        结论很简单, 级联式词典的数据结构并没有带来相当大的好处, 却更不稳定(指时间性能), 更庞大(指内存结构), 更敏感(指重叠率).

        另外需要指出的一点, 本文没有对比级联式词典和组合键式词典在多线程读写条件下的性能对比, 因为这是没有疑问的. 级联式词典在在锁的申请和释放方面会更有效率, 因为锁而带来的等待会因级联的结构而大幅度减少, 这恰恰和重叠率是一对矛盾统一.所以如果你对要存储的数据的特征和分类相当明确, 并且处在一个多线程环境中, 建议尝试一下级联结构.

        还是那句话, 具体问题具体具体分析, 要用实验的方法证明猜测. 祝各位周末愉快!

    作者:Jeffrey Sun
    出处:http://sun.cnblogs.com/
    本文以“现状”提供且没有任何担保,同时也没有授予任何权利。本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    KVM 重命名虚机
    甲醛了解
    递归函数,匿名函数
    函数
    zabbix监控URL
    zabbix自动发现
    vim常用命令总结
    saltstack常用命令
    zabbix监控Apache
    nginx配置详解
  • 原文地址:https://www.cnblogs.com/sun/p/1643163.html
Copyright © 2020-2023  润新知