Skip to content

Commit 2c32d05

Browse files
committed
Inline method to avoid copy-by-value
1 parent 3271ae1 commit 2c32d05

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

src/DotNext/Buffers/Memory.ReadOnlySequence.cs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -245,41 +245,30 @@ public static int CopyTo<T>(this in ReadOnlySequence<T> source, Span<T> destinat
245245
var writtenCount = 0;
246246
ReadOnlyMemory<T> block;
247247

248-
consumed = source.Start;
249-
if (source.IsSingleSegment)
248+
for (var position = consumed = source.Start;
249+
source.TryGet(ref position, out block) && block.Length <= destination.Length;
250+
consumed = position,
251+
writtenCount += block.Length)
252+
{
253+
block.Span.CopyTo(destination);
254+
destination = destination.Slice(block.Length);
255+
}
256+
257+
// copy the last segment
258+
if (block.Length > destination.Length)
250259
{
251-
block = source.First;
260+
block = block.Slice(0, destination.Length);
261+
consumed = source.GetPosition(destination.Length, consumed);
252262
}
253263
else
254264
{
255-
for (var position = consumed;
256-
source.TryGet(ref position, out block) && block.Length <= destination.Length;
257-
consumed = position,
258-
writtenCount += block.Length)
259-
{
260-
block.Span.CopyTo(destination);
261-
destination = destination.Slice(block.Length);
262-
}
265+
consumed = source.End;
263266
}
264267

265-
writtenCount += CopyLastSegment(source, block.Span, destination, ref consumed);
268+
block.Span.CopyTo(destination);
269+
writtenCount += block.Length;
270+
266271
return writtenCount;
267-
268-
static int CopyLastSegment(in ReadOnlySequence<T> source, ReadOnlySpan<T> segment, Span<T> destination, ref SequencePosition consumed)
269-
{
270-
if (segment.Length > destination.Length)
271-
{
272-
segment = segment.Slice(0, destination.Length);
273-
consumed = source.GetPosition(destination.Length, consumed);
274-
}
275-
else
276-
{
277-
consumed = source.End;
278-
}
279-
280-
segment.CopyTo(destination);
281-
return segment.Length;
282-
}
283272
}
284273

285274
/// <summary>

0 commit comments

Comments
 (0)