|
1 | 1 | using System;
|
2 |
| -using System.Buffers.Binary; |
3 | 2 | using System.Runtime.CompilerServices;
|
4 | 3 |
|
5 | 4 | namespace RabbitMQ.Util
|
6 | 5 | {
|
7 | 6 | internal static class NetworkOrderDeserializer
|
8 | 7 | {
|
| 8 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
9 | 9 | internal static double ReadDouble(ReadOnlyMemory<byte> memory) => ReadDouble(memory.Span);
|
10 | 10 |
|
| 11 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
11 | 12 | internal static double ReadDouble(ReadOnlySpan<byte> span)
|
12 | 13 | {
|
13 | 14 | if (span.Length < 8)
|
14 | 15 | {
|
15 | 16 | throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Double from memory.");
|
16 | 17 | }
|
17 | 18 |
|
18 |
| - long val = BinaryPrimitives.ReadInt64BigEndian(span); |
19 |
| - return Unsafe.As<long, double>(ref val); |
| 19 | + ulong val = ReadUInt64(span); |
| 20 | + return Unsafe.As<ulong, double>(ref val); |
20 | 21 | }
|
21 | 22 |
|
| 23 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
22 | 24 | internal static short ReadInt16(ReadOnlyMemory<byte> memory) => ReadInt16(memory.Span);
|
23 | 25 |
|
| 26 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
24 | 27 | internal static short ReadInt16(ReadOnlySpan<byte> span)
|
25 | 28 | {
|
26 | 29 | if (span.Length < 2)
|
27 | 30 | {
|
28 | 31 | throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Int16 from memory.");
|
29 | 32 | }
|
30 | 33 |
|
31 |
| - return BinaryPrimitives.ReadInt16BigEndian(span); |
| 34 | + return (short)ReadUInt16(span); |
32 | 35 | }
|
33 | 36 |
|
| 37 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
34 | 38 | internal static int ReadInt32(ReadOnlyMemory<byte> memory) => ReadInt32(memory.Span);
|
35 | 39 |
|
| 40 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
36 | 41 | internal static int ReadInt32(ReadOnlySpan<byte> span)
|
37 | 42 | {
|
38 | 43 | if (span.Length < 4)
|
39 | 44 | {
|
40 | 45 | throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Int32 from memory.");
|
41 | 46 | }
|
42 | 47 |
|
43 |
| - return BinaryPrimitives.ReadInt32BigEndian(span); |
| 48 | + return (int)ReadUInt32(span); |
44 | 49 | }
|
45 | 50 |
|
| 51 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
46 | 52 | internal static long ReadInt64(ReadOnlyMemory<byte> memory) => ReadInt64(memory.Span);
|
47 | 53 |
|
| 54 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
48 | 55 | internal static long ReadInt64(ReadOnlySpan<byte> span)
|
49 | 56 | {
|
50 | 57 | if (span.Length < 8)
|
51 | 58 | {
|
52 | 59 | throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Int64 from memory.");
|
53 | 60 | }
|
54 | 61 |
|
55 |
| - return BinaryPrimitives.ReadInt64BigEndian(span); |
| 62 | + return (long)ReadUInt64(span); |
56 | 63 | }
|
57 | 64 |
|
| 65 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
58 | 66 | internal static float ReadSingle(ReadOnlyMemory<byte> memory) => ReadSingle(memory.Span);
|
59 | 67 |
|
| 68 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
60 | 69 | internal static float ReadSingle(ReadOnlySpan<byte> span)
|
61 | 70 | {
|
62 | 71 | if (span.Length < 4)
|
63 | 72 | {
|
64 | 73 | throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode Single from memory.");
|
65 | 74 | }
|
66 | 75 |
|
67 |
| - int num = BinaryPrimitives.ReadInt32BigEndian(span); |
68 |
| - return Unsafe.As<int, float>(ref num); |
| 76 | + uint num = ReadUInt32(span); |
| 77 | + return Unsafe.As<uint, float>(ref num); |
69 | 78 | }
|
70 | 79 |
|
| 80 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
71 | 81 | internal static ushort ReadUInt16(ReadOnlyMemory<byte> memory) => ReadUInt16(memory.Span);
|
72 | 82 |
|
| 83 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
73 | 84 | internal static ushort ReadUInt16(ReadOnlySpan<byte> span)
|
74 | 85 | {
|
75 | 86 | if (span.Length < 2)
|
76 | 87 | {
|
77 | 88 | throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode UInt16 from memory.");
|
78 | 89 | }
|
79 | 90 |
|
80 |
| - return BinaryPrimitives.ReadUInt16BigEndian(span); |
| 91 | + return (ushort)((span[0] << 8) | span[1]); |
81 | 92 | }
|
82 | 93 |
|
| 94 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
83 | 95 | internal static uint ReadUInt32(ReadOnlyMemory<byte> memory) => ReadUInt32(memory.Span);
|
84 | 96 |
|
| 97 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
85 | 98 | internal static uint ReadUInt32(ReadOnlySpan<byte> span)
|
86 | 99 | {
|
87 | 100 | if (span.Length < 4)
|
88 | 101 | {
|
89 | 102 | throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode UInt32 from memory.");
|
90 | 103 | }
|
91 | 104 |
|
92 |
| - return BinaryPrimitives.ReadUInt32BigEndian(span); |
| 105 | + return (uint)((span[0] << 24) | (span[1] << 16) | (span[2] << 8) | span[3]); |
93 | 106 | }
|
94 | 107 |
|
| 108 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
95 | 109 | internal static ulong ReadUInt64(ReadOnlyMemory<byte> memory) => ReadUInt64(memory.Span);
|
96 | 110 |
|
| 111 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
97 | 112 | internal static ulong ReadUInt64(ReadOnlySpan<byte> span)
|
98 | 113 | {
|
99 | 114 | if (span.Length < 8)
|
100 | 115 | {
|
101 | 116 | throw new ArgumentOutOfRangeException(nameof(span), "Insufficient length to decode UInt64 from memory.");
|
102 | 117 | }
|
103 | 118 |
|
104 |
| - return BinaryPrimitives.ReadUInt64BigEndian(span); |
| 119 | + return ((ulong)span[0] << 56) | ((ulong)span[1] << 48) | ((ulong)span[2] << 40) | ((ulong)span[3] << 32) | ((ulong)span[4] << 24) | ((ulong)span[5] << 16) | ((ulong)span[6] << 8) | span[7]; |
105 | 120 | }
|
106 | 121 | }
|
107 | 122 | }
|
0 commit comments