Skip to content

Commit 318a461

Browse files
committed
Change limit to remains to reduce calculation on scalar cases.
1 parent 2a707c9 commit 318a461

6 files changed

Lines changed: 175 additions & 134 deletions

src/MsgPack/ByteArrayPackerWriter.TypedWrite.cs

Lines changed: 86 additions & 66 deletions
Large diffs are not rendered by default.

src/MsgPack/ByteArrayPackerWriter.TypedWrite.tt

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,18 @@ namespace MsgPack
8686
#>
8787
var currentBuffer = this._currentBuffer;
8888
var currentBufferOffset = this._currentBufferOffset;
89-
var currentBufferLimit = this._currentBufferLimit;
89+
var currentBufferRemains = this._currentBufferRemains;
9090
var currentBufferIndex = this._currentBufferIndex;
91-
if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ) + 1, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit, ref currentBufferIndex ) )
91+
if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ) + 1, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains, ref currentBufferIndex ) )
9292
{
9393
this.ThrowEofException( sizeof( <#= type #> ) );
9494
}
9595

9696
currentBuffer[ currentBufferOffset ] = header;
9797
currentBufferOffset += 1;
98+
currentBufferRemains -= 1;
9899

99-
if ( currentBufferLimit - currentBufferOffset >= sizeof( <#= type #> ) )
100+
if ( currentBufferRemains >= sizeof( <#= type #> ) )
100101
{
101102
// Fast path
102103
<#
@@ -109,16 +110,17 @@ namespace MsgPack
109110
}
110111
#>
111112
currentBufferOffset += sizeof( <#= type #> );
113+
currentBufferRemains -= sizeof( <#= type #> );
112114
}
113115
else
114116
{
115-
this.WriteBytesSlow( <#= bits #>, ref currentBufferIndex, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit );
117+
this.WriteBytesSlow( <#= bits #>, ref currentBufferIndex, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains );
116118
}
117119

118120
this._currentBufferIndex = currentBufferIndex;
119121
this._currentBuffer = currentBuffer;
120122
this._currentBufferOffset = currentBufferOffset;
121-
this._currentBufferLimit = currentBufferLimit;
123+
this._currentBufferRemains = currentBufferRemains;
122124
<#
123125
} // if isAsync
124126
#>
@@ -128,14 +130,14 @@ namespace MsgPack
128130
if ( !isAsync )
129131
{
130132
#>
131-
private void WriteBytesSlow( <#= this.ToBitsType( type ) #> value, ref int currentBufferIndex, ref byte[] currentBuffer, ref int currentBufferOffset, ref int currentBufferLimit )
133+
private void WriteBytesSlow( <#= this.ToBitsType( type ) #> value, ref int currentBufferIndex, ref byte[] currentBuffer, ref int currentBufferOffset, ref int currentBufferRemains )
132134
{
133-
if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ), 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit, ref currentBufferIndex ) )
135+
if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ), 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains, ref currentBufferIndex ) )
134136
{
135137
this.ThrowEofException( sizeof( <#= type #> ) );
136138
}
137139

138-
var bufferRemaining = currentBufferLimit - currentBufferOffset;
140+
var bufferRemaining = currentBufferRemains;
139141

140142
for ( var totalWritten = 0; totalWritten < sizeof( <#= type #> ); )
141143
{
@@ -146,13 +148,14 @@ namespace MsgPack
146148
}
147149

148150
currentBufferOffset += currentWritten;
151+
currentBufferRemains -= currentWritten;
149152

150-
if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ) - totalWritten, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit, ref currentBufferIndex ) )
153+
if ( !this.ShiftBufferIfNeeded( sizeof( <#= type #> ) - totalWritten, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains, ref currentBufferIndex ) )
151154
{
152155
this.ThrowEofException( sizeof( <#= type #> ) );
153156
}
154157

155-
bufferRemaining = currentBufferLimit - currentBufferOffset;
158+
bufferRemaining = currentBufferRemains;
156159
}
157160
}
158161

@@ -180,11 +183,12 @@ namespace MsgPack
180183
return;
181184
}
182185

183-
if ( encodedLength <= this._currentBufferLimit - this._currentBufferOffset )
186+
if ( encodedLength <= this._currentBufferRemains )
184187
{
185188
// Fast path
186189
Encoding.UTF8.GetBytes( value, 0, value.Length, this._currentBuffer, this._currentBufferOffset );
187190
this._currentBufferOffset += encodedLength;
191+
this._currentBufferRemains -= encodedLength;
188192
}
189193
else
190194
{
@@ -254,7 +258,7 @@ namespace MsgPack
254258
{
255259
var currentBuffer = this._currentBuffer;
256260
var currentBufferOffset = this._currentBufferOffset;
257-
var currentBufferLimit = this._currentBufferLimit;
261+
var currentBufferRemains = this._currentBufferRemains;
258262
var currentBufferIndex = this._currentBufferIndex;
259263
var encoder = Encoding.UTF8.GetEncoder();
260264
#if FEATURE_POINTER_CONVERSION
@@ -265,7 +269,7 @@ namespace MsgPack
265269
var isCompleted = false;
266270
do
267271
{
268-
if ( !this.ShiftBufferIfNeeded( remainingBytesLength, MaximumUtf8Length, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit, ref currentBufferIndex ) )
272+
if ( !this.ShiftBufferIfNeeded( remainingBytesLength, MaximumUtf8Length, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains, ref currentBufferIndex ) )
269273
{
270274
this.ThrowEofExceptionForString( remainingBytesLength );
271275
}
@@ -276,9 +280,9 @@ namespace MsgPack
276280
#endif // FEATURE_POINTER_CONVERSION
277281
{
278282
#if FEATURE_POINTER_CONVERSION
279-
isCompleted = encoder.EncodeString( pChars, remainingCharsLength, pBuffer + currentBufferOffset, currentBufferLimit - currentBufferOffset, out charsUsed, out bytesUsed );
283+
isCompleted = encoder.EncodeString( pChars, remainingCharsLength, pBuffer + currentBufferOffset, currentBufferRemains, out charsUsed, out bytesUsed );
280284
#else
281-
isCompleted = encoder.EncodeString( value, charsOffset, remainingCharsLength, currentBuffer, currentBufferOffset, currentBufferLimit - currentBufferOffset, out charsUsed, out bytesUsed );
285+
isCompleted = encoder.EncodeString( value, charsOffset, remainingCharsLength, currentBuffer, currentBufferOffset, currentBufferRemains, out charsUsed, out bytesUsed );
282286
#endif // FEATURE_POINTER_CONVERSION
283287
}
284288

@@ -290,6 +294,7 @@ namespace MsgPack
290294
remainingCharsLength -= charsUsed;
291295
remainingBytesLength -= bytesUsed;
292296
currentBufferOffset += bytesUsed;
297+
currentBufferRemains -= bytesUsed;
293298
} while ( remainingCharsLength > 0 );
294299
#if DEBUG
295300
Contract.Assert( isCompleted, "Encoding is not completed!" );
@@ -298,7 +303,7 @@ namespace MsgPack
298303
this._currentBufferIndex = currentBufferIndex;
299304
this._currentBuffer = currentBuffer;
300305
this._currentBufferOffset = currentBufferOffset;
301-
this._currentBufferLimit = currentBufferLimit;
306+
this._currentBufferRemains = currentBufferRemains;
302307
}
303308
}
304309
}

src/MsgPack/ByteArrayPackerWriter.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal sealed partial class ByteArrayPackerWriter : PackerWriter
5353
// TODO: Use Span<byte>
5454
private byte[] _currentBuffer;
5555
private int _currentBufferOffset;
56-
private int _currentBufferLimit;
56+
private int _currentBufferRemains;
5757

5858
#if DEBUG
5959

@@ -106,7 +106,7 @@ public ByteArrayPackerWriter( ArraySegment<byte> buffer, ByteBufferAllocator all
106106
this._currentBufferIndex = 0;
107107
this._currentBuffer = buffer.Array;
108108
this._currentBufferOffset = buffer.Offset;
109-
this._currentBufferLimit = buffer.Count + buffer.Offset;
109+
this._currentBufferRemains = buffer.Count;
110110
this._allocator = allocator;
111111
}
112112

@@ -160,7 +160,7 @@ public ByteArrayPackerWriter( IList<ArraySegment<byte>> buffers, int startIndex,
160160

161161
this._currentBuffer = startBuffer.Array;
162162
this._currentBufferOffset = startBuffer.Offset + skip;
163-
this._currentBufferLimit = startBuffer.Count + startBuffer.Offset;
163+
this._currentBufferRemains = startBuffer.Count - skip;
164164
this._allocator = allocator;
165165
}
166166

@@ -169,10 +169,10 @@ public IList<ArraySegment<byte>> GetBufferAsByteArray()
169169
return this._buffers;
170170
}
171171

172-
private bool ShiftBufferIfNeeded( int sizeHint, int minimumSize, ref byte[] currentBuffer, ref int currentBufferOffset, ref int currentBufferLimit, ref int currentBufferIndex )
172+
private bool ShiftBufferIfNeeded( int sizeHint, int minimumSize, ref byte[] currentBuffer, ref int currentBufferOffset, ref int currentBufferRemains, ref int currentBufferIndex )
173173
{
174174
// Check current buffer is empty and whether more buffer is required.
175-
if ( currentBufferLimit - currentBufferOffset < minimumSize && sizeHint > 0 )
175+
if ( currentBufferRemains < minimumSize && sizeHint > 0 )
176176
{
177177
// try shift to next buffer
178178
currentBufferIndex++;
@@ -189,9 +189,10 @@ private bool ShiftBufferIfNeeded( int sizeHint, int minimumSize, ref byte[] curr
189189
newBuffer = this._buffers[ currentBufferIndex ];
190190
}
191191

192+
#warning TODO: IList<ArraySegment<byte>> の使い方を再考する必要あり。今だと抜かせない。
192193
currentBuffer = newBuffer.Array;
193194
currentBufferOffset = newBuffer.Offset;
194-
currentBufferLimit = newBuffer.Offset + newBuffer.Count;
195+
currentBufferRemains = newBuffer.Count;
195196
}
196197

197198
return true;
@@ -201,9 +202,9 @@ public override void WriteByte( byte value )
201202
{
202203
var currentBuffer = this._currentBuffer;
203204
var currentBufferOffset = this._currentBufferOffset;
204-
var currentBufferLimit = this._currentBufferLimit;
205+
var currentBufferRemains = this._currentBufferRemains;
205206
var currentBufferIndex = this._currentBufferIndex;
206-
if ( !this.ShiftBufferIfNeeded( sizeof( byte ), sizeof( byte ), ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit, ref currentBufferIndex ) )
207+
if ( !this.ShiftBufferIfNeeded( sizeof( byte ), sizeof( byte ), ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains, ref currentBufferIndex ) )
207208
{
208209
this.ThrowEofException( 1 );
209210
}
@@ -212,30 +213,32 @@ public override void WriteByte( byte value )
212213
this._currentBufferIndex = currentBufferIndex;
213214
this._currentBuffer = currentBuffer;
214215
this._currentBufferOffset = currentBufferOffset + 1;
215-
this._currentBufferLimit = currentBufferLimit;
216+
this._currentBufferRemains = currentBufferRemains - 1;
216217
}
217218

218219
private void WriteBytes( byte[] value, int startIndex, int count )
219220
{
220221
var currentBuffer = this._currentBuffer;
221222
var currentBufferOffset = this._currentBufferOffset;
222-
var currentBufferLimit = this._currentBufferLimit;
223+
var currentBufferRemains = this._currentBufferRemains;
223224
var currentBufferIndex = this._currentBufferIndex;
224-
if ( !this.ShiftBufferIfNeeded( count, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit, ref currentBufferIndex ) )
225+
if ( !this.ShiftBufferIfNeeded( count, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains, ref currentBufferIndex ) )
225226
{
226227
this.ThrowEofException( count );
227228
}
228229

229230
var written = 0;
230231
do
231232
{
232-
var writes = Math.Min( currentBufferLimit - currentBufferOffset, ( count - written ) );
233+
var remains = count - written;
234+
var writes = Math.Min( currentBufferRemains, remains );
233235
Buffer.BlockCopy( value, startIndex + written, currentBuffer, currentBufferOffset, writes );
234236

235237
written += writes;
236238
currentBufferOffset += writes;
239+
currentBufferRemains -= writes;
237240

238-
if ( !this.ShiftBufferIfNeeded( count - written, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferLimit, ref currentBufferIndex ) )
241+
if ( !this.ShiftBufferIfNeeded( remains, 1, ref currentBuffer, ref currentBufferOffset, ref currentBufferRemains, ref currentBufferIndex ) )
239242
{
240243
this.ThrowEofException( count );
241244
}
@@ -244,7 +247,7 @@ private void WriteBytes( byte[] value, int startIndex, int count )
244247
this._currentBufferIndex = currentBufferIndex;
245248
this._currentBuffer = currentBuffer;
246249
this._currentBufferOffset = currentBufferOffset;
247-
this._currentBufferLimit = currentBufferLimit;
250+
this._currentBufferRemains = currentBufferRemains;
248251
}
249252

250253
public override void WriteBytes( byte[] value )

src/MsgPack/ByteArrayUnpackerReader.TypedRead.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ public override byte ReadByte()
5151

5252
public override int TryReadByte()
5353
{
54-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( byte ) )
54+
if ( this._currentSourceRemains >= sizeof( byte ) )
5555
{
5656
// fast path
5757
var result = BigEndianBinary.ToByte( this._currentSource, this._currentSourceOffset );
5858
this._currentSourceOffset += sizeof( byte );
59+
this._currentSourceRemains -= sizeof( byte );
5960
return result;
6061
}
6162

@@ -70,11 +71,12 @@ public override int TryReadByte()
7071

7172
public override sbyte ReadSByte()
7273
{
73-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( sbyte ) )
74+
if ( this._currentSourceRemains >= sizeof( sbyte ) )
7475
{
7576
// fast path
7677
var result = BigEndianBinary.ToSByte( this._currentSource, this._currentSourceOffset );
7778
this._currentSourceOffset += sizeof( sbyte );
79+
this._currentSourceRemains -= sizeof( sbyte );
7880
return result;
7981
}
8082

@@ -89,11 +91,12 @@ public override sbyte ReadSByte()
8991

9092
public override short ReadInt16()
9193
{
92-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( short ) )
94+
if ( this._currentSourceRemains >= sizeof( short ) )
9395
{
9496
// fast path
9597
var result = BigEndianBinary.ToInt16( this._currentSource, this._currentSourceOffset );
9698
this._currentSourceOffset += sizeof( short );
99+
this._currentSourceRemains -= sizeof( short );
97100
return result;
98101
}
99102

@@ -119,11 +122,12 @@ public override ushort ReadUInt16()
119122

120123
public override int TryReadUInt16()
121124
{
122-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( ushort ) )
125+
if ( this._currentSourceRemains >= sizeof( ushort ) )
123126
{
124127
// fast path
125128
var result = BigEndianBinary.ToUInt16( this._currentSource, this._currentSourceOffset );
126129
this._currentSourceOffset += sizeof( ushort );
130+
this._currentSourceRemains -= sizeof( ushort );
127131
return result;
128132
}
129133

@@ -138,11 +142,12 @@ public override int TryReadUInt16()
138142

139143
public override int ReadInt32()
140144
{
141-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( int ) )
145+
if ( this._currentSourceRemains >= sizeof( int ) )
142146
{
143147
// fast path
144148
var result = BigEndianBinary.ToInt32( this._currentSource, this._currentSourceOffset );
145149
this._currentSourceOffset += sizeof( int );
150+
this._currentSourceRemains -= sizeof( int );
146151
return result;
147152
}
148153

@@ -168,11 +173,12 @@ public override uint ReadUInt32()
168173

169174
public override long TryReadUInt32()
170175
{
171-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( uint ) )
176+
if ( this._currentSourceRemains >= sizeof( uint ) )
172177
{
173178
// fast path
174179
var result = BigEndianBinary.ToUInt32( this._currentSource, this._currentSourceOffset );
175180
this._currentSourceOffset += sizeof( uint );
181+
this._currentSourceRemains -= sizeof( uint );
176182
return result;
177183
}
178184

@@ -187,11 +193,12 @@ public override long TryReadUInt32()
187193

188194
public override long ReadInt64()
189195
{
190-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( long ) )
196+
if ( this._currentSourceRemains >= sizeof( long ) )
191197
{
192198
// fast path
193199
var result = BigEndianBinary.ToInt64( this._currentSource, this._currentSourceOffset );
194200
this._currentSourceOffset += sizeof( long );
201+
this._currentSourceRemains -= sizeof( long );
195202
return result;
196203
}
197204

@@ -206,11 +213,12 @@ public override long ReadInt64()
206213

207214
public override ulong ReadUInt64()
208215
{
209-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( ulong ) )
216+
if ( this._currentSourceRemains >= sizeof( ulong ) )
210217
{
211218
// fast path
212219
var result = BigEndianBinary.ToUInt64( this._currentSource, this._currentSourceOffset );
213220
this._currentSourceOffset += sizeof( ulong );
221+
this._currentSourceRemains -= sizeof( ulong );
214222
return result;
215223
}
216224

@@ -225,11 +233,12 @@ public override ulong ReadUInt64()
225233

226234
public override float ReadSingle()
227235
{
228-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( float ) )
236+
if ( this._currentSourceRemains >= sizeof( float ) )
229237
{
230238
// fast path
231239
var result = BigEndianBinary.ToSingle( this._currentSource, this._currentSourceOffset );
232240
this._currentSourceOffset += sizeof( float );
241+
this._currentSourceRemains -= sizeof( float );
233242
return result;
234243
}
235244

@@ -244,11 +253,12 @@ public override float ReadSingle()
244253

245254
public override double ReadDouble()
246255
{
247-
if ( this._currentSourceLimit - this._currentSourceOffset >= sizeof( double ) )
256+
if ( this._currentSourceRemains >= sizeof( double ) )
248257
{
249258
// fast path
250259
var result = BigEndianBinary.ToDouble( this._currentSource, this._currentSourceOffset );
251260
this._currentSourceOffset += sizeof( double );
261+
this._currentSourceRemains -= sizeof( double );
252262
return result;
253263
}
254264

0 commit comments

Comments
 (0)