Skip to content

Commit abea17a

Browse files
committed
Remove complicated IList<ArraySegment<byte>> based API from ByteArrayUnpacker.
1 parent 9757383 commit abea17a

18 files changed

Lines changed: 2989 additions & 8721 deletions

src/MsgPack/ByteArrayUnpacker.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,12 @@ namespace MsgPack
2828
public abstract class ByteArrayUnpacker : Unpacker
2929
{
3030
/// <summary>
31-
/// Gets the bytes used by this instance.
31+
/// Gets the current offset of the source.
3232
/// </summary>
3333
/// <value>
34-
/// The bytes used by this instance. The initial state is <c>0</c>.
34+
/// The current offset of the source.
3535
/// </value>
36-
public abstract long BytesUsed { get; }
37-
38-
/// <summary>
39-
/// Gets the current index of sources.
40-
/// </summary>
41-
/// <value>
42-
/// The current index of sources.
43-
/// </value>
44-
public abstract int CurrentSourceIndex { get; }
45-
46-
/// <summary>
47-
/// Gets the current offset of the current source.
48-
/// </summary>
49-
/// <value>
50-
/// The current offset of the current source.
51-
/// </value>
52-
public abstract int CurrentSourceOffset { get; }
36+
public abstract int Offset { get; }
5337

5438
/// <summary>
5539
/// Initializes a new instance of the <see cref="ByteArrayUnpacker"/> class.

src/MsgPack/ByteArrayUnpackerReader.TypedRead.cs

Lines changed: 60 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -51,62 +51,44 @@ public override byte ReadByte()
5151

5252
public override int TryReadByte()
5353
{
54-
if ( this._currentSourceRemains >= sizeof( byte ) )
55-
{
56-
// fast path
57-
var result = BigEndianBinary.ToByte( this._currentSource, this._currentSourceOffset );
58-
this._currentSourceOffset += sizeof( byte );
59-
this._currentSourceRemains -= sizeof( byte );
60-
return result;
61-
}
62-
63-
// slow path
64-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( byte ) ) )
54+
var source = this._source;
55+
var offset = this._offset;
56+
if ( source.Length - offset < sizeof( byte ) )
6557
{
6658
return -1;
6759
}
6860

69-
return BigEndianBinary.ToByte( this._scalarBuffer, 0 );
61+
var result = BigEndianBinary.ToByte( source, offset );
62+
this._offset += sizeof( byte );
63+
return result;
7064
}
7165

7266
public override sbyte ReadSByte()
7367
{
74-
if ( this._currentSourceRemains >= sizeof( sbyte ) )
75-
{
76-
// fast path
77-
var result = BigEndianBinary.ToSByte( this._currentSource, this._currentSourceOffset );
78-
this._currentSourceOffset += sizeof( sbyte );
79-
this._currentSourceRemains -= sizeof( sbyte );
80-
return result;
81-
}
82-
83-
// slow path
84-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( sbyte ) ) )
68+
var source = this._source;
69+
var offset = this._offset;
70+
if ( source.Length - offset < sizeof( sbyte ) )
8571
{
8672
this.ThrowEofException( sizeof( sbyte ) );
8773
}
8874

89-
return BigEndianBinary.ToSByte( this._scalarBuffer, 0 );
75+
var result = BigEndianBinary.ToSByte( source, offset );
76+
this._offset += sizeof( sbyte );
77+
return result;
9078
}
9179

9280
public override short ReadInt16()
9381
{
94-
if ( this._currentSourceRemains >= sizeof( short ) )
95-
{
96-
// fast path
97-
var result = BigEndianBinary.ToInt16( this._currentSource, this._currentSourceOffset );
98-
this._currentSourceOffset += sizeof( short );
99-
this._currentSourceRemains -= sizeof( short );
100-
return result;
101-
}
102-
103-
// slow path
104-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( short ) ) )
82+
var source = this._source;
83+
var offset = this._offset;
84+
if ( source.Length - offset < sizeof( short ) )
10585
{
10686
this.ThrowEofException( sizeof( short ) );
10787
}
10888

109-
return BigEndianBinary.ToInt16( this._scalarBuffer, 0 );
89+
var result = BigEndianBinary.ToInt16( source, offset );
90+
this._offset += sizeof( short );
91+
return result;
11092
}
11193

11294
public override ushort ReadUInt16()
@@ -122,42 +104,30 @@ public override ushort ReadUInt16()
122104

123105
public override int TryReadUInt16()
124106
{
125-
if ( this._currentSourceRemains >= sizeof( ushort ) )
126-
{
127-
// fast path
128-
var result = BigEndianBinary.ToUInt16( this._currentSource, this._currentSourceOffset );
129-
this._currentSourceOffset += sizeof( ushort );
130-
this._currentSourceRemains -= sizeof( ushort );
131-
return result;
132-
}
133-
134-
// slow path
135-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( ushort ) ) )
107+
var source = this._source;
108+
var offset = this._offset;
109+
if ( source.Length - offset < sizeof( ushort ) )
136110
{
137111
return -1;
138112
}
139113

140-
return BigEndianBinary.ToUInt16( this._scalarBuffer, 0 );
114+
var result = BigEndianBinary.ToUInt16( source, offset );
115+
this._offset += sizeof( ushort );
116+
return result;
141117
}
142118

143119
public override int ReadInt32()
144120
{
145-
if ( this._currentSourceRemains >= sizeof( int ) )
146-
{
147-
// fast path
148-
var result = BigEndianBinary.ToInt32( this._currentSource, this._currentSourceOffset );
149-
this._currentSourceOffset += sizeof( int );
150-
this._currentSourceRemains -= sizeof( int );
151-
return result;
152-
}
153-
154-
// slow path
155-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( int ) ) )
121+
var source = this._source;
122+
var offset = this._offset;
123+
if ( source.Length - offset < sizeof( int ) )
156124
{
157125
this.ThrowEofException( sizeof( int ) );
158126
}
159127

160-
return BigEndianBinary.ToInt32( this._scalarBuffer, 0 );
128+
var result = BigEndianBinary.ToInt32( source, offset );
129+
this._offset += sizeof( int );
130+
return result;
161131
}
162132

163133
public override uint ReadUInt32()
@@ -173,102 +143,72 @@ public override uint ReadUInt32()
173143

174144
public override long TryReadUInt32()
175145
{
176-
if ( this._currentSourceRemains >= sizeof( uint ) )
177-
{
178-
// fast path
179-
var result = BigEndianBinary.ToUInt32( this._currentSource, this._currentSourceOffset );
180-
this._currentSourceOffset += sizeof( uint );
181-
this._currentSourceRemains -= sizeof( uint );
182-
return result;
183-
}
184-
185-
// slow path
186-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( uint ) ) )
146+
var source = this._source;
147+
var offset = this._offset;
148+
if ( source.Length - offset < sizeof( uint ) )
187149
{
188150
return -1;
189151
}
190152

191-
return BigEndianBinary.ToUInt32( this._scalarBuffer, 0 );
153+
var result = BigEndianBinary.ToUInt32( source, offset );
154+
this._offset += sizeof( uint );
155+
return result;
192156
}
193157

194158
public override long ReadInt64()
195159
{
196-
if ( this._currentSourceRemains >= sizeof( long ) )
197-
{
198-
// fast path
199-
var result = BigEndianBinary.ToInt64( this._currentSource, this._currentSourceOffset );
200-
this._currentSourceOffset += sizeof( long );
201-
this._currentSourceRemains -= sizeof( long );
202-
return result;
203-
}
204-
205-
// slow path
206-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( long ) ) )
160+
var source = this._source;
161+
var offset = this._offset;
162+
if ( source.Length - offset < sizeof( long ) )
207163
{
208164
this.ThrowEofException( sizeof( long ) );
209165
}
210166

211-
return BigEndianBinary.ToInt64( this._scalarBuffer, 0 );
167+
var result = BigEndianBinary.ToInt64( source, offset );
168+
this._offset += sizeof( long );
169+
return result;
212170
}
213171

214172
public override ulong ReadUInt64()
215173
{
216-
if ( this._currentSourceRemains >= sizeof( ulong ) )
217-
{
218-
// fast path
219-
var result = BigEndianBinary.ToUInt64( this._currentSource, this._currentSourceOffset );
220-
this._currentSourceOffset += sizeof( ulong );
221-
this._currentSourceRemains -= sizeof( ulong );
222-
return result;
223-
}
224-
225-
// slow path
226-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( ulong ) ) )
174+
var source = this._source;
175+
var offset = this._offset;
176+
if ( source.Length - offset < sizeof( ulong ) )
227177
{
228178
this.ThrowEofException( sizeof( ulong ) );
229179
}
230180

231-
return BigEndianBinary.ToUInt64( this._scalarBuffer, 0 );
181+
var result = BigEndianBinary.ToUInt64( source, offset );
182+
this._offset += sizeof( ulong );
183+
return result;
232184
}
233185

234186
public override float ReadSingle()
235187
{
236-
if ( this._currentSourceRemains >= sizeof( float ) )
237-
{
238-
// fast path
239-
var result = BigEndianBinary.ToSingle( this._currentSource, this._currentSourceOffset );
240-
this._currentSourceOffset += sizeof( float );
241-
this._currentSourceRemains -= sizeof( float );
242-
return result;
243-
}
244-
245-
// slow path
246-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( float ) ) )
188+
var source = this._source;
189+
var offset = this._offset;
190+
if ( source.Length - offset < sizeof( float ) )
247191
{
248192
this.ThrowEofException( sizeof( float ) );
249193
}
250194

251-
return BigEndianBinary.ToSingle( this._scalarBuffer, 0 );
195+
var result = BigEndianBinary.ToSingle( source, offset );
196+
this._offset += sizeof( float );
197+
return result;
252198
}
253199

254200
public override double ReadDouble()
255201
{
256-
if ( this._currentSourceRemains >= sizeof( double ) )
257-
{
258-
// fast path
259-
var result = BigEndianBinary.ToDouble( this._currentSource, this._currentSourceOffset );
260-
this._currentSourceOffset += sizeof( double );
261-
this._currentSourceRemains -= sizeof( double );
262-
return result;
263-
}
264-
265-
// slow path
266-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( double ) ) )
202+
var source = this._source;
203+
var offset = this._offset;
204+
if ( source.Length - offset < sizeof( double ) )
267205
{
268206
this.ThrowEofException( sizeof( double ) );
269207
}
270208

271-
return BigEndianBinary.ToDouble( this._scalarBuffer, 0 );
209+
var result = BigEndianBinary.ToDouble( source, offset );
210+
this._offset += sizeof( double );
211+
return result;
272212
}
273213

274214
#if FEATURE_TAP

src/MsgPack/ByteArrayUnpackerReader.TypedRead.tt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,9 @@ foreach ( var type in scalarTypes )
120120
private void WriteByteArrayReaderReadBody( string type, string primitiveType, bool isStrict )
121121
{
122122
#>
123-
if ( this._currentSourceRemains >= sizeof( <#= primitiveType #> ) )
124-
{
125-
// fast path
126-
var result = BigEndianBinary.To<#= type #>( this._currentSource, this._currentSourceOffset );
127-
this._currentSourceOffset += sizeof( <#= primitiveType #> );
128-
this._currentSourceRemains -= sizeof( <#= primitiveType #> );
129-
return result;
130-
}
131-
132-
// slow path
133-
if ( !this.TryReadSlow( this._scalarBuffer, sizeof( <#= primitiveType #> ) ) )
123+
var source = this._source;
124+
var offset = this._offset;
125+
if ( source.Length - offset < sizeof( <#= primitiveType #> ) )
134126
{
135127
<#+
136128
if ( isStrict )
@@ -148,7 +140,9 @@ private void WriteByteArrayReaderReadBody( string type, string primitiveType, bo
148140
#>
149141
}
150142

151-
return BigEndianBinary.To<#= type #>( this._scalarBuffer, 0 );
143+
var result = BigEndianBinary.To<#= type #>( source, offset );
144+
this._offset += sizeof( <#= primitiveType #> );
145+
return result;
152146
<#+
153147
} // WriteByteArrayReaderReadBody
154148
#>

0 commit comments

Comments
 (0)