-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeHashSetData.cs
340 lines (307 loc) · 15.5 KB
/
NativeHashSetData.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs.LowLevel.Unsafe;
using Unity.Mathematics;
using UnityEngine.Assertions;
namespace NativeContainers {
[StructLayout(LayoutKind.Sequential)]
public unsafe struct NativeHashSetData {
byte* values;
byte* next;
byte* buckets;
int valueCapacity;
int bucketCapacityMask;
// Adding padding to ensure remaining fields are on separate cache-lines
fixed byte padding[60];
fixed int firstFreeTLS[JobsUtility.MaxJobThreadCount * IntsPerCacheLine];
int allocatedIndexLength;
const int IntsPerCacheLine = JobsUtility.CacheLineSize / sizeof(int);
public int Capacity => valueCapacity;
public int Length {
get {
int* nextPtrs = (int*)next;
int freeListSize = 0;
for(int tls = 0; tls < JobsUtility.MaxJobThreadCount; ++tls) {
int freeIdx = firstFreeTLS[tls * IntsPerCacheLine] - 1;
for(; freeIdx >= 0; freeListSize++, freeIdx = nextPtrs[freeIdx] - 1) {}
}
return math.min(valueCapacity, allocatedIndexLength) - freeListSize;
}
}
static int DoubleCapacity(int capacity) => capacity == 0 ? 1 : capacity * 2;
public static void AllocateHashSet<T>(
int capacity, Allocator label, out NativeHashSetData* buffer) where T : struct {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if(!UnsafeUtility.IsBlittable<T>())
throw new ArgumentException($"{typeof(T)} used in NativeHashSet<{typeof(T)}> must be blittable");
#endif
var data = (NativeHashSetData*)UnsafeUtility.Malloc(
sizeof(NativeHashSetData), UnsafeUtility.AlignOf<NativeHashSetData>(), label);
int bucketCapacity = math.ceilpow2(capacity * 2);
data->valueCapacity = capacity;
data->bucketCapacityMask = bucketCapacity - 1;
int nextOffset, bucketOffset;
int totalSize = CalculateDataSize<T>(capacity, bucketCapacity, out nextOffset, out bucketOffset);
data->values = (byte*)UnsafeUtility.Malloc(totalSize, JobsUtility.CacheLineSize, label);
data->next = data->values + nextOffset;
data->buckets = data->values + bucketOffset;
buffer = data;
}
public static void DeallocateHashSet(NativeHashSetData* data, Allocator allocator) {
UnsafeUtility.Free(data->values, allocator);
data->values = null;
data->buckets = null;
data->next = null;
UnsafeUtility.Free(data, allocator);
}
public void Clear() {
UnsafeUtility.MemClear((int*)buckets, sizeof(int) * (bucketCapacityMask + 1));
UnsafeUtility.MemClear((int*)next, sizeof(int) * valueCapacity);
fixed(int* firstFreeTLS = this.firstFreeTLS) {
UnsafeUtility.MemClear(
firstFreeTLS, sizeof(int) * (JobsUtility.MaxJobThreadCount * IntsPerCacheLine));
}
allocatedIndexLength = 0;
}
public void GetValueArray<T>(NativeArray<T> result) where T : struct {
var buckets = (int*)this.buckets;
var nextPtrs = (int*)next;
int outputIndex = 0;
for(int bucketIndex = 0; bucketIndex <= bucketCapacityMask; ++bucketIndex) {
int valuesIndex = buckets[bucketIndex];
while(valuesIndex > 0) {
result[outputIndex] = UnsafeUtility.ReadArrayElement<T>(values, valuesIndex - 1);
outputIndex++;
valuesIndex = nextPtrs[valuesIndex - 1];
}
}
Assert.AreEqual(result.Length, outputIndex);
}
public bool TryAdd<T>(ref T value, Allocator allocator) where T : struct, IEquatable<T> {
if(Contains(ref value)) {
return false;
}
int valuesIdx = FindFirstFreeIndex<T>(allocator);
UnsafeUtility.WriteArrayElement(values, valuesIdx, value);
// Add the index to the hashset
int* buckets = (int*)this.buckets;
int* nextPtrs = (int*)next;
int bucketIndex = value.GetHashCode() & bucketCapacityMask;
nextPtrs[valuesIdx] = buckets[bucketIndex];
buckets[bucketIndex] = valuesIdx + 1;
return true;
}
public bool TryAddThreaded<T>(ref T value, int threadIndex) where T : IEquatable<T> {
if(Contains(ref value)) {
return false;
}
// Allocate an entry from the free list
int idx = FindFreeIndexFromTLS(threadIndex);
UnsafeUtility.WriteArrayElement(values, idx, value);
// Add the index to the hashset
int* buckets = (int*)this.buckets;
int bucket = value.GetHashCode() & bucketCapacityMask;
int* nextPtrs = (int*)next;
if(Interlocked.CompareExchange(ref buckets[bucket], idx + 1, 0) != 0) {
do {
nextPtrs[idx] = buckets[bucket];
if(Contains(ref value)) {
// Put back the entry in the free list if someone else added it while trying to add
do {
nextPtrs[idx] = firstFreeTLS[threadIndex * IntsPerCacheLine];
} while(Interlocked.CompareExchange(
ref firstFreeTLS[threadIndex * IntsPerCacheLine], idx + 1,
nextPtrs[idx]) != nextPtrs[idx]);
return false;
}
} while(Interlocked.CompareExchange(ref buckets[bucket], idx + 1, nextPtrs[idx]) != nextPtrs[idx]);
}
return true;
}
public bool Contains<T>(ref T value) where T : IEquatable<T> {
if(allocatedIndexLength <= 0) {
return false;
}
int* buckets = (int*)this.buckets;
int* nextPtrs = (int*)next;
int bucket = value.GetHashCode() & bucketCapacityMask;
int valuesIdx = buckets[bucket] - 1;
while(valuesIdx >= 0 && valuesIdx < valueCapacity) {
if(UnsafeUtility.ReadArrayElement<T>(values, valuesIdx).Equals(value)) {
return true;
}
valuesIdx = nextPtrs[valuesIdx] - 1;
}
return false;
}
public bool TryRemove<T>(T key) where T : struct, IEquatable<T> {
int* buckets = (int*)this.buckets;
int* nextPtrs = (int*)next;
int bucketIdx = key.GetHashCode() & bucketCapacityMask;
int valuesIdx = buckets[bucketIdx] - 1;
int prevValuesIdx = -1;
while(valuesIdx >= 0 && valuesIdx < valueCapacity) {
if(UnsafeUtility.ReadArrayElement<T>(values, valuesIdx).Equals(key)) {
if(prevValuesIdx == -1) {
// Sets head->next to head->next->next(or -1)
buckets[bucketIdx] = nextPtrs[valuesIdx];
}
else {
// Sets prev->next to prev->next(current valuesIdx)->next
nextPtrs[prevValuesIdx] = nextPtrs[valuesIdx];
}
// Mark the index as free
nextPtrs[valuesIdx] = firstFreeTLS[0];
firstFreeTLS[0] = valuesIdx + 1;
return true;
}
prevValuesIdx = valuesIdx;
valuesIdx = nextPtrs[valuesIdx] - 1;
}
return false;
}
static int CalculateDataSize<T>(
int capacity, int bucketCapacity, out int nextOffset, out int bucketOffset) where T : struct {
nextOffset = (UnsafeUtility.SizeOf<T>() * capacity) + JobsUtility.CacheLineSize - 1;
nextOffset -= nextOffset % JobsUtility.CacheLineSize;
bucketOffset = nextOffset + (sizeof(int) * capacity) + JobsUtility.CacheLineSize - 1;
bucketOffset -= bucketOffset % JobsUtility.CacheLineSize;
return bucketOffset + (sizeof(int) * bucketCapacity);
}
int FindFirstFreeIndex<T>(Allocator allocator) where T : struct {
int valuesIdx;
int* nextPtrs = (int*)next;
// Try to find an index in another TLS.
if(allocatedIndexLength >= valueCapacity && firstFreeTLS[0] == 0) {
for(int tls = 1; tls < JobsUtility.MaxJobThreadCount; ++tls) {
int tlsIndex = tls * IntsPerCacheLine;
if(firstFreeTLS[tlsIndex] > 0) {
valuesIdx = firstFreeTLS[tlsIndex] - 1;
firstFreeTLS[tlsIndex] = nextPtrs[valuesIdx];
nextPtrs[valuesIdx] = 0;
firstFreeTLS[0] = valuesIdx + 1;
break;
}
}
// No indexes found.
if(firstFreeTLS[0] == 0) {
GrowHashSet<T>(DoubleCapacity(valueCapacity), allocator);
}
}
if(firstFreeTLS[0] == 0) {
valuesIdx = allocatedIndexLength;
allocatedIndexLength++;
}
else {
valuesIdx = firstFreeTLS[0] - 1;
firstFreeTLS[0] = nextPtrs[valuesIdx];
}
if(!(valuesIdx >= 0 && valuesIdx < valueCapacity)) {
throw new InvalidOperationException(
$"Internal HashSet error, values index: {valuesIdx} not in range of 0 and {valueCapacity}");
}
return valuesIdx;
}
int FindFreeIndexFromTLS(int threadIndex) {
int idx;
int* nextPtrs = (int*)next;
int thisTLSIndex = threadIndex * IntsPerCacheLine;
do {
idx = firstFreeTLS[thisTLSIndex] - 1;
if(idx < 0) {
// Mark this TLS index as locked
Interlocked.Exchange(ref firstFreeTLS[thisTLSIndex], -1);
// Try to allocate more indexes with this TLS
if(allocatedIndexLength < valueCapacity) {
idx = Interlocked.Add(ref allocatedIndexLength, 16) - 16;
if(idx < valueCapacity - 1) {
int count = math.min(16, valueCapacity - idx) - 1;
for(int i = 1; i < count; ++i) {
nextPtrs[idx + i] = (idx + 1) + i + 1;
}
nextPtrs[idx + count] = 0;
nextPtrs[idx] = 0;
Interlocked.Exchange(ref firstFreeTLS[thisTLSIndex], (idx + 1) + 1);
return idx;
}
if(idx == valueCapacity - 1) {
Interlocked.Exchange(ref firstFreeTLS[thisTLSIndex], 0);
return idx;
}
}
Interlocked.Exchange(ref firstFreeTLS[thisTLSIndex], 0);
// Could not find an index, try to steal one from another TLS
for(bool iterateAgain = true; iterateAgain;) {
iterateAgain = false;
for(int i = 1; i < JobsUtility.MaxJobThreadCount; i++) {
int nextTLSIndex = ((threadIndex + i) % JobsUtility.MaxJobThreadCount) * IntsPerCacheLine;
do {
idx = firstFreeTLS[nextTLSIndex] - 1;
} while(idx >= 0 && Interlocked.CompareExchange(
ref firstFreeTLS[nextTLSIndex], nextPtrs[idx], idx + 1) != idx + 1);
if(idx == -1) {
iterateAgain = true;
}
else if(idx >= 0) {
nextPtrs[idx] = 0;
return idx;
}
}
}
throw new InvalidOperationException("HashSet has reached capacity, cannot add more.");
}
if(idx > valueCapacity) {
throw new InvalidOperationException($"nextPtr idx {idx} beyond capacity {valueCapacity}");
}
// Another thread is using this TLS, try again.
} while(Interlocked.CompareExchange(
ref firstFreeTLS[threadIndex * IntsPerCacheLine], nextPtrs[idx], idx + 1) != idx + 1);
nextPtrs[idx] = 0;
return idx;
}
void GrowHashSet<T>(int newCapacity, Allocator allocator) where T : struct {
int newBucketCapacity = math.ceilpow2(newCapacity * 2);
if(newCapacity == valueCapacity && newBucketCapacity == (bucketCapacityMask + 1)) {
return;
}
if(valueCapacity > newCapacity) {
throw new ArgumentException("Shrinking a hashset is not supported");
}
int nextOffset, bucketOffset;
int totalSize = CalculateDataSize<T>(newCapacity, newBucketCapacity, out nextOffset, out bucketOffset);
byte* newValues = (byte*)UnsafeUtility.Malloc(totalSize, JobsUtility.CacheLineSize, allocator);
byte* newNext = newValues + nextOffset;
byte* newBuckets = newValues + bucketOffset;
UnsafeUtility.MemClear(newNext, sizeof(int) * newCapacity);
UnsafeUtility.MemCpy(newValues, values, UnsafeUtility.SizeOf<T>() * valueCapacity);
UnsafeUtility.MemCpy(newNext, next, sizeof(int) * valueCapacity);
// Re-hash the buckets, first clear the new buckets, then reinsert.
UnsafeUtility.MemClear(newBuckets, sizeof(int) * newBucketCapacity);
int* oldBuckets = (int*)buckets;
int* newNextPtrs = (int*)newNext;
for(int oldBucket = 0; oldBucket <= bucketCapacityMask; ++oldBucket) {
int curValuesIdx = oldBuckets[oldBucket] - 1;
while(curValuesIdx >= 0 && curValuesIdx < valueCapacity) {
var curValue = UnsafeUtility.ReadArrayElement<T>(values, curValuesIdx);
int newBucket = curValue.GetHashCode() & newBucketCapacity - 1;
oldBuckets[oldBucket] = newNextPtrs[curValuesIdx];
newNextPtrs[curValuesIdx] = ((int*)newBuckets)[newBucket];
((int*)newBuckets)[newBucket] = curValuesIdx + 1;
curValuesIdx = oldBuckets[oldBucket] - 1;
}
}
UnsafeUtility.Free(values, allocator);
if(allocatedIndexLength > valueCapacity) {
allocatedIndexLength = valueCapacity;
}
values = newValues;
next = newNext;
buckets = newBuckets;
valueCapacity = newCapacity;
bucketCapacityMask = newBucketCapacity - 1;
}
}
}