Skip to content

Commit 9da4184

Browse files
authored
Optimize QueryableExtensions: preallocate immutable Expression.Parameter (#402)
1 parent ecb797c commit 9da4184

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Orm/Xtensive.Orm/Orm/QueryableExtensions.Async.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ namespace Xtensive.Orm
1616
/// </summary>
1717
public static partial class QueryableExtensions
1818
{
19+
private static class ParameterTraits<TSource>
20+
{
21+
public static readonly ParameterExpression[] ItemParam = [Expression.Parameter(typeof(TSource), "item")];
22+
}
23+
1924
private static readonly object BoxedZero = 0;
2025

2126
/// <summary>
@@ -1399,7 +1404,7 @@ public static async Task<Dictionary<TKey, TSource>> ToDictionaryAsync<TKey, TSou
13991404
Expression<Func<TSource, TKey>> keySelector, CancellationToken cancellationToken = default)
14001405
{
14011406
var tupleFactoryMethod = TupleCreateMethod.CachedMakeGenericMethod(typeof(TKey), typeof(TSource));
1402-
var itemParam = new[] {Expression.Parameter(typeof(TSource), "item")};
1407+
var itemParam = ParameterTraits<TSource>.ItemParam;
14031408
var body = Expression.Call(null, tupleFactoryMethod,
14041409
ExpressionReplacer.ReplaceAll(keySelector.Body, keySelector.Parameters, itemParam),
14051410
itemParam[0]);
@@ -1441,7 +1446,7 @@ public static async Task<Dictionary<TKey, TValue>> ToDictionaryAsync<TKey, TValu
14411446
CancellationToken cancellationToken = default)
14421447
{
14431448
var tupleFactoryMethod = TupleCreateMethod.CachedMakeGenericMethod(typeof(TKey), typeof(TValue));
1444-
var itemParam = new[] {Expression.Parameter(typeof(TSource), "item")};
1449+
var itemParam = ParameterTraits<TSource>.ItemParam;
14451450
var body = Expression.Call(null, tupleFactoryMethod,
14461451
ExpressionReplacer.ReplaceAll(keySelector.Body, keySelector.Parameters, itemParam),
14471452
ExpressionReplacer.ReplaceAll(valueSelector.Body, valueSelector.Parameters, itemParam));
@@ -1506,7 +1511,7 @@ public static async Task<ILookup<TKey, TSource>> ToLookupAsync<TKey, TSource>(th
15061511
Expression<Func<TSource, TKey>> keySelector, CancellationToken cancellationToken = default)
15071512
{
15081513
var tupleFactoryMethod = TupleCreateMethod.CachedMakeGenericMethod(typeof(TKey), typeof(TSource));
1509-
var itemParam = new[] {Expression.Parameter(typeof(TSource), "item")};
1514+
var itemParam = ParameterTraits<TSource>.ItemParam;
15101515
var body = Expression.Call(null, tupleFactoryMethod,
15111516
ExpressionReplacer.ReplaceAll(keySelector.Body, keySelector.Parameters, itemParam),
15121517
itemParam[0]);
@@ -1543,7 +1548,7 @@ public static async Task<ILookup<TKey, TValue>> ToLookupAsync<TKey, TValue, TSou
15431548
CancellationToken cancellationToken = default)
15441549
{
15451550
var tupleFactoryMethod = TupleCreateMethod.CachedMakeGenericMethod(typeof(TKey), typeof(TValue));
1546-
var itemParam = new[] {Expression.Parameter(typeof(TSource), "item")};
1551+
var itemParam = ParameterTraits<TSource>.ItemParam;
15471552
var body = Expression.Call(null, tupleFactoryMethod,
15481553
ExpressionReplacer.ReplaceAll(keySelector.Body, keySelector.Parameters, itemParam),
15491554
ExpressionReplacer.ReplaceAll(valueSelector.Body, valueSelector.Parameters, itemParam));

0 commit comments

Comments
 (0)