@@ -74,20 +74,20 @@ private static T[] ReadBsonArray<T>(
74
74
using var buffer = ThreadStaticBuffer . RentBuffer ( array . Length ) ;
75
75
76
76
var bytes = buffer . Bytes ;
77
- array . GetBytes ( 0 , bytes , 0 , array . Length ) ;
77
+ array . GetBytes ( 0 , bytes , 0 , array . Length ) ;
78
+ var span = bytes . AsSpan ( ) ;
78
79
79
80
var result = new List < T > ( ) ;
80
-
81
-
81
+
82
82
var index = 4 ; // 4 first bytes are array object size in bytes
83
83
var maxIndex = array . Length - 1 ;
84
84
85
85
while ( index < maxIndex )
86
86
{
87
- ValidateBsonType ( bsonDataType ) ;
87
+ ValidateBsonType ( bsonDataType , span ) ;
88
88
89
89
// Skip name
90
- while ( bytes [ index ] != 0 ) { index ++ ; }
90
+ while ( span [ index ] != 0 ) { index ++ ; }
91
91
index ++ ; // Skip string terminating 0
92
92
93
93
T value = default ;
@@ -97,81 +97,81 @@ private static T[] ReadBsonArray<T>(
97
97
{
98
98
case ConversionType . DoubleToSingle :
99
99
{
100
- var v = ( float ) BitConverter . Int64BitsToDouble ( BinaryPrimitives . ReadInt64LittleEndian ( bytes . AsSpan ( index ) ) ) ;
100
+ var v = ( float ) BinaryPrimitivesCompat . ReadDoubleLittleEndian ( span . Slice ( index ) ) ;
101
101
value = Unsafe . As < float , T > ( ref v ) ;
102
102
break ;
103
103
}
104
104
case ConversionType . DoubleToDouble :
105
105
{
106
- var v = BitConverter . Int64BitsToDouble ( BinaryPrimitives . ReadInt64LittleEndian ( bytes . AsSpan ( index ) ) ) ;
106
+ var v = BitConverter . Int64BitsToDouble ( BinaryPrimitives . ReadInt64LittleEndian ( span . Slice ( index ) ) ) ;
107
107
value = Unsafe . As < double , T > ( ref v ) ;
108
108
break ;
109
109
}
110
110
case ConversionType . Decimal128ToDecimal128 :
111
111
{
112
- var lowBits = BinaryPrimitives . ReadUInt64LittleEndian ( bytes . AsSpan ( index ) ) ;
113
- var highBits = BinaryPrimitives . ReadUInt64LittleEndian ( bytes . AsSpan ( index + 8 ) ) ;
112
+ var lowBits = BinaryPrimitives . ReadUInt64LittleEndian ( span . Slice ( index ) ) ;
113
+ var highBits = BinaryPrimitives . ReadUInt64LittleEndian ( span . Slice ( index + 8 ) ) ;
114
114
var v = Decimal128 . ToDecimal ( Decimal128 . FromIEEEBits ( highBits , lowBits ) ) ;
115
115
value = Unsafe . As < decimal , T > ( ref v ) ;
116
116
break ;
117
117
}
118
118
case ConversionType . BoolToBool :
119
119
{
120
- var v = bytes [ index ] != 0 ;
120
+ var v = span [ index ] != 0 ;
121
121
value = Unsafe . As < bool , T > ( ref v ) ;
122
122
break ;
123
123
}
124
124
case ConversionType . Int32ToInt8 :
125
125
{
126
- var v = ( sbyte ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
126
+ var v = ( sbyte ) BinaryPrimitives . ReadInt32LittleEndian ( span . Slice ( index ) ) ;
127
127
value = Unsafe . As < sbyte , T > ( ref v ) ;
128
128
break ;
129
129
}
130
130
case ConversionType . Int32ToUInt8 :
131
131
{
132
- var v = ( byte ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
132
+ var v = ( byte ) BinaryPrimitives . ReadInt32LittleEndian ( span . Slice ( index ) ) ;
133
133
value = Unsafe . As < byte , T > ( ref v ) ;
134
134
break ;
135
135
}
136
136
case ConversionType . Int32ToInt16 :
137
137
{
138
- var v = ( short ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
138
+ var v = ( short ) BinaryPrimitives . ReadInt32LittleEndian ( span . Slice ( index ) ) ;
139
139
value = Unsafe . As < short , T > ( ref v ) ;
140
140
break ;
141
141
}
142
142
case ConversionType . Int32ToUInt16 :
143
143
{
144
- var v = ( ushort ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
144
+ var v = ( ushort ) BinaryPrimitives . ReadInt32LittleEndian ( span . Slice ( index ) ) ;
145
145
value = Unsafe . As < ushort , T > ( ref v ) ;
146
146
break ;
147
147
}
148
148
case ConversionType . Int32ToChar :
149
149
{
150
- var v = ( char ) ( ushort ) BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
150
+ var v = ( char ) ( ushort ) BinaryPrimitives . ReadInt32LittleEndian ( span . Slice ( index ) ) ;
151
151
value = Unsafe . As < char , T > ( ref v ) ;
152
152
break ;
153
153
}
154
154
case ConversionType . Int32ToInt32 :
155
155
{
156
- var v = BinaryPrimitives . ReadInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
156
+ var v = BinaryPrimitives . ReadInt32LittleEndian ( span . Slice ( index ) ) ;
157
157
value = Unsafe . As < int , T > ( ref v ) ;
158
158
break ;
159
159
}
160
160
case ConversionType . Int32ToUInt32 :
161
161
{
162
- var v = BinaryPrimitives . ReadUInt32LittleEndian ( bytes . AsSpan ( index ) ) ;
162
+ var v = BinaryPrimitives . ReadUInt32LittleEndian ( span . Slice ( index ) ) ;
163
163
value = Unsafe . As < uint , T > ( ref v ) ;
164
164
break ;
165
165
}
166
166
case ConversionType . Int64ToInt64 :
167
167
{
168
- var v = BinaryPrimitives . ReadInt64LittleEndian ( bytes . AsSpan ( index ) ) ;
168
+ var v = BinaryPrimitives . ReadInt64LittleEndian ( span . Slice ( index ) ) ;
169
169
value = Unsafe . As < long , T > ( ref v ) ;
170
170
break ;
171
171
}
172
172
case ConversionType . Int64ToUInt64 :
173
173
{
174
- var v = BinaryPrimitives . ReadUInt64LittleEndian ( bytes . AsSpan ( index ) ) ;
174
+ var v = BinaryPrimitives . ReadUInt64LittleEndian ( span . Slice ( index ) ) ;
175
175
value = Unsafe . As < ulong , T > ( ref v ) ;
176
176
break ;
177
177
}
@@ -183,12 +183,12 @@ private static T[] ReadBsonArray<T>(
183
183
index += bsonDataSize ;
184
184
}
185
185
186
- ValidateBsonType ( BsonType . EndOfDocument ) ;
186
+ ValidateBsonType ( BsonType . EndOfDocument , span ) ;
187
187
return result . ToArray ( ) ;
188
188
189
- void ValidateBsonType ( BsonType bsonType )
189
+ void ValidateBsonType ( BsonType bsonType , Span < byte > span )
190
190
{
191
- if ( ( BsonType ) bytes [ index ] != bsonType )
191
+ if ( ( BsonType ) span [ index ] != bsonType )
192
192
{
193
193
throw new InvalidOperationException ( ) ;
194
194
}
0 commit comments