|
1 | 1 | using System; |
2 | 2 | using System.Buffers.Binary; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using System.Runtime.InteropServices; |
3 | 5 |
|
4 | 6 | namespace RabbitMQ.Util |
5 | 7 | { |
6 | 8 | internal static class NetworkOrderSerializer |
7 | 9 | { |
| 10 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
8 | 11 | internal static void WriteDouble(Memory<byte> memory, double val) |
9 | 12 | { |
10 | 13 | if (memory.Length < 8) |
11 | 14 | { |
12 | 15 | throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to write Double to memory."); |
13 | 16 | } |
14 | 17 |
|
15 | | - BinaryPrimitives.WriteInt64BigEndian(memory.Span, BitConverter.DoubleToInt64Bits(val)); |
| 18 | + long tempVal = BitConverter.DoubleToInt64Bits(val); |
| 19 | + SerializeInt64(memory.Span, tempVal); |
16 | 20 | } |
17 | 21 |
|
| 22 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
18 | 23 | internal static void WriteInt16(Memory<byte> memory, short val) |
19 | 24 | { |
20 | 25 | if (memory.Length < 2) |
21 | 26 | { |
22 | 27 | throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to write Int16 to memory."); |
23 | 28 | } |
24 | 29 |
|
25 | | - BinaryPrimitives.WriteInt16BigEndian(memory.Span, val); |
| 30 | + SerializeInt16(memory.Span, val); |
26 | 31 | } |
27 | 32 |
|
| 33 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
28 | 34 | internal static void WriteInt32(Memory<byte> memory, int val) |
29 | 35 | { |
30 | 36 | if (memory.Length < 4) |
31 | 37 | { |
32 | 38 | throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to write Int32 to memory."); |
33 | 39 | } |
34 | 40 |
|
35 | | - BinaryPrimitives.WriteInt32BigEndian(memory.Span, val); |
| 41 | + SerializeInt32(memory.Span, val); |
36 | 42 | } |
37 | 43 |
|
| 44 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
38 | 45 | internal static void WriteInt64(Memory<byte> memory, long val) |
39 | 46 | { |
40 | 47 | if (memory.Length < 8) |
41 | 48 | { |
42 | 49 | throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to write Int64 to memory."); |
43 | 50 | } |
44 | 51 |
|
45 | | - BinaryPrimitives.WriteInt64BigEndian(memory.Span, val); |
46 | | - } |
| 52 | + SerializeInt64(memory.Span, val); |
| 53 | + } |
47 | 54 |
|
| 55 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
48 | 56 | internal static void WriteSingle(Memory<byte> memory, float val) |
49 | 57 | { |
50 | 58 | if (memory.Length < 4) |
51 | 59 | { |
52 | 60 | throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to write Single to memory."); |
53 | 61 | } |
54 | 62 |
|
55 | | - byte[] bytes = BitConverter.GetBytes(val); |
56 | | - if (BitConverter.IsLittleEndian) |
57 | | - { |
58 | | - Array.Reverse(bytes); |
59 | | - } |
60 | | - |
61 | | - bytes.AsMemory().CopyTo(memory); |
| 63 | + int tempVal = Unsafe.As<float, int>(ref val); |
| 64 | + SerializeInt32(memory.Span, tempVal); |
62 | 65 | } |
63 | 66 |
|
| 67 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
64 | 68 | internal static void WriteUInt16(Memory<byte> memory, ushort val) |
65 | 69 | { |
66 | 70 | if (memory.Length < 2) |
67 | 71 | { |
68 | 72 | throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to write UInt16 to memory."); |
69 | 73 | } |
70 | 74 |
|
71 | | - BinaryPrimitives.WriteUInt16BigEndian(memory.Span, val); |
| 75 | + SerializeUInt16(memory.Span, val); |
72 | 76 | } |
73 | 77 |
|
| 78 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
74 | 79 | internal static void WriteUInt32(Memory<byte> memory, uint val) |
75 | 80 | { |
76 | 81 | if (memory.Length < 4) |
77 | 82 | { |
78 | 83 | throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to write UInt32 to memory."); |
79 | 84 | } |
80 | 85 |
|
81 | | - BinaryPrimitives.WriteUInt32BigEndian(memory.Span, val); |
| 86 | + SerializeUInt32(memory.Span, val); |
82 | 87 | } |
83 | 88 |
|
| 89 | + |
| 90 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
84 | 91 | internal static void WriteUInt64(Memory<byte> memory, ulong val) |
85 | 92 | { |
86 | 93 | if (memory.Length < 8) |
87 | 94 | { |
88 | 95 | throw new ArgumentOutOfRangeException(nameof(memory), "Insufficient length to write UInt64 from memory."); |
89 | 96 | } |
90 | 97 |
|
91 | | - BinaryPrimitives.WriteUInt64BigEndian(memory.Span, val); |
| 98 | + SerializeUInt64(memory.Span, val); |
| 99 | + } |
| 100 | + |
| 101 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 102 | + private static void SerializeInt16(Span<byte> memory, short val) |
| 103 | + { |
| 104 | + SerializeUInt16(memory, (ushort)val); |
| 105 | + } |
| 106 | + |
| 107 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 108 | + private static void SerializeUInt16(Span<byte> memory, ushort val) |
| 109 | + { |
| 110 | + memory[0] = (byte)((val >> 8) & 0xFF); |
| 111 | + memory[1] = (byte)(val & 0xFF); |
| 112 | + } |
| 113 | + |
| 114 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 115 | + private static void SerializeInt32(Span<byte> memory, int val) |
| 116 | + { |
| 117 | + SerializeUInt32(memory, (uint)val); |
| 118 | + } |
| 119 | + |
| 120 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 121 | + private static void SerializeUInt32(Span<byte> memory, uint val) |
| 122 | + { |
| 123 | + memory[0] = (byte)((val >> 24) & 0xFF); |
| 124 | + memory[1] = (byte)((val >> 16) & 0xFF); |
| 125 | + memory[2] = (byte)((val >> 8) & 0xFF); |
| 126 | + memory[3] = (byte)(val & 0xFF); |
| 127 | + } |
| 128 | + |
| 129 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 130 | + private static void SerializeInt64(Span<byte> memory, long val) |
| 131 | + { |
| 132 | + SerializeUInt64(memory, (ulong)val); |
| 133 | + } |
| 134 | + |
| 135 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 136 | + private static void SerializeUInt64(Span<byte> memory, ulong val) |
| 137 | + { |
| 138 | + memory[0] = (byte)((val >> 56) & 0xFF); |
| 139 | + memory[1] = (byte)((val >> 48) & 0xFF); |
| 140 | + memory[2] = (byte)((val >> 40) & 0xFF); |
| 141 | + memory[3] = (byte)((val >> 32) & 0xFF); |
| 142 | + memory[4] = (byte)((val >> 24) & 0xFF); |
| 143 | + memory[5] = (byte)((val >> 16) & 0xFF); |
| 144 | + memory[6] = (byte)((val >> 8) & 0xFF); |
| 145 | + memory[7] = (byte)(val & 0xFF); |
92 | 146 | } |
93 | 147 | } |
94 | 148 | } |
0 commit comments