Skip to content

Commit

Permalink
Add identity method to reduce JIT-ing
Browse files Browse the repository at this point in the history
This is a squashed merge of PR ##880.

Co-authored-by: Atif Aziz <[email protected]>
  • Loading branch information
viceroypenguin and atifaziz authored Nov 30, 2022
1 parent 34ce84a commit 4e09ca4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion MoreLinq/Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static partial class MoreEnumerable

public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(this IEnumerable<TSource> source, int size)
{
return Batch(source, size, x => x);
return Batch(source, size, IdFn);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions MoreLinq/GroupAdjacent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static IEnumerable<IGrouping<TKey, TSource>> GroupAdjacent<TSource, TKey>
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));

return GroupAdjacent(source, keySelector, e => e, comparer);
return GroupAdjacent(source, keySelector, IdFn, comparer);
}

/// <summary>
Expand Down Expand Up @@ -208,7 +208,7 @@ public static IEnumerable<TResult> GroupAdjacent<TSource, TKey, TResult>(
// This should be removed once the target framework is bumped to something that supports covariance
TResult ResultSelectorWrapper(TKey key, IList<TSource> group) => resultSelector(key, group);

return GroupAdjacentImpl(source, keySelector, i => i, ResultSelectorWrapper,
return GroupAdjacentImpl(source, keySelector, IdFn, ResultSelectorWrapper,
EqualityComparer<TKey>.Default);
}

Expand Down Expand Up @@ -253,7 +253,7 @@ public static IEnumerable<TResult> GroupAdjacent<TSource, TKey, TResult>(

// This should be removed once the target framework is bumped to something that supports covariance
TResult ResultSelectorWrapper(TKey key, IList<TSource> group) => resultSelector(key, group);
return GroupAdjacentImpl(source, keySelector, i => i, ResultSelectorWrapper,
return GroupAdjacentImpl(source, keySelector, IdFn, ResultSelectorWrapper,
comparer ?? EqualityComparer<TKey>.Default);
}

Expand Down
24 changes: 24 additions & 0 deletions MoreLinq/IdFn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2022 Turning Code, LLC. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace MoreLinq
{
static partial class MoreEnumerable
{
static T IdFn<T>(T x) => x;
}
}
4 changes: 2 additions & 2 deletions MoreLinq/OrderedMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static IEnumerable<T> OrderedMerge<T>(
IEnumerable<T> second,
IComparer<T>? comparer)
{
return OrderedMerge(first, second, e => e, f => f, s => s, (a, _) => a, comparer);
return OrderedMerge(first, second, IdFn, IdFn, IdFn, (a, _) => a, comparer);
}

/// <summary>
Expand Down Expand Up @@ -98,7 +98,7 @@ public static IEnumerable<T> OrderedMerge<T, TKey>(
IEnumerable<T> second,
Func<T, TKey> keySelector)
{
return OrderedMerge(first, second, keySelector, a => a, b => b, (a, _) => a, null);
return OrderedMerge(first, second, keySelector, IdFn, IdFn, (a, _) => a, null);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions MoreLinq/Rank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static partial class MoreEnumerable

public static IEnumerable<int> Rank<TSource>(this IEnumerable<TSource> source)
{
return source.RankBy(x => x);
return source.RankBy(IdFn);
}

/// <summary>
Expand All @@ -45,7 +45,7 @@ public static IEnumerable<int> Rank<TSource>(this IEnumerable<TSource> source)

public static IEnumerable<int> Rank<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
{
return source.RankBy(x => x, comparer);
return source.RankBy(IdFn, comparer);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions MoreLinq/Split.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static IEnumerable<IEnumerable<TSource>> Split<TSource>(this IEnumerable<
public static IEnumerable<IEnumerable<TSource>> Split<TSource>(this IEnumerable<TSource> source,
TSource separator, int count)
{
return Split(source, separator, count, s => s);
return Split(source, separator, count, IdFn);
}

/// <summary>
Expand Down Expand Up @@ -129,7 +129,7 @@ public static IEnumerable<IEnumerable<TSource>> Split<TSource>(this IEnumerable<
public static IEnumerable<IEnumerable<TSource>> Split<TSource>(this IEnumerable<TSource> source,
TSource separator, IEqualityComparer<TSource>? comparer, int count)
{
return Split(source, separator, comparer, count, s => s);
return Split(source, separator, comparer, count, IdFn);
}

/// <summary>
Expand Down Expand Up @@ -216,7 +216,7 @@ public static IEnumerable<IEnumerable<TSource>> Split<TSource>(this IEnumerable<
public static IEnumerable<IEnumerable<TSource>> Split<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> separatorFunc, int count)
{
return Split(source, separatorFunc, count, s => s);
return Split(source, separatorFunc, count, IdFn);
}

/// <summary>
Expand Down

0 comments on commit 4e09ca4

Please sign in to comment.