Skip to content

Commit

Permalink
Address PR feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
yyjdelete committed Jun 2, 2021
1 parent 12e5af4 commit 849cd3f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 124 deletions.
21 changes: 17 additions & 4 deletions src/DotNetty.Buffers/AbstractByteBuffer.NetStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public virtual int ReadBytes(Memory<byte> destination)

public virtual IByteBuffer SetBytes(int index, in ReadOnlySpan<byte> src)
{
CheckIndex(index);
CheckIndex(index, src.Length);
if (src.IsEmpty) { return this; }

var length = src.Length;
Expand All @@ -236,7 +236,7 @@ public virtual IByteBuffer SetBytes(int index, in ReadOnlySpan<byte> src)
}
public virtual IByteBuffer SetBytes(int index, in ReadOnlyMemory<byte> src)
{
CheckIndex(index);
CheckIndex(index, src.Length);
if (src.IsEmpty) { return this; }

var length = src.Length;
Expand Down Expand Up @@ -279,12 +279,21 @@ protected internal void EnsureWritable0(int writerIdx, int sizeHint)

protected sealed class ReadOnlyBufferSegment : ReadOnlySequenceSegment<byte>
{
public static ReadOnlySequence<byte> Create(IEnumerable<ReadOnlyMemory<byte>> buffers)
public static ReadOnlySequence<byte> Create(List<ReadOnlyMemory<byte>> buffers)
{
switch (buffers.Count)
{
case 0:
return ReadOnlySequence<byte>.Empty;
case 1:
return new ReadOnlySequence<byte>(buffers[0]);
}
ReadOnlyBufferSegment segment = null;
ReadOnlyBufferSegment first = null;
foreach (var buffer in buffers)
{
if (buffer.Length == 0)
continue;
var newSegment = new ReadOnlyBufferSegment()
{
Memory = buffer,
Expand All @@ -305,7 +314,11 @@ public static ReadOnlySequence<byte> Create(IEnumerable<ReadOnlyMemory<byte>> bu

if (first is null)
{
first = segment = new ReadOnlyBufferSegment();
return ReadOnlySequence<byte>.Empty;
}
if (first == segment)
{
return new ReadOnlySequence<byte>(first.Memory);
}

return new ReadOnlySequence<byte>(first, 0, segment, segment.Memory.Length);
Expand Down
80 changes: 25 additions & 55 deletions src/DotNetty.Buffers/CompositeByteBuffer.NetStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,88 +27,46 @@ namespace DotNetty.Buffers
{
using System;
using System.Buffers;
using System.Diagnostics;
using DotNetty.Common;
using DotNetty.Common.Utilities;

public partial class CompositeByteBuffer
{
protected internal override ReadOnlyMemory<byte> _GetReadableMemory(int index, int count)
{
if (0u >= (uint)count) { return ReadOnlyMemory<byte>.Empty; }
if (0u >= (uint)count || _componentCount == 0) { return ReadOnlyMemory<byte>.Empty; }

switch (_componentCount)
{
case 0:
return ReadOnlyMemory<byte>.Empty;
case 1:
ComponentEntry c = _components[0];
IByteBuffer buf = c.Buffer;
if (buf.IsSingleIoBuffer)
{
return buf.GetReadableMemory(c.Idx(index), count);
}
break;
}

var merged = new Memory<byte>(new byte[count]);
var buffers = GetSequence(index, count);
if (buffers.IsSingleSegment) { return buffers.First; }

int offset = 0;
foreach (var buf in buffers)
{
Debug.Assert(merged.Length - offset >= buf.Length);

buf.CopyTo(merged.Slice(offset));
offset += buf.Length;
}

var merged = buffers.ToArray();
return merged;
}

protected internal override ReadOnlySpan<byte> _GetReadableSpan(int index, int count)
{
if (0u >= (uint)count) { return ReadOnlySpan<byte>.Empty; }
if (0u >= (uint)count || _componentCount == 0) { return ReadOnlySpan<byte>.Empty; }

switch (_componentCount)
{
case 0:
return ReadOnlySpan<byte>.Empty;
case 1:
//ComponentEntry c = _components[0];
//return c.Buffer.GetReadableSpan(index, count);
ComponentEntry c = _components[0];
IByteBuffer buf = c.Buffer;
if (buf.IsSingleIoBuffer)
{
return buf.GetReadableSpan(c.Idx(index), count);
}
break;
}

var merged = new Memory<byte>(new byte[count]);
var buffers = GetSequence(index, count);
if (buffers.IsSingleSegment) { return buffers.First.Span; }

int offset = 0;
foreach (var buf in buffers)
{
Debug.Assert(merged.Length - offset >= buf.Length);

buf.CopyTo(merged.Slice(offset));
offset += buf.Length;
}

return merged.Span;
var merged = buffers.ToArray();
return merged;
}

protected internal override ReadOnlySequence<byte> _GetSequence(int index, int count)
{
if (0u >= (uint)count) { return ReadOnlySequence<byte>.Empty; }

int i = ToComponentIndex0(index);
if (i == ToComponentIndex0(index + count - 1))
{
ComponentEntry c = _components[i];
return c.Buffer.GetSequence(c.Idx(index), count);
}
var buffers = ThreadLocalList<ReadOnlyMemory<byte>>.NewInstance(_componentCount);
try
{
int i = ToComponentIndex0(index);
while (count > 0)
{
ComponentEntry c = _components[i];
Expand Down Expand Up @@ -167,6 +125,12 @@ protected internal override Memory<byte> _GetMemory(int index, int count)
ComponentEntry c = _components[0];
return c.Buffer.GetMemory(index, count);
default:
var idx = ToComponentIndex0(index);
if (idx == ToComponentIndex0(index + count - 1))
{
ComponentEntry c1 = _components[idx];
return c1.Buffer.GetMemory(c1.Idx(index), count);
}
throw ThrowHelper.GetNotSupportedException();
}
}
Expand All @@ -183,6 +147,12 @@ protected internal override Span<byte> _GetSpan(int index, int count)
ComponentEntry c = _components[0];
return c.Buffer.GetSpan(index, count);
default:
var idx = ToComponentIndex0(index);
if (idx == ToComponentIndex0(index + count - 1))
{
ComponentEntry c1 = _components[idx];
return c1.Buffer.GetSpan(c1.Idx(index), count);
}
throw ThrowHelper.GetNotSupportedException();
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/DotNetty.Buffers/CompositeByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
namespace DotNetty.Buffers
{
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using DotNetty.Common;
Expand Down Expand Up @@ -789,18 +791,12 @@ public override ArraySegment<byte> GetIoBuffer(int index, int length)
break;
}

var merged = new byte[length];
var memory = new Memory<byte>(merged);
var buffers = GetSequence(index, length);

int offset = 0;
foreach (var buf in buffers)
if (buffers.IsSingleSegment && MemoryMarshal.TryGetArray(buffers.First, out var segment))
{
Debug.Assert(merged.Length - offset >= buf.Length);

buf.CopyTo(memory.Slice(offset));
offset += buf.Length;
return segment;
}
var merged = buffers.ToArray();
return new ArraySegment<byte>(merged);
}

Expand Down
47 changes: 39 additions & 8 deletions src/DotNetty.Buffers/EmptyByteBuffer.NetStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,57 @@ partial class EmptyByteBuffer
public void AdvanceReader(int count) { }

public ReadOnlyMemory<byte> UnreadMemory => ReadOnlyMemory<byte>.Empty;
public ReadOnlyMemory<byte> GetReadableMemory(int index, int count) => ReadOnlyMemory<byte>.Empty;
public ReadOnlyMemory<byte> GetReadableMemory(int index, int count)
{
_ = CheckIndex(index, count);
return ReadOnlyMemory<byte>.Empty;
}

public ReadOnlySpan<byte> UnreadSpan => ReadOnlySpan<byte>.Empty;
public ReadOnlySpan<byte> GetReadableSpan(int index, int count) => ReadOnlySpan<byte>.Empty;
public ReadOnlySpan<byte> GetReadableSpan(int index, int count)
{
_ = CheckIndex(index, count);
return ReadOnlySpan<byte>.Empty;
}

public ReadOnlySequence<byte> UnreadSequence => ReadOnlySequence<byte>.Empty;
public ReadOnlySequence<byte> GetSequence(int index, int count) => ReadOnlySequence<byte>.Empty;
public ReadOnlySequence<byte> GetSequence(int index, int count)
{
_ = CheckIndex(index, count);
return ReadOnlySequence<byte>.Empty;
}

public Memory<byte> FreeMemory => Memory<byte>.Empty;
public Memory<byte> GetMemory(int sizeHintt = 0) => Memory<byte>.Empty;
public Memory<byte> GetMemory(int index, int count) => Memory<byte>.Empty;
public Memory<byte> GetMemory(int index, int count)
{
_ = CheckIndex(index, count);
return Memory<byte>.Empty;
}

public void Advance(int count) { }
public void Advance(int count)
{
_ = CheckLength(count);
}

public Span<byte> FreeSpan => Span<byte>.Empty;
public Span<byte> GetSpan(int sizeHintt = 0) => Span<byte>.Empty;
public Span<byte> GetSpan(int index, int count) => Span<byte>.Empty;
public Span<byte> GetSpan(int index, int count)
{
_ = CheckIndex(index, count);
return Span<byte>.Empty;
}

public int GetBytes(int index, Span<byte> destination) => 0;
public int GetBytes(int index, Memory<byte> destination) => 0;
public int GetBytes(int index, Span<byte> destination)
{
_ = CheckIndex(index);
return 0;
}
public int GetBytes(int index, Memory<byte> destination)
{
_ = CheckIndex(index);
return 0;
}

public int ReadBytes(Span<byte> destination) => 0;
public int ReadBytes(Memory<byte> destination) => 0;
Expand Down
66 changes: 28 additions & 38 deletions src/DotNetty.Buffers/FixedCompositeByteBuf.NetStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,11 @@ protected internal override ReadOnlyMemory<byte> _GetReadableMemory(int index, i
{
if (0u >= (uint)count) { return ReadOnlyMemory<byte>.Empty; }

if (_buffers.Length == 1)
{
var buf = Buffer(0);
if (buf.IsSingleIoBuffer)
{
return buf.GetReadableMemory(index, count);
}
}

var merged = new Memory<byte>(new byte[count]);
var bufs = GetSequence(index, count);
if (bufs.IsSingleSegment) { return bufs.First; }

int offset = 0;
foreach (var buf in bufs)
{
Debug.Assert(merged.Length - offset >= buf.Length);

buf.CopyTo(merged.Slice(offset));
offset += buf.Length;
}
var merged = new Memory<byte>(new byte[count]);
bufs.CopyTo(merged.Span);

return merged;
}
Expand All @@ -64,38 +49,27 @@ protected internal override ReadOnlySpan<byte> _GetReadableSpan(int index, int c
{
if (0u >= (uint)count) { return ReadOnlySpan<byte>.Empty; }

if (_buffers.Length == 1)
{
var buf = Buffer(0);
if (buf.IsSingleIoBuffer)
{
return buf.GetReadableSpan(index, count);
}
}

var merged = new Memory<byte>(new byte[count]);
var bufs = GetSequence(index, count);
if (bufs.IsSingleSegment) { return bufs.First.Span; }

int offset = 0;
foreach (var buf in bufs)
{
Debug.Assert(merged.Length - offset >= buf.Length);

buf.CopyTo(merged.Slice(offset));
offset += buf.Length;
}
var merged = new Span<byte>(new byte[count]);
bufs.CopyTo(merged);

return merged.Span;
return merged;
}

protected internal override ReadOnlySequence<byte> _GetSequence(int index, int count)
{
if (0u >= (uint)count) { return ReadOnlySequence<byte>.Empty; }

var c = FindComponent(index);
if (c == FindComponent(index + count - 1))
{
return c.GetSequence(index - c.Offset, count);
}
var array = ThreadLocalList<ReadOnlyMemory<byte>>.NewInstance(_nioBufferCount);
try
{
var c = FindComponent(index);
int i = c.Index;
int adjustment = c.Offset;
var s = c.Buf;
Expand Down Expand Up @@ -137,6 +111,22 @@ protected internal override ReadOnlySequence<byte> _GetSequence(int index, int c
}
}

protected internal override void _GetBytes(int index, Span<byte> destination, int length)
{
CheckIndex(index, length);
if (0u >= (uint)length) { return; }

_GetSequence(index, length).CopyTo(destination);
}

protected internal override void _GetBytes(int index, Memory<byte> destination, int length)
{
CheckIndex(index, length);
if (0u >= (uint)length) { return; }

_GetSequence(index, length).CopyTo(destination.Span);
}

public override Memory<byte> GetMemory(int sizeHintt = 0)
{
throw ThrowHelper.GetReadOnlyBufferException();
Expand Down
Loading

0 comments on commit 849cd3f

Please sign in to comment.