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

Enable size-optimized LINQ build for Browser #47918

Merged
merged 2 commits into from
Feb 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public void AddRange(IEnumerable<T> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public static partial class PlatformDetection
public static bool IsThreadingSupported => !IsBrowser;
public static bool IsBinaryFormatterSupported => !IsBrowser;

public static bool IsSpeedOptimized => !IsSizeOptimized;
public static bool IsSizeOptimized => IsBrowser || IsAndroid || IsiOS || IstvOS;

public static bool IsBrowserDomSupported => GetIsBrowserDomSupported();
public static bool IsNotBrowserDomSupported => !IsBrowserDomSupported;

Expand Down
12 changes: 10 additions & 2 deletions src/libraries/System.Linq/src/System.Linq.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-Android;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS</TargetFrameworks>
<Nullable>enable</Nullable>
<OptimizeForSize Condition="'$(TargetsBrowser)' == 'true' or '$(TargetsAndroid)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">true</OptimizeForSize>
</PropertyGroup>
<ItemGroup> <!-- Optimize for speed -->
<ItemGroup Condition="'$(OptimizeForSize)' == true">
<Compile Include="System\Linq\Enumerable.SizeOpt.cs" />
<Compile Include="System\Linq\Skip.SizeOpt.cs" />
<Compile Include="System\Linq\Take.SizeOpt.cs" />
<Compile Include="$(CommonPath)\System\Collections\Generic\LargeArrayBuilder.SizeOpt.cs"
Link="System\Collections\Generic\LargeArrayBuilder.SizeOpt.cs" />
</ItemGroup>
<ItemGroup Condition="'$(OptimizeForSize)' != true">
<Compile Include="System\Linq\AppendPrepend.SpeedOpt.cs" />
<Compile Include="System\Linq\Concat.SpeedOpt.cs" />
<Compile Include="System\Linq\DefaultIfEmpty.SpeedOpt.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Linq/tests/ConcatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static IEnumerable<object[]> 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<int> { CountWorker = () => int.MaxValue };
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Linq/tests/EmptyPartitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public void SingleInstance()
Assert.True(ReferenceEquals(GetEmptyPartition<int>(), GetEmptyPartition<int>()));
}

[Fact]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))]
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
[SkipOnTargetFramework(~TargetFrameworkMonikers.Netcoreapp, ".NET Core returns the instance as an optimization")]
public void SkipSame()
{
IEnumerable<int> empty = GetEmptyPartition<int>();
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()
{
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Linq/tests/OrderedSubsetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Linq/tests/RangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Linq/tests/SelectManyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public static IEnumerable<object[]> 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 })]
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Linq/tests/TakeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down