Skip to content

Commit df04fce

Browse files
committed
Don't heap alloc on indices selection on netstandard 2.1
1 parent e529a89 commit df04fce

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

BCnEnc.Net/Encoder/Bptc/BptcEncodingHelpers.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,17 @@ public static int[] Rank2SubsetPartitions(ClusterIndices4X4 reducedIndicesBlock,
5757
// Copy struct to array before the closure so that reducedIndicesBlock is not
5858
// heap-captured. On .NET Framework, MemoryMarshalPolyfills.CreateSpan uses
5959
// Unsafe.AsPointer, which is only GC-safe for stack-allocated structs.
60+
#if NETSTANDARD2_0
6061
var indices = new int[16];
61-
for (var k = 0; k < 16; k++) indices[k] = reducedIndicesBlock[k];
62+
reducedIndicesBlock.AsSpan.CopyTo(indices);
63+
#endif
6264

6365
int CalculatePartitionError(int partitionIndex)
6466
{
67+
#if NETSTANDARD2_1
68+
var indices = reducedIndicesBlock.AsSpan;
69+
#endif
70+
6571
var error = 0;
6672
ReadOnlySpan<int> partitionTable = Bc7Block.Subsets2PartitionTable[partitionIndex];
6773
Span<int> subset0 = stackalloc int[numDistinctClusters];
@@ -122,11 +128,17 @@ public static int[] Rank3SubsetPartitions(ClusterIndices4X4 reducedIndicesBlock,
122128
// Copy struct to array before the closure so that reducedIndicesBlock is not
123129
// heap-captured. On .NET Framework, MemoryMarshalPolyfills.CreateSpan uses
124130
// Unsafe.AsPointer, which is only GC-safe for stack-allocated structs.
131+
#if NETSTANDARD2_0
125132
var indices = new int[16];
126-
for (var k = 0; k < 16; k++) indices[k] = reducedIndicesBlock[k];
133+
reducedIndicesBlock.AsSpan.CopyTo(indices);
134+
#endif
127135

128136
int CalculatePartitionError(int partitionIndex)
129137
{
138+
#if NETSTANDARD2_1
139+
var indices = reducedIndicesBlock.AsSpan;
140+
#endif
141+
130142
var error = 0;
131143
ReadOnlySpan<int> partitionTable = Bc7Block.Subsets3PartitionTable[partitionIndex];
132144

0 commit comments

Comments
 (0)