Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use C# compiler provided nint/nuint #60

Merged
merged 5 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ This is a fork of [DotNetty](https://github.com/azure/dotnetty).
* Nightly builds are available on [MyGet](https://www.myget.org/F/cuteant/api/v2).


|NuGet Package|Status|
|------|-------------|
|SpanNetty.Common|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Common)](https://www.nuget.org/packages/SpanNetty.Common/)|
|SpanNetty.Buffers|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Buffers)](https://www.nuget.org/packages/SpanNetty.Buffers/)|
|SpanNetty.Codecs|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs)](https://www.nuget.org/packages/SpanNetty.Codecs/)|
|SpanNetty.Codecs.Http|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs.Http)](https://www.nuget.org/packages/SpanNetty.Codecs.Http/)|
|SpanNetty.Codecs.Http2|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs.Http2)](https://www.nuget.org/packages/SpanNetty.Codecs.Http2/)|
|SpanNetty.Codecs.Mqtt|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs.Mqtt)](https://www.nuget.org/packages/SpanNetty.Codecs.Mqtt/)|
|SpanNetty.Codecs.Protobuf|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs.Protobuf)](https://www.nuget.org/packages/SpanNetty.Codecs.Protobuf/)|
|SpanNetty.Handlers|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Handlers)](https://www.nuget.org/packages/SpanNetty.Handlers/)|
|SpanNetty.Transport|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Transport)](https://www.nuget.org/packages/SpanNetty.Transport/)|
|SpanNetty.Transport.Libuv|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Transport.Libuv)](https://www.nuget.org/packages/SpanNetty.Transport.Libuv/)|
|Package|NuGet Version|MyGet Version|
|------|-------------|-------------|
|SpanNetty.Common|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Common)](https://www.nuget.org/packages/SpanNetty.Common/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Common)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Common)|
|SpanNetty.Buffers|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Buffers)](https://www.nuget.org/packages/SpanNetty.Buffers/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Buffers)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Buffers)|
|SpanNetty.Codecs|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs)](https://www.nuget.org/packages/SpanNetty.Codecs/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Codecs)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Codecs)|
|SpanNetty.Codecs.Http|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs.Http)](https://www.nuget.org/packages/SpanNetty.Codecs.Http/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Codecs.Http)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Codecs.Http)|
|SpanNetty.Codecs.Http2|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs.Http2)](https://www.nuget.org/packages/SpanNetty.Codecs.Http2/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Codecs.Http2)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Codecs.Http2)|
|SpanNetty.Codecs.Mqtt|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs.Mqtt)](https://www.nuget.org/packages/SpanNetty.Codecs.Mqtt/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Codecs.Mqtt)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Codecs.Mqtt)|
|SpanNetty.Codecs.Protobuf|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Codecs.Protobuf)](https://www.nuget.org/packages/SpanNetty.Codecs.Protobuf/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Codecs.Protobuf)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Codecs.Protobuf)|
|SpanNetty.Handlers|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Handlers)](https://www.nuget.org/packages/SpanNetty.Handlers/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Handlers)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Handlers)|
|SpanNetty.Transport|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Transport)](https://www.nuget.org/packages/SpanNetty.Transport/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Transport)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Transport)|
|SpanNetty.Transport.Libuv|[![NuGet Version and Downloads count](https://buildstats.info/nuget/SpanNetty.Transport.Libuv)](https://www.nuget.org/packages/SpanNetty.Transport.Libuv/)|[![MyGet Version](https://img.shields.io/myget/cuteant/vpre/SpanNetty.Transport.Libuv)](https://www.myget.org/feed/cuteant/package/nuget/SpanNetty.Transport.Libuv)|

## Performance

Expand Down
2 changes: 1 addition & 1 deletion localPublish.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ call Ensure-DotNetSdk.cmd
SET SOLUTION=%CMDHOME%\DotNetty.CrossPlatform.sln

:: Set DateTime prefix or suffix for builds
if "%PublishConfiguration%" == "dev" for /f %%j in ('powershell -NoProfile -ExecutionPolicy ByPass Get-Date -format "{yyMMddHHmm}"') do set DATE_SUFFIX=%%j
if "%PublishConfiguration%" == "dev" for /f %%j in ('powershell -NoProfile -ExecutionPolicy ByPass Get-Date -format "{yyMMdd}"') do set DATE_SUFFIX=%%j
if "%PublishConfiguration%" == "dev" SET AdditionalConfigurationProperties=;VersionDateSuffix=%DATE_SUFFIX%
if "%PublishConfiguration%" == "release" for /f %%j in ('powershell -NoProfile -ExecutionPolicy ByPass Get-Date -format "{yyMM}"') do set YEAR_PREFIX=%%j
if "%PublishConfiguration%" == "release" for /f %%j in ('powershell -NoProfile -ExecutionPolicy ByPass Get-Date -format "{ddHH}"') do set DATE_PREFIX=%%j
Expand Down
42 changes: 41 additions & 1 deletion src/DotNetty.Buffers/AbstractByteBuffer.NetStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public virtual int ReadBytes(Span<byte> destination)
var readableBytes = Math.Min(_writerIndex - readerIndex, destination.Length);
if (readableBytes > 0)
{
_GetBytes(readerIndex, destination, readableBytes);
_GetBytes(readerIndex, destination, readableBytes);
_readerIndex = readerIndex + readableBytes;
}
return readableBytes;
Expand Down Expand Up @@ -398,14 +398,22 @@ public virtual int IndexOf(int fromIndex, int toIndex, byte value)
internal protected virtual int IndexOf0(int index, int count, byte value)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.IndexOf(value);
#else
var result = SpanHelpers.IndexOf(ref MemoryMarshal.GetReference(span), value, span.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

internal protected virtual int LastIndexOf0(int index, int count, byte value)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.LastIndexOf(value);
#else
var result = SpanHelpers.LastIndexOf(ref MemoryMarshal.GetReference(span), value, span.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

Expand All @@ -431,14 +439,22 @@ public virtual int IndexOf(int fromIndex, int toIndex, in ReadOnlySpan<byte> val
internal protected virtual int IndexOf0(int index, int count, in ReadOnlySpan<byte> values)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.IndexOf(values);
#else
var result = SpanHelpers.IndexOf(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

internal protected virtual int LastIndexOf0(int index, int count, in ReadOnlySpan<byte> values)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.LastIndexOf(values);
#else
var result = SpanHelpers.LastIndexOf(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

Expand All @@ -464,14 +480,22 @@ public virtual int IndexOfAny(int fromIndex, int toIndex, byte value0, byte valu
internal protected virtual int IndexOfAny0(int index, int count, byte value0, byte value1)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.IndexOfAny(value0, value1);
#else
var result = SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, span.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

internal protected virtual int LastIndexOfAny0(int index, int count, byte value0, byte value1)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.LastIndexOfAny(value0, value1);
#else
var result = SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, span.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

Expand All @@ -497,14 +521,22 @@ public virtual int IndexOfAny(int fromIndex, int toIndex, byte value0, byte valu
internal protected virtual int IndexOfAny0(int index, int count, byte value0, byte value1, byte value2)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.IndexOfAny(value0, value1, value2);
#else
var result = SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, value2, span.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

internal protected virtual int LastIndexOfAny0(int index, int count, byte value0, byte value1, byte value2)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.LastIndexOfAny(value0, value1, value2);
#else
var result = SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, value2, span.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

Expand All @@ -530,14 +562,22 @@ public virtual int IndexOfAny(int fromIndex, int toIndex, in ReadOnlySpan<byte>
internal protected virtual int IndexOfAny0(int index, int count, in ReadOnlySpan<byte> values)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.IndexOfAny(values);
#else
var result = SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}

internal protected virtual int LastIndexOfAny0(int index, int count, in ReadOnlySpan<byte> values)
{
var span = GetReadableSpan(index, count);
#if NET
var result = span.LastIndexOfAny(values);
#else
var result = SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length);
#endif
return (uint)result < SharedConstants.uIndexNotFound ? index + result : result;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/DotNetty.Buffers/ArrayPooledByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
using System.Buffers;
using DotNetty.Common;
using DotNetty.Common.Internal;
#if NET
using System.Runtime.InteropServices;
#endif

namespace DotNetty.Buffers
{
Expand Down Expand Up @@ -186,7 +189,11 @@ public sealed override byte[] Array
public sealed override ref byte GetPinnableMemoryAddress()
{
EnsureAccessible();
#if NET
return ref MemoryMarshal.GetArrayDataReference(Memory);
#else
return ref Memory[0];
#endif
}

public sealed override IntPtr AddressOfPinnedMemory() => IntPtr.Zero;
Expand Down
12 changes: 11 additions & 1 deletion src/DotNetty.Buffers/ArrayPooledUnsafeDirectByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
using System.Threading;
using System.Threading.Tasks;
using DotNetty.Common;
#if NET
using System.Runtime.InteropServices;
#endif

namespace DotNetty.Buffers
{
Expand Down Expand Up @@ -248,7 +251,14 @@ public sealed override IByteBuffer Copy(int index, int length)
}

[MethodImpl(InlineMethod.AggressiveOptimization)]
ref byte Addr(int index) => ref Memory[index];
ref byte Addr(int index)
{
#if NET
return ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Memory), index);
#else
return ref Memory[index];
#endif
}

public sealed override IByteBuffer SetZero(int index, int length)
{
Expand Down
8 changes: 7 additions & 1 deletion src/DotNetty.Buffers/ByteBufferUtil.Comparable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ namespace DotNetty.Buffers
{
using System;
using System.Runtime.CompilerServices;
using DotNetty.Common.Utilities;
#if !NET
using System.Runtime.InteropServices;
using DotNetty.Common.Internal;
using DotNetty.Common.Utilities;
#endif

partial class ByteBufferUtil
{
Expand All @@ -44,7 +46,11 @@ public static int Compare(IByteBuffer bufferA, IByteBuffer bufferB)
{
var spanA = bufferA.GetReadableSpan();
var spanB = bufferB.GetReadableSpan();
#if NET
return spanA.SequenceCompareTo(spanB);
#else
return SpanHelpers.SequenceCompareTo(ref MemoryMarshal.GetReference(spanA), spanA.Length, ref MemoryMarshal.GetReference(spanB), spanB.Length);
#endif
}
return CompareSlow(bufferA, bufferB);
}
Expand Down
8 changes: 7 additions & 1 deletion src/DotNetty.Buffers/ByteBufferUtil.Equatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
namespace DotNetty.Buffers
{
using System;
using DotNetty.Common.Utilities;
#if !NET
using System.Runtime.InteropServices;
using DotNetty.Common.Internal;
using DotNetty.Common.Utilities;
#endif

partial class ByteBufferUtil
{
Expand All @@ -55,7 +57,11 @@ public static bool Equals(IByteBuffer a, int aStartIndex, IByteBuffer b, int bSt
{
var spanA = a.GetReadableSpan(aStartIndex, length);
var spanB = b.GetReadableSpan(bStartIndex, length);
#if NET
return spanA.SequenceEqual(spanB);
#else
return SpanHelpers.SequenceEqual(ref MemoryMarshal.GetReference(spanA), ref MemoryMarshal.GetReference(spanB), length);
#endif
}
return EqualsSlow(a, aStartIndex, b, bStartIndex, length);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetty.Buffers/ByteBufferUtil.Utf8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ static bool IsUtf8(IByteBuffer buf, int index, int length)
var utf8Span = buf.GetReadableSpan(index, length);
ref byte utf8Source = ref MemoryMarshal.GetReference(utf8Span);

IntPtr offset = (IntPtr)0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
nint offset = 0; // Use IntPtr for arithmetic to avoid unnecessary 64->32->64 truncations
uint uLength = (uint)length;

while ((uint)index < uLength)
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetty.Buffers/DotNetty.Buffers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="..\nuget.props" />

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netstandard2.1;$(StandardTfms)</TargetFrameworks>
<TargetFrameworks>net5.0;netcoreapp2.1;netstandard2.1;$(StandardTfms)</TargetFrameworks>
<RootNamespace>DotNetty.Buffers</RootNamespace>
<AssemblyName>SpanNetty.Buffers</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
8 changes: 8 additions & 0 deletions src/DotNetty.Buffers/PooledHeapByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace DotNetty.Buffers
using System.Threading.Tasks;
using DotNetty.Common;
using DotNetty.Common.Internal;
#if NET
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#endif

sealed partial class PooledHeapByteBuffer : PooledByteBuffer<byte[]>
{
Expand Down Expand Up @@ -212,7 +216,11 @@ public sealed override byte[] Array
public sealed override ref byte GetPinnableMemoryAddress()
{
EnsureAccessible();
#if NET
return ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Memory), Offset);
#else
return ref Memory[Offset];
#endif
}

public sealed override IntPtr AddressOfPinnedMemory() => IntPtr.Zero;
Expand Down
Loading