Skip to content

Commit 108e07b

Browse files
hazzikmaca88
authored andcommitted
Reduce allocations for empty enumerators
1 parent 8fae98c commit 108e07b

File tree

2 files changed

+15
-28
lines changed

2 files changed

+15
-28
lines changed

src/NHibernate/Engine/Query/HQLQueryPlan.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public IAsyncEnumerable<T> PerformAsyncIterate<T>(QueryParameters queryParameter
247247

248248
if (Translators.Length == 0)
249249
{
250-
return new CollectionHelper.EmptyAsyncEnumerableClass<T>();
250+
return new CollectionHelper.EmptyAsyncEnumerable<T>();
251251
}
252252

253253
if (Translators.Length == 1)

src/NHibernate/Util/CollectionHelper.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,6 @@ public bool MoveNext()
4343
}
4444
}
4545

46-
[Serializable]
47-
private class EmtpyAsyncEnumerator<T> : IAsyncEnumerator<T>
48-
{
49-
public T Current => throw new InvalidOperationException("EmtpyAsyncEnumerator.get_Current");
50-
51-
public ValueTask DisposeAsync()
52-
{
53-
return default;
54-
}
55-
56-
public ValueTask<bool> MoveNextAsync()
57-
{
58-
return new ValueTask<bool>(false);
59-
}
60-
}
61-
6246
private class EmptyEnumerableClass : IEnumerable
6347
{
6448
public IEnumerator GetEnumerator()
@@ -67,14 +51,6 @@ public IEnumerator GetEnumerator()
6751
}
6852
}
6953

70-
private class EmptyAsyncEnumerableClass : IAsyncEnumerable<object>
71-
{
72-
public IAsyncEnumerator<object> GetAsyncEnumerator(CancellationToken cancellationToken = default)
73-
{
74-
return new EmtpyAsyncEnumerator<object>();
75-
}
76-
}
77-
7854
/// <summary>
7955
/// A read-only dictionary that is always empty and permits lookup by <see langword="null" /> key.
8056
/// </summary>
@@ -240,7 +216,6 @@ public IEnumerator GetEnumerator()
240216
}
241217

242218
public static readonly IEnumerable EmptyEnumerable = new EmptyEnumerableClass();
243-
public static readonly IAsyncEnumerable<object> EmptyAsyncEnumerable = new EmptyAsyncEnumerableClass();
244219
public static readonly IDictionary EmptyMap = new EmptyMapClass();
245220

246221
public static IDictionary<TKey, TValue> EmptyDictionary<TKey, TValue>()
@@ -454,11 +429,23 @@ public IEnumerator GetEnumerator()
454429
}
455430

456431
[Serializable]
457-
internal class EmptyAsyncEnumerableClass<T> : IAsyncEnumerable<T>
432+
internal class EmptyAsyncEnumerable<T> : IAsyncEnumerable<T>, IAsyncEnumerator<T>
458433
{
434+
public T Current => throw new InvalidOperationException("EmtpyAsyncEnumerator.get_Current");
435+
436+
public ValueTask DisposeAsync()
437+
{
438+
return default;
439+
}
440+
441+
public ValueTask<bool> MoveNextAsync()
442+
{
443+
return new ValueTask<bool>(false);
444+
}
445+
459446
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
460447
{
461-
return new EmtpyAsyncEnumerator<T>();
448+
return this;
462449
}
463450
}
464451

0 commit comments

Comments
 (0)