From 96fb6c3977ec01f7ad19ac4adf1d4e95e366a79c Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Fri, 5 Feb 2021 12:31:19 -0500 Subject: [PATCH 1/2] Enable size-optimized LINQ build for Browser Re-enable the size-optimized variant of LINQ and use it for the Browser build of the library. --- .../Collections/Generic/LargeArrayBuilder.SizeOpt.cs | 2 -- .../tests/TestUtilities/System/PlatformDetection.cs | 3 +++ src/libraries/System.Linq/src/System.Linq.csproj | 12 ++++++++++-- src/libraries/System.Linq/tests/ConcatTests.cs | 2 +- .../System.Linq/tests/EmptyPartitionTests.cs | 4 ++-- src/libraries/System.Linq/tests/OrderedSubsetting.cs | 2 +- src/libraries/System.Linq/tests/RangeTests.cs | 4 ++-- src/libraries/System.Linq/tests/SelectManyTests.cs | 2 +- src/libraries/System.Linq/tests/TakeTests.cs | 2 +- 9 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/libraries/Common/src/System/Collections/Generic/LargeArrayBuilder.SizeOpt.cs b/src/libraries/Common/src/System/Collections/Generic/LargeArrayBuilder.SizeOpt.cs index 4ec34a701d5232..181d2fd282afde 100644 --- a/src/libraries/Common/src/System/Collections/Generic/LargeArrayBuilder.SizeOpt.cs +++ b/src/libraries/Common/src/System/Collections/Generic/LargeArrayBuilder.SizeOpt.cs @@ -35,8 +35,6 @@ public void AddRange(IEnumerable items) } } - public void SlowAdd(T item) => _builder.Add(item); - public T[] ToArray() => _builder.ToArray(); public CopyPosition CopyTo(CopyPosition position, T[] array, int arrayIndex, int count) diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs index dfd4ba7b817626..9c0fa6a75d0cf1 100644 --- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs +++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs @@ -50,6 +50,9 @@ public static partial class PlatformDetection public static bool IsThreadingSupported => !IsBrowser; public static bool IsBinaryFormatterSupported => !IsBrowser; + public static bool IsSpeedOptimized => !IsBrowser; + public static bool IsSizeOptimized => !IsSpeedOptimized; + public static bool IsBrowserDomSupported => GetIsBrowserDomSupported(); public static bool IsNotBrowserDomSupported => !IsBrowserDomSupported; diff --git a/src/libraries/System.Linq/src/System.Linq.csproj b/src/libraries/System.Linq/src/System.Linq.csproj index ea585685f3e609..330555dc13138f 100644 --- a/src/libraries/System.Linq/src/System.Linq.csproj +++ b/src/libraries/System.Linq/src/System.Linq.csproj @@ -1,9 +1,17 @@ - $(NetCoreAppCurrent) + $(NetCoreAppCurrent);$(NetCoreAppCurrent)-Browser enable + true - + + + + + + + diff --git a/src/libraries/System.Linq/tests/ConcatTests.cs b/src/libraries/System.Linq/tests/ConcatTests.cs index 256fea4aaa14e5..6209d846ff2438 100644 --- a/src/libraries/System.Linq/tests/ConcatTests.cs +++ b/src/libraries/System.Linq/tests/ConcatTests.cs @@ -244,7 +244,7 @@ public static IEnumerable ManyConcatsData() yield return new object[] { Enumerable.Range(0, 500).Select(i => Enumerable.Repeat(i, 1)).Reverse() }; } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] public void CountOfConcatIteratorShouldThrowExceptionOnIntegerOverflow() { var supposedlyLargeCollection = new DelegateBasedCollection { CountWorker = () => int.MaxValue }; diff --git a/src/libraries/System.Linq/tests/EmptyPartitionTests.cs b/src/libraries/System.Linq/tests/EmptyPartitionTests.cs index 7417cbe957dec8..c98884ee067000 100644 --- a/src/libraries/System.Linq/tests/EmptyPartitionTests.cs +++ b/src/libraries/System.Linq/tests/EmptyPartitionTests.cs @@ -29,7 +29,7 @@ public void SingleInstance() Assert.True(ReferenceEquals(GetEmptyPartition(), GetEmptyPartition())); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] [SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, ".NET Core returns the instance as an optimization")] public void SkipSame() { @@ -37,7 +37,7 @@ public void SkipSame() Assert.Same(empty, empty.Skip(2)); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] [SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, ".NET Core returns the instance as an optimization")] public void TakeSame() { diff --git a/src/libraries/System.Linq/tests/OrderedSubsetting.cs b/src/libraries/System.Linq/tests/OrderedSubsetting.cs index 1e282147bb50a0..f4ec16f97cc87b 100644 --- a/src/libraries/System.Linq/tests/OrderedSubsetting.cs +++ b/src/libraries/System.Linq/tests/OrderedSubsetting.cs @@ -224,7 +224,7 @@ public void TakeAndSkip() Assert.Equal(Enumerable.Range(10, 1), ordered.Take(11).Skip(10)); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] [SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, "This fails with an OOM, as it iterates through the large array. See https://github.com/dotnet/corefx/pull/6821.")] public void TakeAndSkip_DoesntIterateRangeUnlessNecessary() { diff --git a/src/libraries/System.Linq/tests/RangeTests.cs b/src/libraries/System.Linq/tests/RangeTests.cs index 83527a918af3d9..68894cf8052405 100644 --- a/src/libraries/System.Linq/tests/RangeTests.cs +++ b/src/libraries/System.Linq/tests/RangeTests.cs @@ -211,14 +211,14 @@ public void FirstOrDefault() Assert.Equal(-100, Enumerable.Range(-100, int.MaxValue).FirstOrDefault()); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] [SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, ".NET Core optimizes Enumerable.Range().Last(). Without this optimization, this test takes a long time. See https://github.com/dotnet/corefx/pull/2401.")] public void Last() { Assert.Equal(1000000056, Enumerable.Range(57, 1000000000).Last()); } - [Fact] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] [SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, ".NET Core optimizes Enumerable.Range().LastOrDefault(). Without this optimization, this test takes a long time. See https://github.com/dotnet/corefx/pull/2401.")] public void LastOrDefault() { diff --git a/src/libraries/System.Linq/tests/SelectManyTests.cs b/src/libraries/System.Linq/tests/SelectManyTests.cs index 45c5a7da236608..50d97f07bb291f 100644 --- a/src/libraries/System.Linq/tests/SelectManyTests.cs +++ b/src/libraries/System.Linq/tests/SelectManyTests.cs @@ -464,7 +464,7 @@ public static IEnumerable DisposeAfterEnumerationData() return lengths.SelectMany(l => lengths, (l1, l2) => new object[] { l1, l2 }); } - [Theory] + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] [SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, ".NET Core optimizes SelectMany and throws an OverflowException. On the .NET Framework this takes a long time. See https://github.com/dotnet/corefx/pull/13942.")] [InlineData(new[] { int.MaxValue, 1 })] [InlineData(new[] { 2, int.MaxValue - 1 })] diff --git a/src/libraries/System.Linq/tests/TakeTests.cs b/src/libraries/System.Linq/tests/TakeTests.cs index a9580fb22d3c34..239ec1f9ed716c 100644 --- a/src/libraries/System.Linq/tests/TakeTests.cs +++ b/src/libraries/System.Linq/tests/TakeTests.cs @@ -459,7 +459,7 @@ public void RepeatEnumeratingNotList() Assert.Equal(taken, taken); } - [Theory] + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] [InlineData(1000)] [InlineData(1000000)] [InlineData(int.MaxValue)] From e02c8846ac40ed0af972a7343e29ca386c866e3c Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 8 Feb 2021 21:05:48 -0500 Subject: [PATCH 2/2] Opt more targets into size optimizations, per PR feedback --- .../Common/tests/TestUtilities/System/PlatformDetection.cs | 4 ++-- src/libraries/System.Linq/src/System.Linq.csproj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs index 9c0fa6a75d0cf1..f56404ff270243 100644 --- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs +++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs @@ -50,8 +50,8 @@ public static partial class PlatformDetection public static bool IsThreadingSupported => !IsBrowser; public static bool IsBinaryFormatterSupported => !IsBrowser; - public static bool IsSpeedOptimized => !IsBrowser; - public static bool IsSizeOptimized => !IsSpeedOptimized; + public static bool IsSpeedOptimized => !IsSizeOptimized; + public static bool IsSizeOptimized => IsBrowser || IsAndroid || IsiOS || IstvOS; public static bool IsBrowserDomSupported => GetIsBrowserDomSupported(); public static bool IsNotBrowserDomSupported => !IsBrowserDomSupported; diff --git a/src/libraries/System.Linq/src/System.Linq.csproj b/src/libraries/System.Linq/src/System.Linq.csproj index 330555dc13138f..2f814694d91f56 100644 --- a/src/libraries/System.Linq/src/System.Linq.csproj +++ b/src/libraries/System.Linq/src/System.Linq.csproj @@ -1,8 +1,8 @@ - $(NetCoreAppCurrent);$(NetCoreAppCurrent)-Browser + $(NetCoreAppCurrent);$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-Android;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS enable - true + true