|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | + |
| 7 | +namespace StackExchange.Redis.Benchmarks; |
| 8 | + |
| 9 | +[Config(typeof(CustomConfig))] |
| 10 | +public class FastHashBenchmarks |
| 11 | +{ |
| 12 | + private const string SharedString = "some-typical-data-for-comparisons"; |
| 13 | + private static readonly byte[] SharedUtf8; |
| 14 | + private static readonly ReadOnlySequence<byte> SharedMultiSegment; |
| 15 | + |
| 16 | + static FastHashBenchmarks() |
| 17 | + { |
| 18 | + SharedUtf8 = Encoding.UTF8.GetBytes(SharedString); |
| 19 | + |
| 20 | + var first = new Segment(SharedUtf8.AsMemory(0, 1), null); |
| 21 | + var second = new Segment(SharedUtf8.AsMemory(1), first); |
| 22 | + SharedMultiSegment = new ReadOnlySequence<byte>(first, 0, second, second.Memory.Length); |
| 23 | + } |
| 24 | + |
| 25 | + private sealed class Segment : ReadOnlySequenceSegment<byte> |
| 26 | + { |
| 27 | + public Segment(ReadOnlyMemory<byte> memory, Segment? previous) |
| 28 | + { |
| 29 | + Memory = memory; |
| 30 | + if (previous is { }) |
| 31 | + { |
| 32 | + RunningIndex = previous.RunningIndex + previous.Memory.Length; |
| 33 | + previous.Next = this; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private string _sourceString = SharedString; |
| 39 | + private ReadOnlyMemory<byte> _sourceBytes = SharedUtf8; |
| 40 | + private ReadOnlySequence<byte> _sourceMultiSegmentBytes = SharedMultiSegment; |
| 41 | + private ReadOnlySequence<byte> SingleSegmentBytes => new(_sourceBytes); |
| 42 | + |
| 43 | + [GlobalSetup] |
| 44 | + public void Setup() |
| 45 | + { |
| 46 | + _sourceString = SharedString.Substring(0, Size); |
| 47 | + _sourceBytes = SharedUtf8.AsMemory(0, Size); |
| 48 | + _sourceMultiSegmentBytes = SharedMultiSegment.Slice(0, Size); |
| 49 | + |
| 50 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 51 | + var bytes = _sourceBytes.Span; |
| 52 | + var expected = FastHash.Hash64Fallback(bytes); |
| 53 | + |
| 54 | + Assert(bytes.Hash64(), nameof(FastHash.Hash64)); |
| 55 | + Assert(FastHash.Hash64Unsafe(bytes), nameof(FastHash.Hash64Unsafe)); |
| 56 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 57 | + Assert(SingleSegmentBytes.Hash64(), nameof(FastHash.Hash64) + " (single segment)"); |
| 58 | + Assert(_sourceMultiSegmentBytes.Hash64(), nameof(FastHash.Hash64) + " (multi segment)"); |
| 59 | + |
| 60 | + void Assert(long actual, string name) |
| 61 | + { |
| 62 | + if (actual != expected) |
| 63 | + { |
| 64 | + throw new InvalidOperationException($"Hash mismatch for {name}, {expected} != {actual}"); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + [ParamsSource(nameof(Sizes))] |
| 70 | + public int Size { get; set; } = 7; |
| 71 | + |
| 72 | + public IEnumerable<int> Sizes => [0, 1, 2, 3, 4, 5, 6, 7, 8, 16]; |
| 73 | + |
| 74 | + private const int OperationsPerInvoke = 1024; |
| 75 | + |
| 76 | + [Benchmark(OperationsPerInvoke = OperationsPerInvoke, Baseline = true)] |
| 77 | + public void String() |
| 78 | + { |
| 79 | + var val = _sourceString; |
| 80 | + for (int i = 0; i < OperationsPerInvoke; i++) |
| 81 | + { |
| 82 | + _ = val.GetHashCode(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] |
| 87 | + public void Hash64() |
| 88 | + { |
| 89 | + var val = _sourceBytes.Span; |
| 90 | + for (int i = 0; i < OperationsPerInvoke; i++) |
| 91 | + { |
| 92 | + _ = val.Hash64(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] |
| 97 | + public void Hash64Unsafe() |
| 98 | + { |
| 99 | + var val = _sourceBytes.Span; |
| 100 | + for (int i = 0; i < OperationsPerInvoke; i++) |
| 101 | + { |
| 102 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 103 | + _ = FastHash.Hash64Unsafe(val); |
| 104 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] |
| 109 | + public void Hash64Fallback() |
| 110 | + { |
| 111 | + var val = _sourceBytes.Span; |
| 112 | + for (int i = 0; i < OperationsPerInvoke; i++) |
| 113 | + { |
| 114 | +#pragma warning disable CS0618 // Type or member is obsolete |
| 115 | + _ = FastHash.Hash64Fallback(val); |
| 116 | +#pragma warning restore CS0618 // Type or member is obsolete |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] |
| 121 | + public void Hash64_SingleSegment() |
| 122 | + { |
| 123 | + var val = SingleSegmentBytes; |
| 124 | + for (int i = 0; i < OperationsPerInvoke; i++) |
| 125 | + { |
| 126 | + _ = val.Hash64(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] |
| 131 | + public void Hash64_MultiSegment() |
| 132 | + { |
| 133 | + var val = _sourceMultiSegmentBytes; |
| 134 | + for (int i = 0; i < OperationsPerInvoke; i++) |
| 135 | + { |
| 136 | + _ = val.Hash64(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments