Skip to content

Commit 82dce6f

Browse files
authored
Revert "Revert "Optimize BuildMaterializer()"" (#218)
This reverts commit 442e8ef.
1 parent 7ae39e0 commit 82dce6f

File tree

5 files changed

+41
-51
lines changed

5 files changed

+41
-51
lines changed

Orm/Xtensive.Orm/Orm/Internals/TypeMapping.cs

+8-20
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,12 @@
88
using Xtensive.Tuples.Transform;
99
using Xtensive.Orm.Model;
1010

11-
namespace Xtensive.Orm.Internals
12-
{
13-
internal readonly struct TypeMapping
14-
{
15-
public readonly TypeInfo Type;
16-
public readonly MapTransform KeyTransform;
17-
public readonly IReadOnlyList<ColNum> KeyIndexes;
18-
public readonly MapTransform Transform;
11+
namespace Xtensive.Orm.Internals;
1912

20-
21-
// Constructors
22-
23-
public TypeMapping(TypeInfo type, MapTransform keyTransform, MapTransform transform, IReadOnlyList<ColNum> keyIndexes)
24-
{
25-
Type = type;
26-
KeyTransform = keyTransform;
27-
Transform = transform;
28-
KeyIndexes = keyIndexes;
29-
}
30-
}
31-
}
13+
internal readonly record struct TypeMapping
14+
(
15+
TypeInfo Type,
16+
MapTransform KeyTransform,
17+
MapTransform Transform,
18+
IReadOnlyList<ColNum> KeyIndexes
19+
);

Orm/Xtensive.Orm/Orm/Linq/Materialization/MaterializationContext.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,11 @@ public TypeIdRegistry TypeIdRegistry
6060

6161
public TypeMapping GetTypeMapping(int entityIndex, TypeInfo approximateType, int typeId, IReadOnlyList<Pair<ColNum>> columns)
6262
{
63-
TypeMapping result;
6463
ref var cache = ref entityMappings[entityIndex];
65-
if (cache.SingleItem != null) {
66-
if (typeId != ResolveTypeToNodeSpecificTypeIdentifier(cache.SingleItem?.Type)) {
67-
throw new ArgumentOutOfRangeException("typeId");
68-
}
69-
return cache.SingleItem.Value;
64+
if (cache.SingleItem is TypeMapping result) {
65+
return typeId == ResolveTypeToNodeSpecificTypeIdentifier(result.Type)
66+
? result
67+
: throw new ArgumentOutOfRangeException("typeId");
7068
}
7169
if (cache.Items.TryGetValue(typeId, out result))
7270
return result;
@@ -90,8 +88,8 @@ public TypeMapping GetTypeMapping(int entityIndex, TypeInfo approximateType, int
9088
typeColumnMap = newColumns;
9189
}
9290

93-
ArraySegment<ColNum> allIndexes = MaterializationHelper.CreateSingleSourceMap(descriptor.Count, typeColumnMap);
94-
ArraySegment<ColNum> keyIndexes = allIndexes.Slice(0, keyInfo.TupleDescriptor.Count);
91+
var allIndexes = MaterializationHelper.CreateSingleSourceMap(descriptor.Count, typeColumnMap);
92+
var keyIndexes = allIndexes.Take(keyInfo.TupleDescriptor.Count).ToArray();
9593

9694
var transform = new MapTransform(true, descriptor, allIndexes);
9795
var keyTransform = new MapTransform(true, keyInfo.TupleDescriptor, keyIndexes);

Orm/Xtensive.Orm/Orm/Linq/Materialization/MaterializationInfo.cs

+2-13
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@
66

77
using System.Linq.Expressions;
88

9-
namespace Xtensive.Orm.Linq.Materialization
10-
{
11-
internal sealed class MaterializationInfo
12-
{
13-
public int EntitiesInRow { get; private set; }
14-
public LambdaExpression Expression { get; private set; }
9+
namespace Xtensive.Orm.Linq.Materialization;
1510

16-
public MaterializationInfo(int entitiesInRow, LambdaExpression expression)
17-
{
18-
EntitiesInRow = entitiesInRow;
19-
Expression = expression;
20-
}
21-
}
22-
}
11+
internal readonly record struct MaterializationInfo(int EntitiesInRow, LambdaExpression Expression);

Orm/Xtensive.Orm/Orm/Linq/Translator.Materialization.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,19 @@ private Materializer BuildMaterializer(ProjectionExpression projection, IReadOnl
127127
var materializationInfo = itemProjector.Materialize(context, tupleParameters);
128128
var elementType = itemProjector.Item.Type;
129129
var materializeMethod = MaterializationHelper.MaterializeMethodInfo.CachedMakeGenericMethod(elementType);
130-
var itemMaterializerFactoryMethod =
131-
elementType.IsNullable()
132-
? MaterializationHelper.CreateNullableItemMaterializerMethodInfo.CachedMakeGenericMethod(
133-
elementType.GetGenericArguments()[0])
130+
131+
#if NET8_0_OR_GREATER
132+
var itemMaterializerFactoryMethod = elementType.IsNullable()
133+
? MaterializationHelper.CreateNullableItemMaterializerMethodInfo.CachedMakeGenericMethodInvoker(elementType.GetGenericArguments()[0])
134+
: MaterializationHelper.CreateItemMaterializerMethodInfo.CachedMakeGenericMethodInvoker(elementType);
135+
var itemMaterializer = itemMaterializerFactoryMethod.Invoke(null, materializationInfo.Expression, itemProjector.AggregateType);
136+
#else
137+
var itemMaterializerFactoryMethod = elementType.IsNullable()
138+
? MaterializationHelper.CreateNullableItemMaterializerMethodInfo.CachedMakeGenericMethod(elementType.GetGenericArguments()[0])
134139
: MaterializationHelper.CreateItemMaterializerMethodInfo.CachedMakeGenericMethod(elementType);
140+
var itemMaterializer = itemMaterializerFactoryMethod.Invoke(null, [materializationInfo.Expression, itemProjector.AggregateType]);
141+
#endif
135142

136-
var itemMaterializer = itemMaterializerFactoryMethod.Invoke(
137-
null, new object[] { materializationInfo.Expression, itemProjector.AggregateType });
138143
Expression<Func<Session, int, MaterializationContext>> materializationContextCtor =
139144
(s, entityCount) => new MaterializationContext(s, entityCount);
140145
var materializationContextExpression = materializationContextCtor

Orm/Xtensive.Orm/Orm/Linq/TranslatorContext.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,23 @@ public IReadOnlyList<string> GetMainQueryTags() =>
8282
? applyParameters.Keys.OfType<TagProvider>().Select(p => p.Tag).ToList()
8383
: Array.Empty<string>();
8484

85-
public IDisposable DisableSessionTags()
85+
internal readonly struct TagsRestorer : IDisposable
8686
{
87-
var originalTags = SessionTags;
88-
SessionTags = null;
89-
return new Disposable((b) => SessionTags = originalTags);
87+
private readonly TranslatorContext context;
88+
private readonly IReadOnlyList<string> originalTags;
89+
90+
public void Dispose() => context.SessionTags = originalTags;
91+
92+
internal TagsRestorer(TranslatorContext context)
93+
{
94+
this.context = context;
95+
originalTags = context.SessionTags;
96+
context.SessionTags = null;
97+
}
9098
}
9199

100+
public TagsRestorer DisableSessionTags() => new(this);
101+
92102
public void RebindApplyParameter(CompilableProvider old, CompilableProvider @new)
93103
{
94104
if (applyParameters.TryGetValue(old, out var parameter)) {

0 commit comments

Comments
 (0)