-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
ARROW-7040: [C#] - System.Memory Span.CopyTo - Crashes #6122
Changes from all commits
393be8c
585bd75
49dca4f
6ae8fd2
be3c1f7
6d4b414
4cf3472
2d9ae8c
93259d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,11 @@ | |
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit) inserting this blank line is unnecessary. |
||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'"> | ||
<Compile Remove="Extensions\StreamExtensions.netcoreapp2.1.cs" /> | ||
<Compile Remove="**/*.netcoreapp2.1.cs" /> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'"> | ||
<Compile Remove="Extensions\StreamExtensions.netstandard.cs" /> | ||
<Compile Remove="**/*.netstandard1.3.cs" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Apache.Arrow.Memory; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Apache.Arrow | ||
{ | ||
public partial struct ArrowBuffer | ||
{ | ||
public partial class Builder<T> | ||
where T : struct | ||
{ | ||
|
||
public Builder<T> Append(ReadOnlySpan<T> source) | ||
{ | ||
EnsureCapacity(source.Length); | ||
source.CopyTo(Span.Slice(Length, source.Length)); | ||
Length += source.Length; | ||
return this; | ||
} | ||
|
||
public ArrowBuffer Build(MemoryAllocator allocator = default) | ||
{ | ||
int currentBytesLength = Length * _size; | ||
int bufferLength = checked((int)BitUtility.RoundUpToMultipleOf64(currentBytesLength)); | ||
|
||
var memoryAllocator = allocator ?? MemoryAllocator.Default.Value; | ||
var memoryOwner = memoryAllocator.Allocate(bufferLength); | ||
|
||
if (memoryOwner != null) | ||
{ | ||
Memory.Slice(0, currentBytesLength).CopyTo(memoryOwner.Memory); | ||
} | ||
|
||
return new ArrowBuffer(memoryOwner); | ||
} | ||
|
||
private void Reallocate(int length) | ||
{ | ||
if (length < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(length)); | ||
} | ||
|
||
if (length != 0) | ||
{ | ||
var memory = new Memory<byte>(new byte[length]); | ||
Memory.CopyTo(memory); | ||
|
||
Memory = memory; | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Apache.Arrow.Memory; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Apache.Arrow | ||
{ | ||
public partial struct ArrowBuffer | ||
{ | ||
public partial class Builder<T> | ||
where T : struct | ||
{ | ||
|
||
public Builder<T> Append(ReadOnlySpan<T> source) | ||
{ | ||
EnsureCapacity(source.Length); | ||
source.CopyToFix(Span.Slice(Length, source.Length)); | ||
Length += source.Length; | ||
return this; | ||
} | ||
|
||
public ArrowBuffer Build(MemoryAllocator allocator = default) | ||
{ | ||
int currentBytesLength = Length * _size; | ||
int bufferLength = checked((int)BitUtility.RoundUpToMultipleOf64(currentBytesLength)); | ||
|
||
var memoryAllocator = allocator ?? MemoryAllocator.Default.Value; | ||
var memoryOwner = memoryAllocator.Allocate(bufferLength); | ||
|
||
if (memoryOwner != null) | ||
{ | ||
Memory.Slice(0, currentBytesLength).CopyToFix(memoryOwner.Memory); | ||
} | ||
|
||
return new ArrowBuffer(memoryOwner); | ||
} | ||
|
||
private void Reallocate(int length) | ||
{ | ||
if (length < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(length)); | ||
} | ||
|
||
if (length != 0) | ||
{ | ||
var memory = new Memory<byte>(new byte[length]); | ||
Memory.CopyToFix(memory); | ||
|
||
Memory = memory; | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,16 +14,19 @@ | |
// limitations under the License. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Apache.Arrow | ||
{ | ||
public static class SpanExtensions | ||
public static partial class SpanExtensions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit) double spaces between |
||
{ | ||
[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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Apache.Arrow | ||
{ | ||
// TODO: remove all CopyToFix methods after a fix has been released for: https://github.com/dotnet/coreclr/issues/27590 | ||
public static partial class SpanExtensions | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static void CopyToFix<T>(this ReadOnlySpan<T> source, Span<T> target) | ||
{ | ||
CopyToFix(source, 0, target, 0, source.Length); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static void CopyToFix<T>(this ReadOnlySpan<T> source, T[] target) | ||
{ | ||
CopyToFix(source, 0, target, 0, source.Length); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static void CopyToFix<T>(this Span<T> source, Span<T> target) | ||
{ | ||
CopyToFix(source, 0, target, 0, source.Length); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static void CopyToFix<T>(this Memory<T> source, Memory<T> target) | ||
{ | ||
CopyToFix(source.Span, 0, target.Span, 0, source.Length); | ||
} | ||
|
||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal 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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we guaranteed that If they do, this is not going to work correctly. If the beginning of |
||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static void CopyToFix<T>(ReadOnlySpan<T> source, int sourceOffset, T[] target, int targetOffset, int length) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this method? Won't the above method that takes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it might no longer be needed (I removed the usage in flat buffers) - I created different overloads to get the compilation to work when I did a global replace of Span.CopyTo - If i created it, it was being used at some point for compliation |
||
{ | ||
for (int i = 0; i < length; ++i) | ||
{ | ||
target[targetOffset + i] = source[sourceOffset + i]; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Apache.Arrow; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you revert this change as well? |
||
|
||
#if ENABLE_SPAN_T | ||
using System.Buffers.Binary; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to change the
.gitignore
file with this change. Can you explain why this is being proposed here?If there is a C# specific pattern that needs to be ignored (like
.vs
), there is acsharp/.gitignore
file for the C# specific patterns - but it already has.vs
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you click "Open in Visual Studio" on github - the root folder is opened and folders are created automatically... Any one else that does that will probably see those folders get created as well - so its just annoying to see those all the time..