Skip to content

Commit d682f53

Browse files
committed
Add some optimizations
1 parent bbfb46f commit d682f53

File tree

8 files changed

+48
-23
lines changed

8 files changed

+48
-23
lines changed

src/NHibernate/Async/Cache/StandardQueryCache.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Linq;
1515
using NHibernate.Cfg;
1616
using NHibernate.Engine;
17+
using NHibernate.Persister.Collection;
1718
using NHibernate.Type;
1819
using NHibernate.Util;
1920

@@ -308,13 +309,13 @@ private async Task<IList> GetResultFromCacheableAsync(
308309
}
309310
else
310311
{
311-
var collectionTypes = new Dictionary<int, CollectionType>();
312+
var collectionIndexes = new Dictionary<int, ICollectionPersister>();
312313
var nonCollectionTypeIndexes = new List<int>();
313314
for (var i = 0; i < returnTypes.Length; i++)
314315
{
315316
if (returnTypes[i] is CollectionType collectionType)
316317
{
317-
collectionTypes.Add(i, collectionType);
318+
collectionIndexes.Add(i, session.Factory.GetCollectionPersister(collectionType.Role));
318319
}
319320
else
320321
{
@@ -336,9 +337,12 @@ private async Task<IList> GetResultFromCacheableAsync(
336337
// Initialization of the fetched collection must be done at the end in order to be able to batch fetch them
337338
// from the cache or database. The collections were already created in the previous for statement so we only
338339
// have to initialize them.
339-
for (var i = 1; i < cacheable.Count; i++)
340+
if (collectionIndexes.Count > 0)
340341
{
341-
await (TypeHelper.InitializeCollectionsAsync((object[]) cacheable[i], (object[]) result[i - 1], collectionTypes, session, cancellationToken)).ConfigureAwait(false);
342+
for (var i = 1; i < cacheable.Count; i++)
343+
{
344+
await (TypeHelper.InitializeCollectionsAsync((object[]) cacheable[i], (object[]) result[i - 1], collectionIndexes, session, cancellationToken)).ConfigureAwait(false);
345+
}
342346
}
343347
}
344348

src/NHibernate/Async/Type/TypeHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using NHibernate.Collection;
1515
using NHibernate.Engine;
1616
using NHibernate.Intercept;
17+
using NHibernate.Persister.Collection;
1718
using NHibernate.Properties;
1819
using NHibernate.Tuple;
1920

@@ -106,26 +107,25 @@ internal static async Task<object[]> AssembleAsync(
106107
/// </summary>
107108
/// <param name="cacheRow">The cached values.</param>
108109
/// <param name="assembleRow">The assembled values to update.</param>
109-
/// <param name="types">The dictionary containing collection types and their indexes in the <paramref name="cacheRow"/> parameter as key.</param>
110+
/// <param name="collectionIndexes">The dictionary containing collection persisters and their indexes in the <paramref name="cacheRow"/> parameter as key.</param>
110111
/// <param name="session">The originating session.</param>
111112
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
112113
internal static async Task InitializeCollectionsAsync(
113114
object[] cacheRow,
114115
object[] assembleRow,
115-
IDictionary<int, CollectionType> types,
116+
IDictionary<int, ICollectionPersister> collectionIndexes,
116117
ISessionImplementor session, CancellationToken cancellationToken)
117118
{
118119
cancellationToken.ThrowIfCancellationRequested();
119-
foreach (var pair in types)
120+
foreach (var pair in collectionIndexes)
120121
{
121122
var value = cacheRow[pair.Key];
122123
if (value == null)
123124
{
124125
continue;
125126
}
126127

127-
var persister = session.Factory.GetCollectionPersister(pair.Value.Role);
128-
var collection = session.PersistenceContext.GetCollection(new CollectionKey(persister, value));
128+
var collection = session.PersistenceContext.GetCollection(new CollectionKey(pair.Value, value));
129129
await (collection.ForceInitializationAsync(cancellationToken)).ConfigureAwait(false);
130130
assembleRow[pair.Key] = collection;
131131
}

src/NHibernate/Cache/QueryCacheResultBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ internal QueryCacheResultBuilder(Loader.Loader loader)
5353
_collectionFetchIndexes.Add(i);
5454
}
5555
}
56+
57+
_hasFetches = _hasFetches || _collectionFetchIndexes.Count > 0;
5658
}
5759

5860
internal IList Result { get; } = new List<object>();

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using NHibernate.Cfg;
66
using NHibernate.Engine;
7+
using NHibernate.Persister.Collection;
78
using NHibernate.Type;
89
using NHibernate.Util;
910

@@ -332,13 +333,13 @@ private IList GetResultFromCacheable(
332333
}
333334
else
334335
{
335-
var collectionTypes = new Dictionary<int, CollectionType>();
336+
var collectionIndexes = new Dictionary<int, ICollectionPersister>();
336337
var nonCollectionTypeIndexes = new List<int>();
337338
for (var i = 0; i < returnTypes.Length; i++)
338339
{
339340
if (returnTypes[i] is CollectionType collectionType)
340341
{
341-
collectionTypes.Add(i, collectionType);
342+
collectionIndexes.Add(i, session.Factory.GetCollectionPersister(collectionType.Role));
342343
}
343344
else
344345
{
@@ -360,9 +361,12 @@ private IList GetResultFromCacheable(
360361
// Initialization of the fetched collection must be done at the end in order to be able to batch fetch them
361362
// from the cache or database. The collections were already created in the previous for statement so we only
362363
// have to initialize them.
363-
for (var i = 1; i < cacheable.Count; i++)
364+
if (collectionIndexes.Count > 0)
364365
{
365-
TypeHelper.InitializeCollections((object[]) cacheable[i], (object[]) result[i - 1], collectionTypes, session);
366+
for (var i = 1; i < cacheable.Count; i++)
367+
{
368+
TypeHelper.InitializeCollections((object[]) cacheable[i], (object[]) result[i - 1], collectionIndexes, session);
369+
}
366370
}
367371
}
368372

src/NHibernate/Multi/ICachingInformation.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ public interface ICachingInformation
7777

7878
internal static class CachingInformationExtensions
7979
{
80-
// 6.0 TODO: Move to ICachingInformation as a Property
80+
// 6.0 TODO: Remove and use CacheTypes instead.
8181
public static IType[] GetCacheTypes(this ICachingInformation cachingInformation)
8282
{
83-
var loaderProperty = cachingInformation.GetType().GetProperty("Loader");
84-
if (loaderProperty?.GetValue(cachingInformation) is Loader.Loader loader)
83+
if (cachingInformation is ICachingInformationWithFetches cachingInformationWithFetches)
8584
{
86-
return loader.CacheTypes;
85+
return cachingInformationWithFetches.CacheTypes;
8786
}
8887

8988
#pragma warning disable 618
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using NHibernate.Type;
2+
3+
namespace NHibernate.Multi
4+
{
5+
// 6.0 TODO: merge into 'ICachingInformation'.
6+
internal interface ICachingInformationWithFetches
7+
{
8+
/// <summary>
9+
/// The query cache types.
10+
/// </summary>
11+
IType[] CacheTypes { get; }
12+
}
13+
}

src/NHibernate/Multi/QueryBatchItemBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract partial class QueryBatchItemBase<TResult> : IQueryBatchItem<TRes
2222
private CacheMode? _cacheMode;
2323
private IList<TResult> _finalResults;
2424

25-
protected class QueryInfo : ICachingInformation
25+
protected class QueryInfo : ICachingInformation, ICachingInformationWithFetches
2626
{
2727
/// <summary>
2828
/// The query loader.
@@ -56,6 +56,9 @@ protected class QueryInfo : ICachingInformation
5656
/// <inheritdoc />
5757
public IType[] ResultTypes => Loader.ResultTypes;
5858

59+
/// <inheritdoc />
60+
public IType[] CacheTypes => Loader.CacheTypes;
61+
5962
/// <inheritdoc />
6063
public string QueryIdentifier => Loader.QueryIdentifier;
6164

src/NHibernate/Type/TypeHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NHibernate.Collection;
55
using NHibernate.Engine;
66
using NHibernate.Intercept;
7+
using NHibernate.Persister.Collection;
78
using NHibernate.Properties;
89
using NHibernate.Tuple;
910

@@ -116,24 +117,23 @@ internal static object[] Assemble(
116117
/// </summary>
117118
/// <param name="cacheRow">The cached values.</param>
118119
/// <param name="assembleRow">The assembled values to update.</param>
119-
/// <param name="types">The dictionary containing collection types and their indexes in the <paramref name="cacheRow"/> parameter as key.</param>
120+
/// <param name="collectionIndexes">The dictionary containing collection persisters and their indexes in the <paramref name="cacheRow"/> parameter as key.</param>
120121
/// <param name="session">The originating session.</param>
121122
internal static void InitializeCollections(
122123
object[] cacheRow,
123124
object[] assembleRow,
124-
IDictionary<int, CollectionType> types,
125+
IDictionary<int, ICollectionPersister> collectionIndexes,
125126
ISessionImplementor session)
126127
{
127-
foreach (var pair in types)
128+
foreach (var pair in collectionIndexes)
128129
{
129130
var value = cacheRow[pair.Key];
130131
if (value == null)
131132
{
132133
continue;
133134
}
134135

135-
var persister = session.Factory.GetCollectionPersister(pair.Value.Role);
136-
var collection = session.PersistenceContext.GetCollection(new CollectionKey(persister, value));
136+
var collection = session.PersistenceContext.GetCollection(new CollectionKey(pair.Value, value));
137137
collection.ForceInitialization();
138138
assembleRow[pair.Key] = collection;
139139
}

0 commit comments

Comments
 (0)