Skip to content

Commit

Permalink
work around for bug in Jitter / System.Memory
Browse files Browse the repository at this point in the history
  • Loading branch information
abbotware committed Jan 3, 2020
1 parent 6ae8fd2 commit be3c1f7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
6 changes: 3 additions & 3 deletions csharp/src/Apache.Arrow/ArrowBuffer.Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Builder<T> Append(T value)
public Builder<T> Append(ReadOnlySpan<T> source)
{
EnsureCapacity(source.Length);
source.CopyTo(Span.Slice(Length, source.Length));
source.CopyToFix(Span.Slice(Length, source.Length));
Length += source.Length;
return this;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public ArrowBuffer Build(MemoryAllocator allocator = default)

if (memoryOwner != null)
{
Memory.Slice(0, currentBytesLength).CopyTo(memoryOwner.Memory);
Memory.Slice(0, currentBytesLength).CopyToFix(memoryOwner.Memory);
}

return new ArrowBuffer(memoryOwner);
Expand Down Expand Up @@ -140,7 +140,7 @@ private void Reallocate(int length)
if (length != 0)
{
var memory = new Memory<byte>(new byte[length]);
Memory.CopyTo(memory);
Memory.CopyToFix(memory);

Memory = memory;
}
Expand Down
47 changes: 47 additions & 0 deletions csharp/src/Apache.Arrow/Extensions/SpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,65 @@
// limitations under the License.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Apache.Arrow
{
public static class SpanExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> CastTo<T>(this Span<byte> span)
where T: struct =>
MemoryMarshal.Cast<byte, T>(span);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<T> CastTo<T>(this ReadOnlySpan<byte> span)
where T: struct =>
MemoryMarshal.Cast<byte, T>(span);


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToFix<T>(this ReadOnlySpan<T> source, Span<T> target)
{
CopyToFix(source, 0, target, 0, source.Length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToFix<T>(this ReadOnlySpan<T> source, T[] target)
{
CopyToFix(source, 0, target, 0, source.Length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToFix<T>(this Span<T> source, Span<T> target)
{
CopyToFix(source, 0, target, 0, source.Length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToFix<T>(this Memory<T> source, Memory<T> target)
{
CopyToFix(source.Span, 0, target.Span, 0, source.Length);
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToFix<T>(ReadOnlySpan<T> source, int sourceOffset, Span<T> target, int targetOffset, int length)
{
for (int i = 0; i < length; ++i)
{
target[targetOffset + i] = source[sourceOffset + i];
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyToFix<T>(ReadOnlySpan<T> source, int sourceOffset, T[] target, int targetOffset, int length)
{
for (int i = 0; i < length; ++i)
{
target[targetOffset + i] = source[sourceOffset + i];
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static int Read(this Stream stream, Memory<byte> buffer)
try
{
int result = stream.Read(sharedBuffer, 0, buffer.Length);
new Span<byte>(sharedBuffer, 0, result).CopyTo(buffer.Span);
new Span<byte>(sharedBuffer, 0, result).CopyToFix(buffer.Span);
return result;
}
finally
Expand All @@ -63,7 +63,7 @@ async ValueTask<int> FinishReadAsync(Task<int> readTask, byte[] localBuffer, Mem
try
{
int result = await readTask.ConfigureAwait(false);
new Span<byte>(localBuffer, 0, result).CopyTo(localDestination.Span);
new Span<byte>(localBuffer, 0, result).CopyToFix(localDestination.Span);
return result;
}
finally
Expand All @@ -83,7 +83,7 @@ public static ValueTask WriteAsync(this Stream stream, ReadOnlyMemory<byte> buff
else
{
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
buffer.Span.CopyTo(sharedBuffer);
buffer.Span.CopyToFix(sharedBuffer);
return FinishWriteAsync(stream.WriteAsync(sharedBuffer, 0, buffer.Length, cancellationToken), sharedBuffer);
}
}
Expand Down
5 changes: 3 additions & 2 deletions csharp/src/Apache.Arrow/Flatbuf/FlatBuffers/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Apache.Arrow;

#if ENABLE_SPAN_T
using System.Buffers.Binary;
Expand Down Expand Up @@ -833,7 +834,7 @@ public int Put<T>(int offset, T[] x)
AssertOffsetAndLength(offset, numBytes);
// if we are LE, just do a block copy
#if ENABLE_SPAN_T
MemoryMarshal.Cast<T, byte>(x).CopyTo(_buffer.Span.Slice(offset, numBytes));
MemoryMarshal.Cast<T, byte>(x).CopyToFix(_buffer.Span.Slice(offset, numBytes));
#else
Buffer.BlockCopy(x, 0, _buffer.Buffer, offset, numBytes);
#endif
Expand Down Expand Up @@ -872,7 +873,7 @@ public int Put<T>(int offset, Span<T> x)
offset -= numBytes;
AssertOffsetAndLength(offset, numBytes);
// if we are LE, just do a block copy
MemoryMarshal.Cast<T, byte>(x).CopyTo(_buffer.Span.Slice(offset, numBytes));
MemoryMarshal.Cast<T, byte>(x).CopyToFix(_buffer.Span.Slice(offset, numBytes));
}
else
{
Expand Down

0 comments on commit be3c1f7

Please sign in to comment.