-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
852 lines (719 loc) · 25.5 KB
/
Program.cs
File metadata and controls
852 lines (719 loc) · 25.5 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
// #define ENABLE_STOPWATCH
// #define ENABLE_EXPANSION_LOG
// #define ENABLE_HANDLED_ENTRY_COUNTER
// #define ENABLE_WORKER_DEBUG_LOG
// #define USE_ONCE_WORKER
// #define DISABLE_RESULT_OUTPUT
using System.Buffers;
using System.Buffers.Text;
using System.IO.MemoryMappedFiles;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Text;
#if ENABLE_STOPWATCH
using System.Diagnostics;
#endif
var sequence512 = Vector512.Create(
(byte)0, 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
);
if (args.Length == 0)
{
Console.Error.Write("Please specify the dataset path.");
Environment.Exit(1);
return;
}
var filePath = args[0];
#if ENABLE_STOPWATCH
var stopwatch = Stopwatch.StartNew();
#endif
var resultLock = new Lock();
var totalResults = new FastGlobalDictionary();
var fi = new FileInfo(filePath);
var fileSize = fi.Length;
using var mmf = MemoryMappedFile.CreateFromFile(
filePath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read);
#if USE_ONCE_WORKER
const int coreCount = 1;
#else
var coreCount = Environment.ProcessorCount;
#endif
var chunkSizePerWorker = fileSize / coreCount;
#if ENABLE_HANDLED_ENTRY_COUNTER
var handledEntryCount = 0;
#endif
Parallel.For(0, coreCount, (i, _) =>
{
// ReSharper disable once AccessToDisposedClosure
DoWork(i, chunkSizePerWorker, mmf);
});
var first = true;
var delimiter = ", "u8;
Console.OutputEncoding = Encoding.UTF8;
var stdOutputStream = Console.OpenStandardOutput();
var accessIndexes = SortAccessIndexes(totalResults);
#if !DISABLE_RESULT_OUTPUT
stdOutputStream.WriteByte((byte)'{');
var floatFormat = new StandardFormat('F', 1);
#endif
Span<byte> floatingFormatBuffer = stackalloc byte[6];
for (var index = 0; index < accessIndexes.Length; index++)
{
var value = totalResults.Entries[accessIndexes[index]];
if (value.Count == 0)
{
continue;
}
if (!first)
{
#if !DISABLE_RESULT_OUTPUT
stdOutputStream.Write(delimiter);
#endif
}
#if !DISABLE_RESULT_OUTPUT
stdOutputStream.Write(value.Key);
stdOutputStream.WriteByte((byte)'=');
Utf8Formatter.TryFormat(Round(value.Min / 10.0), floatingFormatBuffer, out var written, floatFormat);
stdOutputStream.Write(floatingFormatBuffer[..written]);
stdOutputStream.WriteByte((byte)'/');
Utf8Formatter.TryFormat(Get1BrcAverage(value.Total, value.Count), floatingFormatBuffer, out written, floatFormat);
stdOutputStream.Write(floatingFormatBuffer[..written]);
stdOutputStream.WriteByte((byte)'/');
Utf8Formatter.TryFormat(Round(value.Max / 10.0), floatingFormatBuffer, out written, floatFormat);
stdOutputStream.Write(floatingFormatBuffer[..written]);
#endif
first = false;
}
#if !DISABLE_RESULT_OUTPUT
stdOutputStream.WriteByte((byte)'}');
stdOutputStream.WriteByte((byte)'\n');
#endif
#if ENABLE_STOPWATCH
stopwatch.Stop();
Console.Write('\n');
#if ENABLE_HANDLED_ENTRY_COUNTER
Console.Error.WriteLine($"Warning! When the processing entry counter is enabled, the execution time is unreliable!");
#endif
Console.WriteLine($"Execution Time: {stopwatch.Elapsed}ms.");
#endif
#if ENABLE_HANDLED_ENTRY_COUNTER
Console.WriteLine($"Handled Entry Count: {handledEntryCount}");
#endif
return;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
double Round(double value)
{
var rounded = Math.Floor(value * 10.0 + 0.5 + 1e-15);
return rounded / 10.0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static double Get1BrcAverage(long total, long count)
{
var valueToRound = (double)total / count;
return Math.Floor(valueToRound + 0.5 + 1e-15) / 10.0;
}
void DoWork(int i, long chunkSize, MemoryMappedFile mappedFile)
{
var localResult = new FastLocalDictionary();
unsafe
{
var startOffset = i * chunkSize;
var sizeToMap = Math.Min(chunkSize + 256, fileSize - startOffset);
#if ENABLE_WORKER_DEBUG_LOG
Console.WriteLine($"Worker {i} Started at {startOffset} - {startOffset + chunkSize} - {sizeToMap}");
#endif
using var accessor = mappedFile.CreateViewAccessor(startOffset, sizeToMap, MemoryMappedFileAccess.Read);
byte* basePtr = null;
accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref basePtr);
basePtr += accessor.PointerOffset;
var currentPtr = basePtr;
byte* endOfLogicalChunk;
var endOfView = basePtr + sizeToMap;
if (i == coreCount - 1)
{
endOfLogicalChunk = endOfView;
}
else
{
endOfLogicalChunk = basePtr + chunkSize;
}
if (i > 0)
{
#if ENABLE_WORKER_DEBUG_LOG
var skipCount = 0;
#endif
do
{
var (validLength, lineSeparatorMask) = FindLineSeparatorMask(currentPtr, endOfView - currentPtr);
if (lineSeparatorMask == 0)
{
currentPtr += validLength;
}
else
{
var firstLineSeparatorIndex = BitOperations.TrailingZeroCount(lineSeparatorMask);
currentPtr += firstLineSeparatorIndex + 1;
if (currentPtr >= endOfView)
{
return;
}
break;
}
#if ENABLE_WORKER_DEBUG_LOG
skipCount++;
#endif
} while(currentPtr < endOfView);
#if ENABLE_WORKER_DEBUG_LOG
Console.WriteLine($"Worker {i} Skipped {currentPtr - basePtr} at {skipCount} Iterators.");
#endif
}
var lineStartPtr = currentPtr;
byte* delimiterPtr = null;
while (currentPtr < endOfView)
{
var (moveStep, delimiterMask, lineSeparatorMask) = FindAllDelimiterMasks(currentPtr, endOfView - currentPtr);
while (!Vector512.IsHardwareAccelerated || lineSeparatorMask != 0)
{
var delimiterIndex = BitOperations.TrailingZeroCount(delimiterMask);
var lineSeparatorIndex = BitOperations.TrailingZeroCount(lineSeparatorMask);
if (Vector512.IsHardwareAccelerated)
{
delimiterPtr = currentPtr + delimiterIndex;
}
else
{
if (delimiterIndex != 64 && (lineSeparatorIndex == 64 || lineSeparatorIndex > delimiterIndex))
{
delimiterPtr = currentPtr + delimiterIndex;
}
if (lineSeparatorIndex == 64)
{
break;
}
}
var lineSeparatorPtr = currentPtr + lineSeparatorIndex;
var keyLength = delimiterPtr - lineStartPtr;
var value = FastParseFloatingToLong(delimiterPtr + 1, lineSeparatorPtr - delimiterPtr - 1);
localResult.UpdateOrAdd(lineStartPtr, (int)keyLength, value);
lineStartPtr = lineSeparatorPtr + 1;
lineSeparatorMask &= lineSeparatorMask - 1;
delimiterMask &= ~((1UL << (lineSeparatorIndex + 1)) - 1);
}
if (Vector512.IsHardwareAccelerated)
{
currentPtr = lineStartPtr;
} else {
currentPtr += moveStep;
}
if (currentPtr >= endOfLogicalChunk)
{
break;
}
}
#if ENABLE_WORKER_DEBUG_LOG
if (currentPtr > endOfView)
{
Console.WriteLine($"Worker {i} Ended on {(long)currentPtr:N0}: End Of View");
}
#endif
#if ENABLE_WORKER_DEBUG_LOG
Console.WriteLine($"Worker {i} Actual Ended at {(long)currentPtr:N0} (Actual Read {currentPtr - basePtr:N0}/{chunkSize}({currentPtr - basePtr - chunkSize}), OverRead {currentPtr - endOfLogicalChunk:N0})");
#endif
lock (resultLock)
{
for (var j = 0; j < localResult.Entries.Length; j++)
{
ref var entry = ref localResult.Entries[j];
if (entry.KeyLength == 0)
{
continue;
}
totalResults.UpdateOrAdd(ref entry);
}
}
accessor.SafeMemoryMappedViewHandle.ReleasePointer();
}
}
// 使用 AVX 快速查找分号和换行符.
// 返回的 4 个 byte 中, 第一个是这次移动了多少 byte, 第二个是分号的位置, 第三个是换行符的位置.
// 这里计划是把负号也加进去的, 但是可能会被 StationName 干扰, 因此不可行.
// 如果 StationName 明确不包括负号的情况下, 这里可以配合 Worker 实现快速符号位设置.
// 后面再试试.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe (uint, ulong, ulong) FindAllDelimiterMasks(byte* ptr, long maxSize)
{
var stepSize = Vector512.IsHardwareAccelerated ? 64u : 32u;
var size = (int)Math.Min(stepSize, maxSize);
if (size <= 0)
{
return (stepSize, 0, 0);
}
if (Vector512.IsHardwareAccelerated)
{
Vector512<byte> data;
if (size == stepSize)
{
data = Vector512.Load(ptr);
}
else
{
var vN = Vector512.Create((byte)size);
data = Avx512BW.MaskLoad(ptr, Vector512.LessThan(sequence512, vN), Vector512<byte>.Zero);
}
var delimiterMask = Vector512.Create((byte)';');
var lineSeparatorMask = Vector512.Create((byte)'\n');
var delimiterResult = Vector512.Equals(data, delimiterMask);
var lineSeparatorResult = Vector512.Equals(data, lineSeparatorMask);
var delimiterResultMask = delimiterResult.ExtractMostSignificantBits();
var lineSeparatorResultMask = lineSeparatorResult.ExtractMostSignificantBits();
return (stepSize, delimiterResultMask, lineSeparatorResultMask);
}
else
{
var delimiterMask = Vector256.Create((byte)';');
var lineSeparatorMask = Vector256.Create((byte)'\n');
if (size == stepSize)
{
var data = Vector256.Load(ptr);
var delimiterResult = Vector256.Equals(data, delimiterMask);
var lineSeparatorResult = Vector256.Equals(data, lineSeparatorMask);
var delimiterResultMask = delimiterResult.ExtractMostSignificantBits();
var lineSeparatorResultMask = lineSeparatorResult.ExtractMostSignificantBits();
return (stepSize, delimiterResultMask, lineSeparatorResultMask);
}
else
{
var fullInts = size >> 2; // n / 4 向下
var tail = size & 3; // n % 4
var indices = Vector256.Create(0, 1, 2, 3, 4, 5, 6, 7);
var mask = Vector256.LessThan(indices, Vector256.Create(fullInts));
var data = Unsafe.BitCast<Vector256<int>, Vector256<byte>>(Avx2.MaskLoad((int*)ptr, mask));
var delimiterResult = Vector256.Equals(data, delimiterMask);
var lineSeparatorResult = Vector256.Equals(data, lineSeparatorMask);
var delimiterResultMask = delimiterResult.ExtractMostSignificantBits();
var lineSeparatorResultMask = lineSeparatorResult.ExtractMostSignificantBits();
var baseOffset = fullInts << 2;
for (var i = 0; i < tail; i++)
{
switch (ptr[baseOffset + i])
{
case (byte)'\n':
lineSeparatorResultMask |= 1u << (baseOffset + i);
break;
case (byte)';':
delimiterResultMask |= 1u << (baseOffset + i);
break;
}
}
return (stepSize, delimiterResultMask, lineSeparatorResultMask);
}
}
}
// 第一位是有效输出字节数, 如果支持启用 AVX512, 则固定为 64, 如果降级到 AVX2, 则固定是 32.
// 第二个是掩码结果.
// 分两个出来可以避免无意义的计算, 加快 Worker 启动速度.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe (uint, ulong) FindLineSeparatorMask(byte* ptr, long maxSize)
{
var stepSize = Vector512.IsHardwareAccelerated ? 64u : 32u;
var size = (int)Math.Min(stepSize, maxSize);
if (size <= 0)
{
return (stepSize, 0);
}
if (Vector512.IsHardwareAccelerated)
{
var vN = Vector512.Create((byte)size);
var data = Avx512BW.MaskLoad(ptr, Vector512.LessThan(sequence512, vN), Vector512<byte>.Zero);
var lineSeparatorMask = Vector512.Create((byte)'\n');
var lineSeparatorResult = Vector512.Equals(data, lineSeparatorMask);
var lineSeparatorResultMask = lineSeparatorResult.ExtractMostSignificantBits();
return (stepSize, lineSeparatorResultMask);
}
var nl = Vector256.Create((byte)'\n');
if (size == 32)
{
var data = Vector256.Load(ptr);
var cmp = Vector256.Equals(data, nl);
var m = cmp.ExtractMostSignificantBits();
return (stepSize, m);
}
else
{
var fullInts = size >> 2; // n / 4 向下
var tail = size & 3; // n % 4
var indices = Vector256.Create(0, 1, 2, 3, 4, 5, 6, 7);
var mask = Vector256.LessThan(indices, Vector256.Create(fullInts));
var data = Unsafe.BitCast<Vector256<int>, Vector256<byte>>(Avx2.MaskLoad((int*)ptr, mask));
var lineSeparatorResult = Avx2.CompareEqual(data, nl);
var lineSeparatorMask = lineSeparatorResult.ExtractMostSignificantBits();
// 走循环处理末尾不足位
var baseOffset = fullInts << 2;
for (var i = 0; i < tail; i++)
{
if (ptr[baseOffset + i] == (byte)'\n')
{
lineSeparatorMask |= 1u << (baseOffset + i);
}
}
lineSeparatorMask &= (uint)((1UL << size) - 1UL);
return (stepSize, lineSeparatorMask);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe long FastParseFloatingToLong(byte* ptr, long len)
{
var isNeg = *ptr - 0x2D == 0 ? 1L : 0L;
var signMask = -isNeg;
ptr += isNeg;
len -= isNeg;
var result = len switch
{
// \d\.\d
3 => (ptr[0] - '0') * 10 + (ptr[2] - '0'),
// \d{2}\.\d
4 => (ptr[0] - '0') * 100 + (ptr[1] - '0') * 10 + (ptr[3] - '0'),
_ => 0
};
return (result ^ signMask) - signMask;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
int[] SortAccessIndexes(FastGlobalDictionary dictionary)
{
var indexes = new int[dictionary.Count];
var actualIndex = 0;
for (var i = 0; i < dictionary.Entries.Length; i++)
{
if (dictionary.Entries[i].Count > 0)
{
indexes[actualIndex++] = i;
}
}
indexes.Sort((i1, i2) => totalResults.Entries[i1].Key.AsSpan()
.SequenceCompareTo(totalResults.Entries[i2].Key.AsSpan()));
return indexes;
}
internal struct DataEntry
{
public ulong Hash;
public byte[] Key;
public long Total;
public long Count;
public long Min;
public long Max;
}
[StructLayout(LayoutKind.Sequential, Size = 64)]
internal unsafe struct FastDataEntry
{
public ulong Hash;
public byte* KeyOffset;
public int KeyLength;
public long Total;
public long Count;
public long Min;
public long Max;
}
internal unsafe class FastLocalDictionary
{
#if USE_ONCE_WORKER
public FastDataEntry[] Entries = new FastDataEntry[30000];
#else
public FastDataEntry[] Entries = new FastDataEntry[1024];
private int _count;
#endif
#if ENABLE_EXPANSION_LOG
private int _expansionCount;
#endif
private ref FastDataEntry GetOrAddDefault(byte* key, int keyLength, out bool exists)
{
var hash = FastHash(key, keyLength);
ref var entry = ref GetOrAddDefault0(Entries, hash, key, keyLength, out var flag);
if (flag == 2)
{
Expansion();
entry = ref GetOrAddDefault0(Entries, hash, key, keyLength, out flag);
}
exists = flag == 0;
#if !USE_ONCE_WORKER
if (!exists)
{
_count++;
if (_count >= Entries.Length * 1)
{
Expansion();
}
}
#endif
return ref entry;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool CompareKey(byte* a, byte* b, int length)
{
// ---------------------------------------------------------
// 第一梯队:最常见的长度区间 (8 到 16 字节)
// 1BRC 中大量的城市名落在这里,例如 "San Francisco" (13)
// ---------------------------------------------------------
if (length >= 8)
{
if (length <= 16)
{
// 1. 先比头 8 个字节 (把指针当 ulong 读)
if (*(ulong*)a != *(ulong*)b)
{
return false;
}
// 2. 再比尾 8 个字节 (Overlapping 重叠读取)
// 哪怕 length 是 8,这行代码也是安全的(原地重叠)
return *(ulong*)(a + length - 8) == *(ulong*)(b + length - 8);
}
// ---------------------------------------------------------
// 第二梯队:长 Key (> 16 字节)
// 比如 "City of ...", "North ...", 或者 100 字节的长串
// ---------------------------------------------------------
return CompareKeyLong(a, b, length);
}
// ---------------------------------------------------------
// 第三梯队:短 Key (< 8 字节)
// ---------------------------------------------------------
// 如果 >= 4 (即 4, 5, 6, 7)
if (length >= 4)
{
// 同样的逻辑:比头 4 字节(int),比尾 4 字节(int)
return *(uint*)a == *(uint*)b &&
*(uint*)(a + length - 4) == *(uint*)(b + length - 4);
}
// 极短 Key (0, 1, 2, 3)
// 这种太短了,直接暴力比字节,或者用 switch
if (length > 0)
{
if (*a != *b) return false;
if (length > 1)
{
if (*(ushort*)(a + length - 2) != *(ushort*)(b + length - 2)) return false;
}
}
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool CompareKeyLong(byte* a, byte* b, int length)
{
return new ReadOnlySpan<byte>(a, length).SequenceEqual(new ReadOnlySpan<byte>(b, length));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ref FastDataEntry GetOrAddDefault0(FastDataEntry[] entries, ulong hash, byte* keyOffset,
int keyLength,
out int flag)
{
var index = hash & ((uint)entries.Length - 1);
if (entries[index].Hash == 0)
{
flag = 1;
entries[index].Hash = hash;
entries[index].KeyOffset = keyOffset;
entries[index].KeyLength = keyLength;
return ref entries[index];
}
if (entries[index].Hash == hash
&& entries[index].KeyLength == keyLength
&& CompareKey(entries[index].KeyOffset, keyOffset, keyLength))
{
flag = 0;
return ref entries[index];
}
var i = 0;
do
{
index = (index + 1) & ((uint)entries.Length - 1);
if (entries[index].Hash == 0)
{
flag = 1;
entries[index].Hash = hash;
entries[index].KeyOffset = keyOffset;
entries[index].KeyLength = keyLength;
return ref entries[index];
}
if (entries[index].Hash == hash
&& entries[index].KeyLength == keyLength
&& CompareKey(entries[index].KeyOffset, keyOffset, keyLength))
{
flag = 0;
return ref entries[index];
}
i++;
} while (i < entries.Length);
flag = 2;
return ref entries[index];
}
public void UpdateOrAdd(byte* key, int keyLength, long data)
{
ref var entry = ref GetOrAddDefault(key, keyLength, out var exists);
entry.Count++;
entry.Total += data;
if (!exists || entry.Max < data)
{
entry.Max = data;
}
if (!exists || entry.Min > data)
{
entry.Min = data;
}
}
private void Expansion()
{
var newEntries = new FastDataEntry[Entries.Length * 2];
for (var i = 0; i < Entries.Length; i++)
{
ref var newSlot = ref GetOrAddDefault0(newEntries, Entries[i].Hash, Entries[i].KeyOffset,
Entries[i].KeyLength, out _);
newSlot.Total = Entries[i].Total;
newSlot.Count = Entries[i].Count;
newSlot.Min = Entries[i].Min;
newSlot.Max = Entries[i].Max;
}
#if ENABLE_EXPANSION_LOG
Console.WriteLine($"Expansion Completed: {Entries.Length} -> {newEntries.Length}, Count: {++_expansionCount}");
#endif
Entries = newEntries;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong FastHash(byte* ptr, int length)
{
var hash = 2166136261;
for (var i = 0; i < length; i++)
{
hash ^= *(ptr + i);
hash *= 16777619;
}
return hash;
}
}
internal unsafe class FastGlobalDictionary
{
#if USE_ONCE_WORKER
public DataEntry[] Entries = new DataEntry[30000];
#else
public DataEntry[] Entries = new DataEntry[1024];
#endif
public int Count;
#if ENABLE_EXPANSION_LOG
private int _expansionCount;
#endif
private ref DataEntry GetOrAddDefault(byte* key, int keyLength, out bool exists)
{
var hash = FastHash(key, keyLength);
var keyArray = new byte[keyLength];
ref var dest = ref MemoryMarshal.GetArrayDataReference(keyArray);
Unsafe.CopyBlock(ref dest, ref *key, (uint)keyLength);
ref var entry = ref GetOrAddDefault0(Entries, hash, keyArray, out var flag);
if (flag == 2)
{
Expansion();
entry = ref GetOrAddDefault0(Entries, hash, keyArray, out flag);
}
exists = flag == 0;
if (!exists)
{
Count++;
#if !USE_ONCE_WORKER
if (Count >= Entries.Length * 1)
{
Expansion();
}
#endif
}
return ref entry;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool CompareKey(byte[] a, byte[] b)
{
return new ReadOnlySpan<byte>(a).SequenceEqual(b);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ref DataEntry GetOrAddDefault0(DataEntry[] entries, ulong hash, byte[] key,
out int flag)
{
var index = hash & ((uint)entries.Length - 1);
if (entries[index].Hash == 0)
{
flag = 1;
entries[index].Hash = hash;
entries[index].Key = key;
return ref entries[index];
}
if (entries[index].Hash == hash
&& entries[index].Key.Length == key.Length
&& CompareKey(entries[index].Key, key))
{
flag = 0;
return ref entries[index];
}
var i = 0;
do
{
index = (index + 1) & ((uint)entries.Length - 1);
if (entries[index].Hash == 0)
{
flag = 1;
entries[index].Hash = hash;
entries[index].Key = key;
return ref entries[index];
}
if (entries[index].Hash == hash
&& entries[index].Key.Length == key.Length
&& CompareKey(entries[index].Key, key))
{
flag = 0;
return ref entries[index];
}
i++;
} while (i < entries.Length);
flag = 2;
return ref entries[index];
}
public void UpdateOrAdd(ref FastDataEntry incoming)
{
ref var entry = ref GetOrAddDefault(incoming.KeyOffset, incoming.KeyLength, out var exists);
entry.Count += incoming.Count;
entry.Total += incoming.Total;
if (!exists || entry.Max < incoming.Max)
{
entry.Max = incoming.Max;
}
if (!exists || entry.Min > incoming.Min)
{
entry.Min = incoming.Min;
}
}
private void Expansion()
{
var newEntries = new DataEntry[Entries.Length * 2];
for (var i = 0; i < Entries.Length; i++)
{
ref var newSlot = ref GetOrAddDefault0(newEntries, Entries[i].Hash, Entries[i].Key, out _);
newSlot.Total += Entries[i].Total;
newSlot.Count = Entries[i].Count;
newSlot.Min = Entries[i].Min;
newSlot.Max = Entries[i].Max;
}
#if ENABLE_EXPANSION_LOG
Console.WriteLine($"Expansion Completed: {Entries.Length} -> {newEntries.Length}, Count: {++_expansionCount}");
#endif
Entries = newEntries;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong FastHash(byte* ptr, int length)
{
var hash = 2166136261;
for (var i = 0; i < length; i++)
{
hash ^= *(ptr + i);
hash *= 16777619;
}
return hash;
}
}