-
-
Notifications
You must be signed in to change notification settings - Fork 711
/
Copy pathThreadsafeTypeKeyHashTable.cs
204 lines (178 loc) · 6.49 KB
/
ThreadsafeTypeKeyHashTable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
namespace MessagePack.Internal
{
// Safe for multiple-read, single-write.
internal class ThreadsafeTypeKeyHashTable<TValue>
{
Entry[] buckets;
int size; // only use in writer lock
readonly object writerLock = new object();
readonly float loadFactor;
// IEqualityComparer.Equals is overhead if key only Type, don't use it.
// readonly IEqualityComparer<TKey> comparer;
public ThreadsafeTypeKeyHashTable(int capacity = 4, float loadFactor = 0.75f)
{
var tableSize = CalculateCapacity(capacity, loadFactor);
this.buckets = new Entry[tableSize];
this.loadFactor = loadFactor;
}
public bool TryAdd(Type key, Func<Type, TValue> valueFactory)
{
TValue _;
return TryAddInternal(key, valueFactory, out _);
}
bool TryAddInternal(Type key, Func<Type, TValue> valueFactory, out TValue resultingValue)
{
lock (writerLock)
{
var nextCapacity = CalculateCapacity(size + 1, loadFactor);
if (buckets.Length < nextCapacity)
{
// rehash
var nextBucket = new Entry[nextCapacity];
for (int i = 0; i < buckets.Length; i++)
{
var e = buckets[i];
while (e != null)
{
var newEntry = new Entry { Key = e.Key, Value = e.Value, Hash = e.Hash };
AddToBuckets(nextBucket, key, newEntry, null, out resultingValue);
e = e.Next;
}
}
// add entry(if failed to add, only do resize)
var successAdd = AddToBuckets(nextBucket, key, null, valueFactory, out resultingValue);
// replace field(threadsafe for read)
System.Threading.Volatile.Write(ref buckets, nextBucket);
if (successAdd) size++;
return successAdd;
}
else
{
// add entry(insert last is thread safe for read)
var successAdd = AddToBuckets(buckets, key, null, valueFactory, out resultingValue);
if (successAdd) size++;
return successAdd;
}
}
}
bool AddToBuckets(Entry[] buckets, Type newKey, Entry newEntryOrNull, Func<Type, TValue> valueFactory, out TValue resultingValue)
{
var h = (newEntryOrNull != null) ? newEntryOrNull.Hash : newKey.GetHashCode();
if (buckets[h & (buckets.Length - 1)] == null)
{
if (newEntryOrNull != null)
{
resultingValue = newEntryOrNull.Value;
System.Threading.Volatile.Write(ref buckets[h & (buckets.Length - 1)], newEntryOrNull);
}
else
{
resultingValue = valueFactory(newKey);
System.Threading.Volatile.Write(ref buckets[h & (buckets.Length - 1)], new Entry { Key = newKey, Value = resultingValue, Hash = h });
}
}
else
{
var searchLastEntry = buckets[h & (buckets.Length - 1)];
while (true)
{
if (searchLastEntry.Key == newKey)
{
resultingValue = searchLastEntry.Value;
return false;
}
if (searchLastEntry.Next == null)
{
if (newEntryOrNull != null)
{
resultingValue = newEntryOrNull.Value;
System.Threading.Volatile.Write(ref searchLastEntry.Next, newEntryOrNull);
}
else
{
resultingValue = valueFactory(newKey);
System.Threading.Volatile.Write(ref searchLastEntry.Next, new Entry { Key = newKey, Value = resultingValue, Hash = h });
}
break;
}
searchLastEntry = searchLastEntry.Next;
}
}
return true;
}
public bool TryGetValue(Type key, out TValue value)
{
var table = buckets;
var hash = key.GetHashCode();
var entry = table[hash & table.Length - 1];
if (entry == null) goto NOT_FOUND;
if (entry.Key == key)
{
value = entry.Value;
return true;
}
var next = entry.Next;
while (next != null)
{
if (next.Key == key)
{
value = next.Value;
return true;
}
next = next.Next;
}
NOT_FOUND:
value = default(TValue);
return false;
}
public TValue GetOrAdd(Type key, Func<Type, TValue> valueFactory)
{
TValue v;
if (TryGetValue(key, out v))
{
return v;
}
TryAddInternal(key, valueFactory, out v);
return v;
}
static int CalculateCapacity(int collectionSize, float loadFactor)
{
var initialCapacity = (int)(((float)collectionSize) / loadFactor);
var capacity = 1;
while (capacity < initialCapacity)
{
capacity <<= 1;
}
if (capacity < 8)
{
return 8;
}
return capacity;
}
class Entry
{
public Type Key;
public TValue Value;
public int Hash;
public Entry Next;
// debug only
public override string ToString()
{
return Key + "(" + Count() + ")";
}
int Count()
{
var count = 1;
var n = this;
while (n.Next != null)
{
count++;
n = n.Next;
}
return count;
}
}
}
}