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

Add a feature flag to not use GVM in Linq Select #109978

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/libraries/System.Linq/src/System/Linq/Enumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -63,5 +64,8 @@ internal static bool TryGetSpan<TSource>(this IEnumerable<TSource> source, out R

return result;
}

[FeatureSwitchDefinition("System.Linq.Enumerable.ValueTypeTrimFriendlySelect")]
internal static bool ValueTypeTrimFriendlySelect { get; } = AppContext.TryGetSwitch("System.Linq.Enumerable.ValueTypeTrimFriendlySelect", out bool isEnabled) ? isEnabled : true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> s
new IEnumerableWhereSelectIterator<object, TResult2>(objectSource, isTResult, localSelector);
}

return base.Select(selector);
return new IteratorSelectIterator<TResult, TResult2>(this, selector);
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion src/libraries/System.Linq/src/System/Linq/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using static System.Linq.Utilities;

namespace System.Linq
Expand All @@ -25,7 +26,7 @@ public static IEnumerable<TResult> Select<TSource, TResult>(

if (source is Iterator<TSource> iterator)
{
return iterator.Select(selector);
return SelectImplementation(selector, iterator);
}

if (source is IList<TSource> ilist)
Expand All @@ -51,6 +52,26 @@ public static IEnumerable<TResult> Select<TSource, TResult>(
return new IEnumerableSelectIterator<TSource, TResult>(source, selector);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IEnumerable<TResult> SelectImplementation<TSource, TResult>(Func<TSource, TResult> selector, Iterator<TSource> iterator)
{
// With native AOT, calling into the `Select` generic virtual method results in NxM
// expansion of native code. If the option is enabled, we don't call the generic virtual
// for value types. We don't do the same for reference types because reference type
// expansion can happen lazily at runtime and the AOT compiler does postpone it (we
// don't need more code, just more data structures describing the new types).
if (ValueTypeTrimFriendlySelect && typeof(TResult).IsValueType)
{
#if OPTIMIZE_FOR_SIZE
return new IEnumerableSelectIterator<TSource, TResult>(iterator, selector);
#else
return new IteratorSelectIterator<TSource, TResult>(iterator, selector);
#endif
}

return iterator.Select(selector);
}

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
{
if (source is null)
Expand Down
32 changes: 26 additions & 6 deletions src/libraries/System.Linq/tests/SkipWhileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;

namespace System.Linq.Tests
Expand Down Expand Up @@ -62,14 +63,33 @@ public void RunOnce()
Assert.Equal(Enumerable.Range(10, 10), Enumerable.Range(0, 20).RunOnce().SkipWhile((i, idx) => idx < 10));
}

[Fact]
public void SkipErrorWhenSourceErrors()
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public static void SkipErrorWhenSourceErrors_TrimFriendlySelectTrue()
{
var source = NumberRangeGuaranteedNotCollectionType(-2, 5).Select(i => (decimal)i).Select(m => 1 / m).Skip(4);
using(var en = source.GetEnumerator())
RemoteExecutor.Invoke(() =>
{
Assert.Throws<DivideByZeroException>(() => en.MoveNext());
}
AppContext.SetSwitch("System.Linq.Enumerable.ValueTypeTrimFriendlySelect", true);

var source = NumberRangeGuaranteedNotCollectionType(-2, 5).Select(i => (decimal)i).Select(m => 1 / m).Skip(4);
var valuesFromEnumerable = source.ToList();
List<decimal> expectedValues = [(decimal)1/2];
Assert.Equal(expectedValues, valuesFromEnumerable);
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void SkipErrorWhenSourceErrors_TrimFriendlySelectFalse()
{
RemoteExecutor.Invoke(() =>
{
AppContext.SetSwitch("System.Linq.Enumerable.ValueTypeTrimFriendlySelect", false);

var source = NumberRangeGuaranteedNotCollectionType(-2, 5).Select(i => (decimal)i).Select(m => 1 / m).Skip(4);
using (var en = source.GetEnumerator())
{
Assert.Throws<DivideByZeroException>(() => en.MoveNext());
}
}).Dispose();
}

[Fact]
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Linq/tests/System.Linq.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<DebuggerSupport Condition="'$(DebuggerSupport)' == '' and '$(TargetOS)' == 'browser'">true</DebuggerSupport>
</PropertyGroup>
Expand Down
Loading