|
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The gRPC Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#endregion |
| 18 | + |
| 19 | +using System.Collections.Concurrent; |
| 20 | + |
| 21 | +internal sealed class ThreadSafeLookup<TKey, TValue> where TKey : notnull |
| 22 | +{ |
| 23 | + // Avoid allocating ConcurrentDictionary until the threshold is reached. |
| 24 | + // Looking up a key in an array is as fast as a dictionary for small collections and uses much less memory. |
| 25 | + internal const int Threshold = 10; |
| 26 | + |
| 27 | + private KeyValuePair<TKey, TValue>[] _array = Array.Empty<KeyValuePair<TKey, TValue>>(); |
| 28 | + private ConcurrentDictionary<TKey, TValue>? _dictionary; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets the value for the key if it exists. If the key does not exist then the value is created using the valueFactory. |
| 32 | + /// The value is created outside of a lock and there is no guarentee which value will be stored or returned. |
| 33 | + /// </summary> |
| 34 | + public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory) |
| 35 | + { |
| 36 | + if (_dictionary != null) |
| 37 | + { |
| 38 | + return _dictionary.GetOrAdd(key, valueFactory); |
| 39 | + } |
| 40 | + |
| 41 | + if (TryGetValue(_array, key, out var value)) |
| 42 | + { |
| 43 | + return value; |
| 44 | + } |
| 45 | + |
| 46 | + var newValue = valueFactory(key); |
| 47 | + |
| 48 | + lock (this) |
| 49 | + { |
| 50 | + if (_dictionary != null) |
| 51 | + { |
| 52 | + _dictionary.TryAdd(key, newValue); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + // Double check inside lock if the key was added to the array by another thread. |
| 57 | + if (TryGetValue(_array, key, out value)) |
| 58 | + { |
| 59 | + return value; |
| 60 | + } |
| 61 | + |
| 62 | + if (_array.Length > Threshold - 1) |
| 63 | + { |
| 64 | + // Array length exceeds threshold so switch to dictionary. |
| 65 | + var newDict = new ConcurrentDictionary<TKey, TValue>(); |
| 66 | + foreach (var kvp in _array) |
| 67 | + { |
| 68 | + newDict.TryAdd(kvp.Key, kvp.Value); |
| 69 | + } |
| 70 | + newDict.TryAdd(key, newValue); |
| 71 | + |
| 72 | + _dictionary = newDict; |
| 73 | + _array = Array.Empty<KeyValuePair<TKey, TValue>>(); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + // Add new value by creating a new array with old plus new value. |
| 78 | + var newArray = new KeyValuePair<TKey, TValue>[_array.Length + 1]; |
| 79 | + Array.Copy(_array, newArray, _array.Length); |
| 80 | + newArray[newArray.Length - 1] = new KeyValuePair<TKey, TValue>(key, newValue); |
| 81 | + |
| 82 | + _array = newArray; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return newValue; |
| 88 | + } |
| 89 | + |
| 90 | + private static bool TryGetValue(KeyValuePair<TKey, TValue>[] array, TKey key, out TValue value) |
| 91 | + { |
| 92 | + foreach (var kvp in array) |
| 93 | + { |
| 94 | + if (EqualityComparer<TKey>.Default.Equals(kvp.Key, key)) |
| 95 | + { |
| 96 | + value = kvp.Value; |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + value = default!; |
| 102 | + return false; |
| 103 | + } |
| 104 | +} |
0 commit comments