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
[API Proposal]: Additional ToFrozenDictionary overloads with optimizeForReading #87633
Comments
Tagging subscribers to this area: @dotnet/area-system-collections Issue DetailsBackground and motivationWhen experimenting with using This matches the other overload of API Proposalpublic static partial class FrozenDictionary
{
public static FrozenDictionary<TKey, TValue> ToFrozenDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey>? comparer = null) where TKey : notnull;
public static FrozenDictionary<TKey, TValue> ToFrozenDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey>? comparer, bool optimizeForReading) where TKey : notnull;
public static FrozenDictionary<TKey, TSource> ToFrozenDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull;
+ public static FrozenDictionary<TKey, TSource> ToFrozenDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, bool optimizeForReading) where TKey : notnull;
public static FrozenDictionary<TKey, TElement> ToFrozenDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer = null) where TKey : notnull;
+ public static FrozenDictionary<TKey, TElement> ToFrozenDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey>? comparer, bool optimizeForReading) where TKey : notnull;
} API Usageentries.ToFrozenDictionary(x => x.text, x => x.destination, StringComparer.OrdinalIgnoreCase, true); Alternative DesignsNo response RisksMore API surface.
|
While I'm the one who pushed for us to add optimizeForReading, I'd still really like to see if we can do away with it before we actually ship. It's there because a) the creation costs are so massive when compared to dictionary that anyone choosing to use the type for its surface area rather than many, many reads over the lifetime of the app will find themselves regressing perf, and b) for native aot carrying around all the different specializations for each TKey/TValue leads to asm bloat. However, @adamsitnik is currently whittling down the costs; if we got them down sufficiently, it'd be nice to not force extra work on behalf of devs to achieve the primary reason these types were initially created. That said, if we don't get there and we need to ship with the flag still in place, adding the additional overloads is reasonable. |
Background and motivation
When experimenting with using
FrozenDictionary
in dotnet/aspnetcore#47569, we had to writeentries.ToDictionary(x => x.text, x => x.destination).ToFrozenDictionary(StringComparer.OrdinalIgnoreCase, true);
to get an optimized
FrozenDictionary
when ideally it would have beenentries.ToFrozenDictionary(x => x.text, x => x.destination, StringComparer.OrdinalIgnoreCase, true);
This matches the other overload of
ToFrozenDictionary
that takes anIEnumerable<KeyValuePair<TKey, TValue>>
.API Proposal
API Usage
Alternative Designs
No response
Risks
More API surface.
The text was updated successfully, but these errors were encountered: