Skip to content
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

Switch .NET 5 target to .NET 6 #42

Merged
merged 8 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CommunityToolkit.Common/CommunityToolkit.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -14,8 +14,8 @@
<PackageTags>Incremental;Loading;Collection;IncrementalLoadingCollection;String;Array;Extensions;Helpers</PackageTags>
</PropertyGroup>

<!-- .NET Standard 2.1 and .NET 5 already have [NotNullIfNotNull] and [NotNullWhen] -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net5.0'">
<!-- .NET Standard 2.1 and .NET 6 already have [NotNullIfNotNull] and [NotNullWhen] -->
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<DefineConstants>NETSTANDARD2_1_OR_GREATER</DefineConstants>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion CommunityToolkit.Common/Deferred/EventDeferral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace CommunityToolkit.Common.Deferred;
/// </summary>
public class EventDeferral : IDisposable
{
//// TODO: If/when .NET 5 is base, we can upgrade to non-generic version
//// TODO: If/when .NET 6 is base, we can upgrade to non-generic version
private readonly TaskCompletionSource<object?> _taskCompletionSource = new();

internal EventDeferral()
Expand Down
2 changes: 1 addition & 1 deletion CommunityToolkit.Common/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static class TaskExtensions
{
// We need an explicit check to ensure the input task is not the cached
// Task.CompletedTask instance, because that can internally be stored as
// a Task<T> for some given T (eg. on .NET 5 it's VoidTaskResult), which
// a Task<T> for some given T (eg. on .NET 6 it's VoidTaskResult), which
// would cause the following code to return that result instead of null.
if (task != Task.CompletedTask)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if !NET5_0
#if !NET6_0_OR_GREATER

namespace System.Runtime.CompilerServices;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -34,7 +34,7 @@
</ItemGroup>
</When>

<When Condition="'$(TargetFramework)' == 'net5.0'">
<When Condition="'$(TargetFramework)' == 'net6.0'">
<PropertyGroup>
<DefineConstants>NETSTANDARD2_1_OR_GREATER</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if !NET5_0
#if !NET6_0_OR_GREATER

namespace System.Runtime.CompilerServices;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CommunityToolkit.HighPerformance.Buffers.Views;
using CommunityToolkit.HighPerformance.Helpers.Internals;
#if NET6_0_OR_GREATER
using BitOperations = System.Numerics.BitOperations;
#else
using BitOperations = CommunityToolkit.HighPerformance.Helpers.Internals.BitOperations;
#endif

namespace CommunityToolkit.HighPerformance.Buffers;

Expand Down Expand Up @@ -283,7 +287,7 @@ private void CheckBufferAndEnsureCapacity(int sizeHint)
[MethodImpl(MethodImplOptions.NoInlining)]
private void ResizeBuffer(int sizeHint)
{
int minimumSize = this.index + sizeHint;
uint minimumSize = (uint)this.index + (uint)sizeHint;

// The ArrayPool<T> class has a maximum threshold of 1024 * 1024 for the maximum length of
// pooled arrays, and once this is exceeded it will just allocate a new array every time
Expand All @@ -292,10 +296,10 @@ private void ResizeBuffer(int sizeHint)
// use is bigger than that threshold don't end up causing a resize every single time.
if (minimumSize > 1024 * 1024)
{
minimumSize = BitOperations.RoundUpPowerOfTwo(minimumSize);
minimumSize = BitOperations.RoundUpToPowerOf2(minimumSize);
}

this.pool.Resize(ref this.array, minimumSize);
this.pool.Resize(ref this.array, (int)minimumSize);
}

/// <inheritdoc/>
Expand Down
28 changes: 16 additions & 12 deletions CommunityToolkit.HighPerformance/Buffers/StringPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
using System.Runtime.CompilerServices;
using System.Text;
using CommunityToolkit.HighPerformance.Helpers;
#if NET6_0_OR_GREATER
using BitOperations = System.Numerics.BitOperations;
#else
using BitOperations = CommunityToolkit.HighPerformance.Helpers.Internals.BitOperations;
#endif

namespace CommunityToolkit.HighPerformance.Buffers;

Expand Down Expand Up @@ -66,13 +70,13 @@ public StringPool(int minimumSize)
minimumSize = Math.Max(minimumSize, MinimumSize);

// Calculates the rounded up factors for a specific size/factor pair
static void FindFactors(int size, int factor, out int x, out int y)
static void FindFactors(int size, int factor, out uint x, out uint y)
{
double a = Math.Sqrt((double)size / factor);
double b = factor * a;

x = BitOperations.RoundUpPowerOfTwo((int)a);
y = BitOperations.RoundUpPowerOfTwo((int)b);
x = BitOperations.RoundUpToPowerOf2((uint)a);
y = BitOperations.RoundUpToPowerOf2((uint)b);
}

// We want to find two powers of 2 factors that produce a number
Expand All @@ -84,13 +88,13 @@ static void FindFactors(int size, int factor, out int x, out int y)
// as well as the multithreading performance when locking on maps.
// We still want to contraint this number to avoid situations where we
// have a way too high number of maps compared to total size.
FindFactors(minimumSize, 2, out int x2, out int y2);
FindFactors(minimumSize, 3, out int x3, out int y3);
FindFactors(minimumSize, 4, out int x4, out int y4);
FindFactors(minimumSize, 2, out uint x2, out uint y2);
FindFactors(minimumSize, 3, out uint x3, out uint y3);
FindFactors(minimumSize, 4, out uint x4, out uint y4);

int p2 = x2 * y2;
int p3 = x3 * y3;
int p4 = x4 * y4;
uint p2 = x2 * y2;
uint p3 = x3 * y3;
uint p4 = x4 * y4;

if (p3 < p2)
{
Expand All @@ -113,12 +117,12 @@ static void FindFactors(int size, int factor, out int x, out int y)
// This lets us lock on each individual maps when retrieving a string instance.
foreach (ref FixedSizePriorityMap map in span)
{
map = new FixedSizePriorityMap(y2);
map = new FixedSizePriorityMap((int)y2);
}

this.numberOfMaps = x2;
this.numberOfMaps = (int)x2;

Size = p2;
Size = (int)p2;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net6.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -45,9 +45,9 @@
</ItemGroup>
</When>

<When Condition="'$(TargetFramework)' == 'net5.0'">
<When Condition="'$(TargetFramework)' == 'net6.0'">

<!-- NETSTANDARD2_1_OR_GREATER: includes both .NET Standard 2.1, .NET Core 3.1 and .NET 5 -->
<!-- NETSTANDARD2_1_OR_GREATER: includes both .NET Standard 2.1, .NET Core 3.1 and .NET 6 -->
<PropertyGroup>
<DefineConstants>NETSTANDARD2_1_OR_GREATER</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static partial class ArrayExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousGetReference<T>(this T[] array)
{
#if NET5_0
#if NET6_0_OR_GREATER
return ref MemoryMarshal.GetArrayDataReference(array);
#elif NETCOREAPP3_1
RawArrayData? arrayData = Unsafe.As<RawArrayData>(array)!;
Expand All @@ -59,7 +59,7 @@ public static ref T DangerousGetReference<T>(this T[] array)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousGetReferenceAt<T>(this T[] array, int i)
{
#if NET5_0
#if NET6_0_OR_GREATER
ref T r0 = ref MemoryMarshal.GetArrayDataReference(array);
ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i);

Expand Down
13 changes: 11 additions & 2 deletions CommunityToolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public static partial class ArrayExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousGetReference<T>(this T[,] array)
{
#if NETCOREAPP3_1
#if NET6_0_OR_GREATER
return ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(array));
#elif NETCOREAPP3_1
RawArray2DData? arrayData = Unsafe.As<RawArray2DData>(array)!;
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);

Expand Down Expand Up @@ -62,7 +64,14 @@ public static ref T DangerousGetReference<T>(this T[,] array)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousGetReferenceAt<T>(this T[,] array, int i, int j)
{
#if NETCOREAPP3_1
#if NET6_0_OR_GREATER
int width = array.GetLength(1);
nint index = ((nint)(uint)i * (nint)(uint)width) + (nint)(uint)j;
ref T r0 = ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(array));
ref T ri = ref Unsafe.Add(ref r0, index);

return ref ri;
#elif NETCOREAPP3_1
RawArray2DData? arrayData = Unsafe.As<RawArray2DData>(array)!;
nint offset = ((nint)(uint)i * (nint)(uint)arrayData.Width) + (nint)(uint)j;
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);
Expand Down
16 changes: 14 additions & 2 deletions CommunityToolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static partial class ArrayExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousGetReference<T>(this T[,,] array)
{
#if NETCOREAPP3_1
#if NET6_0_OR_GREATER
return ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(array));
#elif NETCOREAPP3_1
RawArray3DData? arrayData = Unsafe.As<RawArray3DData>(array)!;
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);

Expand Down Expand Up @@ -62,7 +64,17 @@ public static ref T DangerousGetReference<T>(this T[,,] array)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T DangerousGetReferenceAt<T>(this T[,,] array, int i, int j, int k)
{
#if NETCOREAPP3_1
#if NET6_0_OR_GREATER
int height = array.GetLength(1);
int width = array.GetLength(2);
nint index =
((nint)(uint)i * (nint)(uint)height * (nint)(uint)width) +
((nint)(uint)j * (nint)(uint)width) + (nint)(uint)k;
ref T r0 = ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(array));
ref T ri = ref Unsafe.Add(ref r0, index);

return ref ri;
#elif NETCOREAPP3_1
RawArray3DData? arrayData = Unsafe.As<RawArray3DData>(array)!;
nint offset =
((nint)(uint)i * (nint)(uint)arrayData.Height * (nint)(uint)arrayData.Width) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if NET5_0
#if NET6_0_OR_GREATER

using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// This extension is restricted to the .NET 5 because it shares the same BCL
// This extension is restricted to the .NET 6 because it shares the same BCL
// across all targets, ensuring that the layout of our Nullable<T> mapping type
// will be correct. Exposing this API on older targets (especially .NET Standard)
// is not guaranteed to be correct and could result in invalid memory accesses.
#if NET5_0

#if NET6_0_OR_GREATER

using System;
using System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class StringExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref char DangerousGetReference(this string text)
{
#if NETCOREAPP3_1 || NET5_0
#if NETCOREAPP3_1_OR_GREATER
return ref Unsafe.AsRef(text.GetPinnableReference());
#else
return ref MemoryMarshal.GetReference(text.AsSpan());
Expand All @@ -46,7 +46,7 @@ public static ref char DangerousGetReference(this string text)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref char DangerousGetReferenceAt(this string text, int i)
{
#if NETCOREAPP3_1 || NET5_0
#if NETCOREAPP3_1_OR_GREATER
ref char r0 = ref Unsafe.AsRef(text.GetPinnableReference());
#else
ref char r0 = ref MemoryMarshal.GetReference(text.AsSpan());
Expand Down
10 changes: 5 additions & 5 deletions CommunityToolkit.HighPerformance/Helpers/BitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
#if NETCOREAPP3_1 || NET5_0
#if NETCOREAPP3_1_OR_GREATER
using System.Runtime.Intrinsics.X86;
#endif

Expand Down Expand Up @@ -233,7 +233,7 @@ public static unsafe uint SetFlag(uint value, int n, bool flag)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ExtractRange(uint value, byte start, byte length)
{
#if NETCOREAPP3_1 || NET5_0
#if NETCOREAPP3_1_OR_GREATER
if (Bmi1.IsSupported)
{
return Bmi1.BitFieldExtract(value, start, length);
Expand Down Expand Up @@ -280,7 +280,7 @@ public static uint SetRange(uint value, byte start, byte length, uint flags)
uint loadMask = highBits << start;
uint storeMask = (flags & highBits) << start;

#if NETCOREAPP3_1 || NET5_0
#if NETCOREAPP3_1_OR_GREATER
if (Bmi1.IsSupported)
{
return Bmi1.AndNot(loadMask, value) | storeMask;
Expand Down Expand Up @@ -400,7 +400,7 @@ public static unsafe ulong SetFlag(ulong value, int n, bool flag)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ExtractRange(ulong value, byte start, byte length)
{
#if NETCOREAPP3_1 || NET5_0
#if NETCOREAPP3_1_OR_GREATER
if (Bmi1.X64.IsSupported)
{
return Bmi1.X64.BitFieldExtract(value, start, length);
Expand Down Expand Up @@ -447,7 +447,7 @@ public static ulong SetRange(ulong value, byte start, byte length, ulong flags)
ulong loadMask = highBits << start;
ulong storeMask = (flags & highBits) << start;

#if NETCOREAPP3_1 || NET5_0
#if NETCOREAPP3_1_OR_GREATER
if (Bmi1.X64.IsSupported)
{
return Bmi1.X64.AndNot(loadMask, value) | storeMask;
Expand Down
Loading