Skip to content

Commit

Permalink
GH-41349: [C#] Optimize DecimalUtility.GetBytes(SqlDecimal) on .NET 7+ (
Browse files Browse the repository at this point in the history
#42150)

### What changes are included in this PR?

Adds code to avoid an allocation when converting from SqlDecimal to Decimal128.
Adds a .NET 8 target to Apache.Arrow.csproj to enable the optimization.
Makes a small source change to build successfully with latest C# version.

### Are these changes tested?

Yes.

### Are there any user-facing changes?

No.

Closes #41349 
* GitHub Issue: #41349

Authored-by: Curt Hagenlocher <[email protected]>
Signed-off-by: Curt Hagenlocher <[email protected]>
  • Loading branch information
CurtHagenlocher authored Jun 14, 2024
1 parent 3333648 commit a7a46b2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions csharp/src/Apache.Arrow/Apache.Arrow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
</PropertyGroup>

<PropertyGroup Condition="'$(IsWindows)'=='true'">
<TargetFrameworks>netstandard2.0;net6.0;net462</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net8.0;net462</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(IsWindows)'!='true'">
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETStandard' or '$(TargetFramework)' == 'net462'">
Expand Down
9 changes: 6 additions & 3 deletions csharp/src/Apache.Arrow/DecimalUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,12 @@ internal static void GetBytes(SqlDecimal value, int precision, int scale, Span<b
value = SqlDecimal.ConvertToPrecScale(value, precision, scale);
}

// TODO: Consider groveling in the internals to avoid the probable allocation
Span<int> span = bytes.CastTo<int>();
value.Data.AsSpan().CopyTo(span);
#if NET7_0_OR_GREATER
value.WriteTdsValue(bytes.CastTo<uint>());
#else
value.Data.AsSpan().CopyTo(bytes.CastTo<int>());
#endif

if (!value.IsPositive)
{
Span<long> longSpan = bytes.CastTo<long>();
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Apache.Arrow/Scalars/BinaryView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private BinaryView(int length, int prefix, int bufferIndex, int offset)
public bool IsInline => Length <= MaxInlineLength;

#if NET5_0_OR_GREATER
public ReadOnlySpan<byte> Bytes => MemoryMarshal.CreateReadOnlySpan<byte>(ref Unsafe.AsRef(_inline[0]), IsInline ? Length : PrefixLength);
public ReadOnlySpan<byte> Bytes => MemoryMarshal.CreateReadOnlySpan<byte>(ref Unsafe.AsRef(in _inline[0]), IsInline ? Length : PrefixLength);
#else
public unsafe ReadOnlySpan<byte> Bytes => new ReadOnlySpan<byte>(Unsafe.AsPointer(ref _inline[0]), IsInline ? Length : PrefixLength);
#endif
Expand Down

0 comments on commit a7a46b2

Please sign in to comment.