Skip to content

Commit 8d043a7

Browse files
committed
Optimizations to ByteBufferStream and SingleChunkBuffer.
1 parent d1bad73 commit 8d043a7

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/MongoDB.Bson/IO/ByteBufferStream.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,19 +361,19 @@ public override ArraySegment<byte> ReadCStringBytes()
361361
ThrowIfDisposed();
362362
ThrowIfEndOfStream(1);
363363

364-
var startPosition = _position;
365-
var nullPosition = FindNullByte();
366-
var length = nullPosition - startPosition;
367-
368-
var segment = _buffer.AccessBackingBytes(startPosition);
369-
if (segment.Count >= length)
364+
var segment = _buffer.AccessBackingBytes(_position);
365+
var index = Array.IndexOf<byte>(segment.Array, 0, segment.Offset, segment.Count);
366+
if (index != -1)
370367
{
371-
_position = nullPosition + 1; // advance over null byte
368+
var length = index - segment.Offset;
369+
_position += length + 1; // advance over the null byte
372370
return new ArraySegment<byte>(segment.Array, segment.Offset, length); // without the null byte
373371
}
374372
else
375373
{
376-
var cstring = ReadBytes(length + 1); // read null byte also
374+
var nullPosition = FindNullByte();
375+
var length = nullPosition - _position;
376+
var cstring = ReadBytes(length + 1); // advance over the null byte
377377
return new ArraySegment<byte>(cstring, 0, length); // without the null byte
378378
}
379379
}

src/MongoDB.Bson/IO/SingleChunkBuffer.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public sealed class SingleChunkBuffer : IByteBuffer
3535
/// <param name="chunk">The chuns.</param>
3636
/// <param name="length">The length.</param>
3737
/// <param name="isReadOnly">Whether the buffer is read only.</param>
38-
internal SingleChunkBuffer(IBsonChunk chunk, int length, bool isReadOnly = false)
38+
public SingleChunkBuffer(IBsonChunk chunk, int length, bool isReadOnly = false)
3939
{
4040
if (chunk == null)
4141
{
@@ -103,7 +103,8 @@ public ArraySegment<byte> AccessBackingBytes(int position)
103103
throw new ArgumentOutOfRangeException("position");
104104
}
105105

106-
return new ArraySegment<byte>(_chunk.Bytes.Array, _chunk.Bytes.Offset + position, _length - position);
106+
var segment = _chunk.Bytes;
107+
return new ArraySegment<byte>(segment.Array, segment.Offset + position, _length - position);
107108
}
108109

109110
/// <inheritdoc/>
@@ -120,7 +121,8 @@ public void Clear(int position, int count)
120121
}
121122
EnsureIsWritable();
122123

123-
Array.Clear(_chunk.Bytes.Array, _chunk.Bytes.Offset + position, count);
124+
var segment = _chunk.Bytes;
125+
Array.Clear(segment.Array, segment.Offset + position, count);
124126
}
125127

126128
/// <inheritdoc/>
@@ -159,7 +161,8 @@ public byte GetByte(int position)
159161
throw new ArgumentOutOfRangeException("position");
160162
}
161163

162-
return _chunk.Bytes.Array[_chunk.Bytes.Offset + position];
164+
var segment = _chunk.Bytes;
165+
return segment.Array[segment.Offset + position];
163166
}
164167

165168
/// <inheritdoc/>
@@ -183,7 +186,8 @@ public void GetBytes(int position, byte[] destination, int offset, int count)
183186
throw new ArgumentOutOfRangeException("count");
184187
}
185188

186-
Buffer.BlockCopy(_chunk.Bytes.Array, _chunk.Bytes.Offset + position, destination, offset, count);
189+
var segment = _chunk.Bytes;
190+
Buffer.BlockCopy(segment.Array, segment.Offset + position, destination, offset, count);
187191
}
188192

189193
/// <inheritdoc/>
@@ -221,7 +225,8 @@ public void SetByte(int position, byte value)
221225
}
222226
EnsureIsWritable();
223227

224-
_chunk.Bytes.Array[_chunk.Bytes.Offset + position] = value;
228+
var segment = _chunk.Bytes;
229+
segment.Array[segment.Offset + position] = value;
225230
}
226231

227232
/// <inheritdoc/>
@@ -246,7 +251,8 @@ public void SetBytes(int position, byte[] source, int offset, int count)
246251
}
247252
EnsureIsWritable();
248253

249-
Buffer.BlockCopy(source, offset, _chunk.Bytes.Array, _chunk.Bytes.Offset + position, count);
254+
var segment = _chunk.Bytes;
255+
Buffer.BlockCopy(source, offset, segment.Array, segment.Offset + position, count);
250256
}
251257

252258
// private methods

0 commit comments

Comments
 (0)