Skip to content

Commit

Permalink
SpanT is available in .Net Standard 2.0. (#6137)
Browse files Browse the repository at this point in the history
  • Loading branch information
harujoh authored Sep 25, 2020
1 parent dca1252 commit 52e3628
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
74 changes: 49 additions & 25 deletions net/FlatBuffers/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@
using System.Runtime.InteropServices;
using System.Text;

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
using System.Buffers.Binary;
#endif

#if ENABLE_SPAN_T && !UNSAFE_BYTEBUFFER && !NETSTANDARD2_1
#warning ENABLE_SPAN_T requires UNSAFE_BYTEBUFFER to also be defined
#endif

namespace FlatBuffers
{
public abstract class ByteBufferAllocator
{
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
public abstract Span<byte> Span { get; }
public abstract ReadOnlySpan<byte> ReadOnlySpan { get; }
public abstract Memory<byte> Memory { get; }
Expand Down Expand Up @@ -98,7 +102,7 @@ public override void GrowFront(int newSize)
InitBuffer();
}

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
public override Span<byte> Span => _buffer;
public override ReadOnlySpan<byte> ReadOnlySpan => _buffer;
public override Memory<byte> Memory => _buffer;
Expand All @@ -108,7 +112,7 @@ public override void GrowFront(int newSize)
private void InitBuffer()
{
Length = _buffer.Length;
#if !ENABLE_SPAN_T || !NETSTANDARD2_1
#if !ENABLE_SPAN_T
Buffer = _buffer;
#endif
}
Expand Down Expand Up @@ -220,7 +224,7 @@ public static int ArraySize<T>(T[] x)
return SizeOf<T>() * x.Length;
}

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
public static int ArraySize<T>(Span<T> x)
{
return SizeOf<T>() * x.Length;
Expand All @@ -229,7 +233,7 @@ public static int ArraySize<T>(Span<T> x)

// Get a portion of the buffer casted into an array of type T, given
// the buffer position and length.
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
public T[] ToArray<T>(int pos, int len)
where T : struct
{
Expand Down Expand Up @@ -257,7 +261,7 @@ public byte[] ToFullArray()
return ToArray<byte>(0, Length);
}

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
public ReadOnlyMemory<byte> ToReadOnlyMemory(int pos, int len)
{
return _buffer.ReadOnlyMemory.Slice(pos, len);
Expand Down Expand Up @@ -320,7 +324,7 @@ static public ulong ReverseBytes(ulong input)
((input & 0xFF00000000000000UL) >> 56));
}

#if !UNSAFE_BYTEBUFFER && !ENABLE_SPAN_T
#if !UNSAFE_BYTEBUFFER && (!ENABLE_SPAN_T || !NETSTANDARD2_1)
// Helper functions for the safe (but slower) version.
protected void WriteLittleEndian(int offset, int count, ulong data)
{
Expand Down Expand Up @@ -399,7 +403,7 @@ protected ulong ReadLittleEndian(int offset, int count)
}
return r;
}
#endif // !UNSAFE_BYTEBUFFER && !ENABLE_SPAN_T
#endif

private void AssertOffsetAndLength(int offset, int length)
{
Expand All @@ -410,7 +414,7 @@ private void AssertOffsetAndLength(int offset, int length)
#endif
}

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)

public void PutSbyte(int offset, sbyte value)
{
Expand Down Expand Up @@ -458,7 +462,19 @@ public void Put(int offset, byte value)
PutByte(offset, value);
}

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public unsafe void PutStringUTF8(int offset, string value)
{
AssertOffsetAndLength(offset, value.Length);
fixed (char* s = value)
{
fixed (byte* buffer = &MemoryMarshal.GetReference(_buffer.Span))
{
Encoding.UTF8.GetBytes(s, value.Length, buffer + offset, Length - offset);
}
}
}
#elif ENABLE_SPAN_T && NETSTANDARD2_1
public void PutStringUTF8(int offset, string value)
{
AssertOffsetAndLength(offset, value.Length);
Expand All @@ -484,7 +500,7 @@ public void PutShort(int offset, short value)
public unsafe void PutUshort(int offset, ushort value)
{
AssertOffsetAndLength(offset, sizeof(ushort));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
Span<byte> span = _buffer.Span.Slice(offset);
BinaryPrimitives.WriteUInt16LittleEndian(span, value);
#else
Expand All @@ -505,7 +521,7 @@ public void PutInt(int offset, int value)
public unsafe void PutUint(int offset, uint value)
{
AssertOffsetAndLength(offset, sizeof(uint));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
Span<byte> span = _buffer.Span.Slice(offset);
BinaryPrimitives.WriteUInt32LittleEndian(span, value);
#else
Expand All @@ -526,7 +542,7 @@ public unsafe void PutLong(int offset, long value)
public unsafe void PutUlong(int offset, ulong value)
{
AssertOffsetAndLength(offset, sizeof(ulong));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
Span<byte> span = _buffer.Span.Slice(offset);
BinaryPrimitives.WriteUInt64LittleEndian(span, value);
#else
Expand All @@ -542,7 +558,7 @@ public unsafe void PutUlong(int offset, ulong value)
public unsafe void PutFloat(int offset, float value)
{
AssertOffsetAndLength(offset, sizeof(float));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
fixed (byte* ptr = &MemoryMarshal.GetReference(_buffer.Span))
#else
fixed (byte* ptr = _buffer.Buffer)
Expand All @@ -562,7 +578,7 @@ public unsafe void PutFloat(int offset, float value)
public unsafe void PutDouble(int offset, double value)
{
AssertOffsetAndLength(offset, sizeof(double));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
fixed (byte* ptr = &MemoryMarshal.GetReference(_buffer.Span))
#else
fixed (byte* ptr = _buffer.Buffer)
Expand Down Expand Up @@ -635,7 +651,7 @@ public void PutDouble(int offset, double value)

#endif // UNSAFE_BYTEBUFFER

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
public sbyte GetSbyte(int index)
{
AssertOffsetAndLength(index, sizeof(sbyte));
Expand All @@ -661,7 +677,15 @@ public byte Get(int index)
}
#endif

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public unsafe string GetStringUTF8(int startPos, int len)
{
fixed (byte* buffer = &MemoryMarshal.GetReference(_buffer.ReadOnlySpan.Slice(startPos)))
{
return Encoding.UTF8.GetString(buffer, len);
}
}
#elif ENABLE_SPAN_T && NETSTANDARD2_1
public string GetStringUTF8(int startPos, int len)
{
return Encoding.UTF8.GetString(_buffer.Span.Slice(startPos, len));
Expand All @@ -683,7 +707,7 @@ public short GetShort(int offset)
public unsafe ushort GetUshort(int offset)
{
AssertOffsetAndLength(offset, sizeof(ushort));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
ReadOnlySpan<byte> span = _buffer.ReadOnlySpan.Slice(offset);
return BinaryPrimitives.ReadUInt16LittleEndian(span);
#else
Expand All @@ -704,7 +728,7 @@ public int GetInt(int offset)
public unsafe uint GetUint(int offset)
{
AssertOffsetAndLength(offset, sizeof(uint));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
ReadOnlySpan<byte> span = _buffer.ReadOnlySpan.Slice(offset);
return BinaryPrimitives.ReadUInt32LittleEndian(span);
#else
Expand All @@ -725,7 +749,7 @@ public long GetLong(int offset)
public unsafe ulong GetUlong(int offset)
{
AssertOffsetAndLength(offset, sizeof(ulong));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
ReadOnlySpan<byte> span = _buffer.ReadOnlySpan.Slice(offset);
return BinaryPrimitives.ReadUInt64LittleEndian(span);
#else
Expand All @@ -741,7 +765,7 @@ public unsafe ulong GetUlong(int offset)
public unsafe float GetFloat(int offset)
{
AssertOffsetAndLength(offset, sizeof(float));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
fixed (byte* ptr = &MemoryMarshal.GetReference(_buffer.ReadOnlySpan))
#else
fixed (byte* ptr = _buffer.Buffer)
Expand All @@ -762,7 +786,7 @@ public unsafe float GetFloat(int offset)
public unsafe double GetDouble(int offset)
{
AssertOffsetAndLength(offset, sizeof(double));
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T // && UNSAFE_BYTEBUFFER
fixed (byte* ptr = &MemoryMarshal.GetReference(_buffer.ReadOnlySpan))
#else
fixed (byte* ptr = _buffer.Buffer)
Expand Down Expand Up @@ -861,7 +885,7 @@ public int Put<T>(int offset, T[] x)
offset -= numBytes;
AssertOffsetAndLength(offset, numBytes);
// if we are LE, just do a block copy
#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
MemoryMarshal.Cast<T, byte>(x).CopyTo(_buffer.Span.Slice(offset, numBytes));
#else
Buffer.BlockCopy(x, 0, _buffer.Buffer, offset, numBytes);
Expand All @@ -880,7 +904,7 @@ public int Put<T>(int offset, T[] x)
return offset;
}

#if ENABLE_SPAN_T && NETSTANDARD2_1
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
public int Put<T>(int offset, Span<T> x)
where T : struct
{
Expand Down
6 changes: 3 additions & 3 deletions net/FlatBuffers/FlatBufferBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void Put<T>(T[] x)
_space = _bb.Put(_space, x);
}

#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
/// <summary>
/// Puts a span of type T into this builder at the
/// current offset
Expand Down Expand Up @@ -317,7 +317,7 @@ public void Add<T>(T[] x)
Put(x);
}

#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
/// <summary>
/// Add a span of type T to the buffer (aligns the data and grows if necessary).
/// </summary>
Expand Down Expand Up @@ -567,7 +567,7 @@ public StringOffset CreateString(string s)
}


#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
/// <summary>
/// Creates a string in the buffer from a Span containing
/// a UTF8 string.
Expand Down
2 changes: 1 addition & 1 deletion net/FlatBuffers/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public int __vector(int offset)
return offset + bb.GetInt(offset) + sizeof(int); // data starts after the length
}

#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && (UNSAFE_BYTEBUFFER || NETSTANDARD2_1)
// Get the data of a vector whoses offset is stored at "offset" in this object as an
// Spant&lt;byte&gt;. If the vector is not present in the ByteBuffer,
// then an empty span will be returned.
Expand Down

0 comments on commit 52e3628

Please sign in to comment.