From d2750191465b1a6b59011ad49a8a5a34577557e6 Mon Sep 17 00:00:00 2001 From: Michael Sharp Date: Tue, 11 Jun 2024 16:18:17 -0600 Subject: [PATCH] changes to use const for stack alloc comparison --- .../Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs | 10 +++++----- .../System/Numerics/Tensors/netcore/TensorSpan.cs | 12 ++++++------ .../Numerics/Tensors/netcore/TensorSpanExtensions.cs | 2 ++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs index 7907da7a17060c..9b0c30b58c2439 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs @@ -522,7 +522,7 @@ public void CopyTo(scoped TensorSpan destination) { scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; @@ -569,7 +569,7 @@ public bool TryCopyTo(scoped TensorSpan destination) { scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; @@ -653,7 +653,7 @@ public ReadOnlyTensorSpan Slice(params scoped ReadOnlySpan ranges) scoped Span lengths; scoped Span offsets; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { lengths = stackalloc nint[Rank]; offsets = stackalloc nint[Rank]; @@ -692,7 +692,7 @@ public bool TryFlattenTo(scoped Span destination) { scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; @@ -733,7 +733,7 @@ public void FlattenTo(scoped Span destination) scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs index b85c11d29b427b..c64deebe4b3335 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs @@ -505,7 +505,7 @@ public void Clear() { scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; @@ -570,7 +570,7 @@ public void CopyTo(scoped TensorSpan destination) scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; @@ -613,7 +613,7 @@ public bool TryCopyTo(scoped TensorSpan destination) { scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; @@ -703,7 +703,7 @@ public TensorSpan Slice(params scoped ReadOnlySpan ranges) scoped Span lengths; scoped Span offsets; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { lengths = stackalloc nint[Rank]; offsets = stackalloc nint[Rank]; @@ -745,7 +745,7 @@ public bool TryFlattenTo(scoped Span destination) { scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; @@ -786,7 +786,7 @@ public void FlattenTo(scoped Span destination) scoped Span curIndexes; nint[]? curIndexesArray; - if (Rank > 5) + if (Rank > TensorSpan.MaxRankForStackAlloc) { curIndexesArray = ArrayPool.Shared.Rent(Rank); curIndexes = curIndexesArray; diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpanExtensions.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpanExtensions.cs index 88c280d27ae4ed..ce9d88447d1fc5 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpanExtensions.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpanExtensions.cs @@ -12,6 +12,8 @@ namespace System.Numerics.Tensors { public static class TensorSpan { + internal const int MaxRankForStackAlloc = 5; + #region SequenceEqual /// /// Determines whether two sequences are equal by comparing the elements using IEquatable{T}.Equals(T).