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

Clean up code for old runtimes #491

Merged
merged 1 commit into from
Nov 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
#undef PROMISE_DEBUG
#endif

using System;
// ArrayPool for old runtime is in Proto.Promises.Collections namespace.
#if (NETCOREAPP || NETSTANDARD2_0_OR_GREATER || UNITY_2021_2_OR_NEWER)
using System.Buffers;
#else
using Proto.Promises.Collections;
#endif
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down
5 changes: 0 additions & 5 deletions Package/Core/Collections/Internal/PoolBackedDequeInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
#endif

using System;
// ArrayPool for old runtime is in Proto.Promises.Collections namespace.
#if (NETCOREAPP || NETSTANDARD2_0_OR_GREATER || UNITY_2021_2_OR_NEWER)
using System.Buffers;
#else
using Proto.Promises.Collections;
#endif
using System.Diagnostics;
using System.Runtime.CompilerServices;

Expand Down
2 changes: 0 additions & 2 deletions Package/Core/Collections/TempCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#endif

using System;
#if UNITY_2021_2_OR_NEWER || !UNITY_2018_3_OR_NEWER
using System.Buffers;
#endif
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down
15 changes: 7 additions & 8 deletions Package/Core/ForOldRuntime/ArrayPool/ArrayPool.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// Different namespace to avoid collisions if someone else brings in the ArrayPool package.
namespace Proto.Promises.Collections
{
// We reference the nuget package for the nuget builds, but to not have any nuget dependencies in Unity, we include the source.
// ArrayPool was added in netstandard2.1, so we only need this in Unity versions older than 2021.2.
// We reference the nuget package for the nuget builds, but to not have any nuget dependencies in Unity, we include the source.
// ArrayPool was added in netstandard2.1, so we only need this in Unity versions older than 2021.2.
#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER

namespace System.Buffers
{
/// <summary>
/// Provides a resource pool that enables reusing instances of arrays.
/// </summary>
Expand All @@ -25,7 +24,7 @@ internal abstract class ArrayPool<T>
{
// Store the shared ArrayPool in a field of its derived sealed type so the Jit can "see" the exact type
// when the Shared property is inlined which will allow it to devirtualize calls made on it.
private static readonly TlsOverPerCoreLockedStacksArrayPool<T> s_shared = new TlsOverPerCoreLockedStacksArrayPool<T>();
private static readonly SharedArrayPool<T> s_shared = new SharedArrayPool<T>();

/// <summary>
/// Retrieves a shared <see cref="ArrayPool{T}"/> instance.
Expand Down Expand Up @@ -81,6 +80,6 @@ internal abstract class ArrayPool<T>
/// </remarks>
public abstract void Return(T[] array, bool clearArray = false);
}
}

#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
}
#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@

// Modified to work in Unity/netstandard2.0 without using the nuget package.
// Hooks up to Promise.Manager.ClearObjectPool() event instead of using Gen2GC callbacks.

#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
using System;
using System.Collections.Generic;
using System.Diagnostics;

using System.Runtime.CompilerServices;
using System.Threading;
#endif

namespace Proto.Promises.Collections
namespace System.Buffers
{
#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER

/// <summary>
/// Provides an ArrayPool implementation meant to be used as the singleton returned from ArrayPool.Shared.
/// </summary>
Expand All @@ -26,7 +20,7 @@ namespace Proto.Promises.Collections
/// checking its processor number, because multiple threads could interleave on the same core, and because
/// a thread is allowed to check other core's buckets if its core's bucket is empty/full.
/// </remarks>
internal sealed partial class TlsOverPerCoreLockedStacksArrayPool<T> : ArrayPool<T>
internal sealed partial class SharedArrayPool<T> : ArrayPool<T>
{
/// <summary>The number of buckets (array sizes) in the pool, one for each array length, starting from length 16.</summary>
private const int NumBuckets = 17; // Utilities.SelectBucketIndex(2*1024*1024)
Expand All @@ -44,7 +38,7 @@ internal sealed partial class TlsOverPerCoreLockedStacksArrayPool<T> : ArrayPool
private readonly PerCoreLockedStacks[] _buckets = new PerCoreLockedStacks[NumBuckets];

/// <summary>Initialize the pool.</summary>
public TlsOverPerCoreLockedStacksArrayPool()
public SharedArrayPool()
{
var sizes = new int[NumBuckets];
for (int i = 0; i < sizes.Length; i++)
Expand All @@ -53,8 +47,8 @@ public TlsOverPerCoreLockedStacksArrayPool()
}
_bucketArraySizes = sizes;

// Hook up to Promise.Manage.ClearObjectPool() event.
Internal.AddClearPoolListener(Trim);
// Hook up to Promise.Manager.ClearObjectPool() event.
Proto.Promises.Internal.AddClearPoolListener(Trim);
}

/// <summary>Allocate a new PerCoreLockedStacks and try to store it into the <see cref="_buckets"/> array.</summary>
Expand Down Expand Up @@ -271,6 +265,6 @@ public void Trim(uint tickCount, int id, int bucketSize)
}
}
}
}

#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
}
#endif
11 changes: 4 additions & 7 deletions Package/Core/ForOldRuntime/ArrayPool/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.

#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
using System;

using System.Diagnostics;
using System.Runtime.CompilerServices;
#endif

namespace Proto.Promises.Collections
namespace System.Buffers
{
#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER

internal static class Utilities
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -46,6 +43,6 @@ private static int Log2SoftwareFallback(uint value)
return (int) Math.Log(value, 2);
}
}
}

#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
}
#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
8 changes: 5 additions & 3 deletions Package/Core/ForOldRuntime/Span/ArraySortHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// TODO: Unity hasn't adopted .Net 5+ yet, and they usually use different compilation symbols than .Net SDK, so we'll have to update the compilation symbols here once Unity finally does adopt it.
#if !NET5_0_OR_GREATER

using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -12,8 +15,6 @@
// Using Proto.Promises namespace instead of the original System.Runtime.CompilerServices, and wrapped in Internal class.
namespace Proto.Promises
{
// TODO: Unity hasn't adopted .Net 5+ yet, and they usually use different compilation symbols than .Net SDK, so we'll have to update the compilation symbols here once Unity finally does adopt it.
#if !NET5_0_OR_GREATER
partial class Internal
{
internal static class IntrospectiveSortUtilities
Expand Down Expand Up @@ -220,5 +221,6 @@ private static void InsertionSort<TComparer>(Span<T> keys, TComparer comparer) w
}
}
}
#endif // !NET5_0_OR_GREATER
}

#endif // !NET5_0_OR_GREATER
12 changes: 7 additions & 5 deletions Package/Core/ForOldRuntime/Span/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// ReadOnlySpan is part of netstandard2.1, and we use the Span nuget package in netstandard2.0,
// but we don't use nuget packages in Unity, so we have to implement it ourselves.
#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER

using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
Expand All @@ -6,9 +10,6 @@

namespace System
{
// ReadOnlySpan is part of netstandard2.1, and we use the Span nuget package in netstandard2.0,
// but we don't use nuget packages in Unity, so we have to implement it ourselves.
#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
internal readonly ref struct ReadOnlySpan<T>
{
private readonly Span<T> _span;
Expand Down Expand Up @@ -110,5 +111,6 @@ public T Current

internal void CopyTo(Span<T> destination) => _span.CopyTo(destination);
}
#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
}
}

#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
12 changes: 7 additions & 5 deletions Package/Core/ForOldRuntime/Span/Span.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Span is part of netstandard2.1, and we use the Span nuget package in netstandard2.0,
// but we don't use nuget packages in Unity, so we have to implement it ourselves.
#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER

using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
Expand All @@ -6,9 +10,6 @@

namespace System
{
// Span is part of netstandard2.1, and we use the Span nuget package in netstandard2.0,
// but we don't use nuget packages in Unity, so we have to implement it ourselves.
#if UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
internal readonly ref struct Span<T>
{
// We only use it as a view over an array, not using pointers.
Expand Down Expand Up @@ -133,5 +134,6 @@ internal void CopyTo(Span<T> destination)
}
}
}
#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
}
}

#endif // UNITY_2018_3_OR_NEWER && !UNITY_2021_2_OR_NEWER
4 changes: 1 addition & 3 deletions Package/Core/ForOldRuntime/StackTraceHiddenAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// TODO: Unity hasn't adopted .Net 6+ yet, and they usually use different compilation symbols than .Net SDK, so we'll have to update the compilation symbols here once Unity finally does adopt it.
#if !NET6_0_OR_GREATER

using System;

namespace Proto.Promises
namespace System.Diagnostics
{
// This doesn't actually do anything before .Net 6, this is just to make it so we don't need to #if NET6_0_OR_GREATER at every place the attribute is used.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Struct, Inherited = false)]
Expand Down
5 changes: 0 additions & 5 deletions Package/Core/Linq/Internal/PoolBackedQueueInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
#endif

using System;
// ArrayPool for old runtime is in Proto.Promises.Collections namespace.
#if (NETCOREAPP || NETSTANDARD2_0_OR_GREATER || UNITY_2021_2_OR_NEWER)
using System.Buffers;
#else
using Proto.Promises.Collections;
#endif
using System.Diagnostics;
using System.Runtime.CompilerServices;

Expand Down
Loading