-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Don't use a GVM in Linq Select with NAOT by default #109978
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace System.Linq | ||
{ | ||
/// <summary> | ||
/// Defines options for how iterators behave. | ||
/// </summary> | ||
internal static class IteratorOptions | ||
{ | ||
[FeatureSwitchDefinition("System.Linq.ValueTypeTrimFriendlySelect")] | ||
public static bool ValueTypeTrimFriendlySelect { get; } = AppContext.TryGetSwitch("System.Linq.ValueTypeTrimFriendlySelect", out bool isEnabled) ? isEnabled : true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||||||||||
|
@@ -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) | ||||||||||||||||||||||
|
@@ -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) | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
// In Native AOT, GVMs with Value types are not trim friendly. | ||||||||||||||||||||||
// If the option is enabled, we don't call the GVM for value types. We don't do the | ||||||||||||||||||||||
// same for reference types because reference types are trim friendly with GVMs | ||||||||||||||||||||||
// and it is preferable to call the GVM as it allows the select implementation to be | ||||||||||||||||||||||
// specialized. | ||||||||||||||||||||||
Comment on lines
+58
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
if (/*IteratorOptions.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) | ||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ Copyright (c) .NET Foundation. All rights reserved. | |
<EnableUnsafeBinaryFormatterSerialization Condition="'$(EnableUnsafeBinaryFormatterSerialization)' == ''">false</EnableUnsafeBinaryFormatterSerialization> | ||
<EnableUnsafeUTF7Encoding Condition="'$(EnableUnsafeUTF7Encoding)' == ''">false</EnableUnsafeUTF7Encoding> | ||
<BuiltInComInteropSupport Condition="'$(BuiltInComInteropSupport)' == ''">false</BuiltInComInteropSupport> | ||
<ValueTypeTrimFriendlySelect Condition="'$(ValueTypeTrimFriendlySelect)' == ''">true</ValueTypeTrimFriendlySelect> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have any measurements how this affects |
||
<!-- GeneratedComInterface/ComImport interop requires built-in COM --> | ||
<EnableGeneratedComInterfaceComImportInterop Condition="'$(BuiltInComInteropSupport)' == 'false'">false</EnableGeneratedComInterfaceComImportInterop> | ||
<EnableGeneratedComInterfaceComImportInterop Condition="'$(EnableGeneratedComInterfaceComImportInterop)' == ''">false</EnableGeneratedComInterfaceComImportInterop> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to pick feature switch names that could match the name of a public property (if we ever decided to make it public) - so maybe put this in the
Enumerable
class?