Skip to content

Commit 442e8ef

Browse files
committed
Revert "Optimize BuildMaterializer()"
This reverts commit c0979b3.
1 parent c0979b3 commit 442e8ef

File tree

5 files changed

+51
-41
lines changed

5 files changed

+51
-41
lines changed

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

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

11-
namespace Xtensive.Orm.Internals;
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;
1219

13-
internal readonly record struct TypeMapping
14-
(
15-
TypeInfo Type,
16-
MapTransform KeyTransform,
17-
MapTransform Transform,
18-
IReadOnlyList<ColNum> KeyIndexes
19-
);
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+
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ public TypeIdRegistry TypeIdRegistry
6060

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

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

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

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
using System.Linq.Expressions;
88

9-
namespace Xtensive.Orm.Linq.Materialization;
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; }
1015

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

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,14 @@ 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-
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])
130+
var itemMaterializerFactoryMethod =
131+
elementType.IsNullable()
132+
? MaterializationHelper.CreateNullableItemMaterializerMethodInfo.CachedMakeGenericMethod(
133+
elementType.GetGenericArguments()[0])
139134
: MaterializationHelper.CreateItemMaterializerMethodInfo.CachedMakeGenericMethod(elementType);
140-
var itemMaterializer = itemMaterializerFactoryMethod.Invoke(null, [materializationInfo.Expression, itemProjector.AggregateType]);
141-
#endif
142135

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

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

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

85-
internal readonly struct TagsRestorer : IDisposable
85+
public IDisposable DisableSessionTags()
8686
{
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-
}
87+
var originalTags = SessionTags;
88+
SessionTags = null;
89+
return new Disposable((b) => SessionTags = originalTags);
9890
}
9991

100-
public TagsRestorer DisableSessionTags() => new(this);
101-
10292
public void RebindApplyParameter(CompilableProvider old, CompilableProvider @new)
10393
{
10494
if (applyParameters.TryGetValue(old, out var parameter)) {

0 commit comments

Comments
 (0)