|
1 | 1 | using System;
|
2 | 2 | using System.Buffers;
|
3 |
| -using System.Buffers.Binary; |
4 |
| -using System.Runtime.CompilerServices; |
5 | 3 | using System.Runtime.InteropServices;
|
6 | 4 | using BenchmarkDotNet.Attributes;
|
7 | 5 | using ProGaudi.MsgPack;
|
8 | 6 |
|
9 | 7 | namespace msgpack.spec.linux
|
10 | 8 | {
|
11 | 9 | [MemoryDiagnoser]
|
12 |
| - [DisassemblyDiagnoser] |
| 10 | + [DisassemblyDiagnoser(false , true)] |
13 | 11 | [Q3Column]
|
14 |
| - [MarkdownExporterAttribute.GitHub] |
15 | 12 | public class NativeComparison
|
16 | 13 | {
|
17 |
| - private const ushort length = 100; |
18 |
| - private const uint baseInt = 1 << 30; |
| 14 | + private const uint length = 100; |
| 15 | + private uint baseInt = 99000; |
19 | 16 | private readonly byte[] _buffer = ArrayPool<byte>.Shared.Rent(short.MaxValue);
|
20 | 17 |
|
21 |
| - [Benchmark] |
22 |
| - public int MsgPackSpecArray() |
23 |
| - { |
24 |
| - var buffer = _buffer.AsSpan(); |
25 |
| - var wroteSize = MsgPackSpec.WriteArray16Header(buffer, length); |
26 |
| - for (var i = 0u; i < length; i++) |
27 |
| - wroteSize += MsgPackSpec.WriteUInt32(buffer.Slice(wroteSize), baseInt); |
28 |
| - |
29 |
| - return wroteSize; |
30 |
| - } |
31 |
| - [Benchmark] |
32 |
| - public int MsgPackSpecArrayMinus() |
| 18 | + [Benchmark(Baseline = true)] |
| 19 | + public int Span() |
33 | 20 | {
|
34 | 21 | var buffer = _buffer.AsSpan();
|
35 |
| - var wroteSize = MsgPackSpec.WriteArray16Header(buffer, length); |
36 |
| - for (var i = 0u; i < length; i++) |
37 |
| - wroteSize += MsgPackSpec.WriteUInt32(buffer.Slice(wroteSize), baseInt - i); |
38 |
| - |
39 |
| - return wroteSize; |
40 |
| - } |
41 |
| - |
42 |
| - [Benchmark] |
43 |
| - public unsafe void Pointer() |
44 |
| - { |
45 |
| - fixed (byte* pointer = &_buffer[0]) |
| 22 | + var i = 0; |
| 23 | + for (; i < length; i++) |
46 | 24 | {
|
47 |
| - pointer[0] = DataCodes.Array16; |
48 |
| - Unsafe.WriteUnaligned(ref pointer[1], length); |
49 |
| - for (var i = 0u; i < length; i++) |
50 |
| - { |
51 |
| - pointer[3 + 5 * i] = DataCodes.UInt32; |
52 |
| - Unsafe.WriteUnaligned(ref pointer[3 + 5 * i + 1], baseInt); |
53 |
| - } |
| 25 | + baseInt -= 1000u; |
| 26 | + MsgPackSpec.WriteUInt32(buffer.Slice(5 * i), baseInt); |
54 | 27 | }
|
| 28 | + |
| 29 | + return i; |
55 | 30 | }
|
56 | 31 |
|
57 | 32 | [Benchmark]
|
58 |
| - public unsafe void PointerMinus() |
| 33 | + public int SpanConst() |
59 | 34 | {
|
60 |
| - fixed (byte* pointer = &_buffer[0]) |
| 35 | + var buffer = _buffer.AsSpan(); |
| 36 | + var i = 0; |
| 37 | + for (; i < length; i++) |
61 | 38 | {
|
62 |
| - pointer[0] = DataCodes.Array16; |
63 |
| - Unsafe.WriteUnaligned(ref pointer[1], length); |
64 |
| - for (var i = 0u; i < length; i++) |
65 |
| - { |
66 |
| - pointer[3 + 5 * i] = DataCodes.UInt32; |
67 |
| - Unsafe.WriteUnaligned(ref pointer[3 + 5 * i + 1], baseInt - i); |
68 |
| - } |
| 39 | + baseInt -= 1000u; |
| 40 | + MsgPackSpec.WriteUInt32(buffer.Slice(5 * i), length); |
69 | 41 | }
|
70 |
| - } |
71 | 42 |
|
72 |
| - [Benchmark(Baseline = true)] |
73 |
| - public void CArray() => CNative.SerializeArray(); |
74 |
| - |
75 |
| - [Benchmark] |
76 |
| - public void CArrayMinus() => CNative.SerializeArrayMinus(); |
77 |
| - |
78 |
| - [Benchmark] |
79 |
| - public void CppArray() => CppNative.SerializeArray(); |
80 |
| - |
81 |
| - [Benchmark] |
82 |
| - public void CppArrayMinus() => CppNative.SerializeArrayMinus(); |
| 43 | + return i; |
| 44 | + } |
83 | 45 |
|
84 | 46 | [Benchmark]
|
85 |
| - public unsafe void PointerBigEndian() |
| 47 | + public unsafe int Fixed() |
86 | 48 | {
|
87 | 49 | fixed (byte* pointer = &_buffer[0])
|
88 | 50 | {
|
89 |
| - pointer[0] = DataCodes.Array16; |
90 |
| - Unsafe.WriteUnaligned(ref pointer[1], BinaryPrimitives.ReverseEndianness(length)); |
91 |
| - for (var i = 0u; i < length; i++) |
| 51 | + var i = 0; |
| 52 | + for (; i < length; i++) |
92 | 53 | {
|
93 |
| - pointer[3 + 5 * i] = DataCodes.UInt32; |
94 |
| - Unsafe.WriteUnaligned(ref pointer[3 + 5 * i + 1], BinaryPrimitives.ReverseEndianness(baseInt)); |
| 54 | + baseInt -= 1000; |
| 55 | + MsgPackSpecPointer.WriteUInt32(pointer, 5 * i, baseInt); |
95 | 56 | }
|
96 |
| - } |
97 |
| - } |
98 | 57 |
|
99 |
| - [Benchmark] |
100 |
| - public void SpanBigEndian() |
101 |
| - { |
102 |
| - var pointer = _buffer.AsSpan(); |
103 |
| - var l = BinaryPrimitives.ReverseEndianness(length); |
104 |
| - pointer[0] = DataCodes.Array16; |
105 |
| - MemoryMarshal.Write(pointer.Slice(1), ref l); |
106 |
| - var x = BinaryPrimitives.ReverseEndianness(baseInt); |
107 |
| - for (var i = 0; i < length; i++) |
108 |
| - { |
109 |
| - pointer[3 + 5 * i] = DataCodes.UInt32; |
110 |
| - MemoryMarshal.Write(pointer.Slice(3 + 5 * i + 1), ref x); |
| 58 | + return i; |
111 | 59 | }
|
112 | 60 | }
|
113 | 61 |
|
114 | 62 | [Benchmark]
|
115 |
| - public void SpanLengthBigEndian() |
116 |
| - { |
117 |
| - var pointer = _buffer.AsSpan(); |
118 |
| - var l = BinaryPrimitives.ReverseEndianness(length); |
119 |
| - pointer[0] = DataCodes.Array16; |
120 |
| - MemoryMarshal.Write(pointer.Slice(1, 2), ref l); |
121 |
| - var x = BinaryPrimitives.ReverseEndianness(baseInt); |
122 |
| - for (var i = 0; i < length; i++) |
123 |
| - { |
124 |
| - pointer[3 + 5 * i] = DataCodes.UInt32; |
125 |
| - MemoryMarshal.Write(pointer.Slice(3 + 5 * i + 1, 4), ref x); |
126 |
| - } |
127 |
| - } |
| 63 | + public uint C() => CNative.SerializeInts(length); |
128 | 64 |
|
129 | 65 | [Benchmark]
|
130 |
| - public void SpanBigEndianBinaryPrimitive() |
131 |
| - { |
132 |
| - var pointer = _buffer.AsSpan(); |
133 |
| - pointer[0] = DataCodes.Array16; |
134 |
| - BinaryPrimitives.WriteUInt16BigEndian(pointer.Slice(1), length); |
135 |
| - for (var i = 0; i < length; i++) |
136 |
| - { |
137 |
| - pointer[3 + 5 * i] = DataCodes.UInt32; |
138 |
| - BinaryPrimitives.WriteUInt32BigEndian(pointer.Slice(3 + 5 * i + 1), baseInt); |
139 |
| - } |
140 |
| - } |
141 |
| - |
142 |
| - [Benchmark] |
143 |
| - public void Empty() => CNative.Empty(); |
| 66 | + public uint Cpp() => CppNative.SerializeInts(length); |
144 | 67 |
|
145 | 68 | private static class CNative
|
146 | 69 | {
|
147 |
| - [DllImport("libcMsgPack.so", EntryPoint = "serializeIntArray", CallingConvention = CallingConvention.Cdecl)] |
148 |
| - public static extern void SerializeArray(); |
149 |
| - |
150 |
| - [DllImport("libcMsgPack.so", EntryPoint = "serializeIntArrayMinus", CallingConvention = CallingConvention.Cdecl)] |
151 |
| - public static extern void SerializeArrayMinus(); |
152 |
| - |
153 |
| - [DllImport("libcMsgPack.so", EntryPoint = "empty", CallingConvention = CallingConvention.Cdecl)] |
154 |
| - public static extern void Empty(); |
| 70 | + [DllImport("libcMsgPack.so", EntryPoint = "serializeInts", CallingConvention = CallingConvention.Cdecl)] |
| 71 | + public static extern uint SerializeInts(uint size); |
155 | 72 | }
|
156 | 73 |
|
157 | 74 | private static class CppNative
|
158 | 75 | {
|
159 |
| - [DllImport("libcppMsgPack.so", EntryPoint = "serializeIntArray", CallingConvention = CallingConvention.Cdecl)] |
160 |
| - public static extern void SerializeArray(); |
161 |
| - |
162 |
| - [DllImport("libcMsgPack.so", EntryPoint = "serializeIntArrayMinus", CallingConvention = CallingConvention.Cdecl)] |
163 |
| - public static extern void SerializeArrayMinus(); |
| 76 | + [DllImport("libcMsgPack.so", EntryPoint = "serializeInts", CallingConvention = CallingConvention.Cdecl)] |
| 77 | + public static extern uint SerializeInts(uint size); |
164 | 78 | }
|
165 | 79 | }
|
166 | 80 | }
|
0 commit comments