Skip to content

Optimization: avoid intermediate Pair<ColNum> array #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Orm/Xtensive.Orm/Orm/Internals/EntityDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class EntityDataReader : DomainBound
private readonly record struct RecordPartMapping
(
int TypeIdColumnIndex,
IReadOnlyList<Pair<ColNum>> Columns,
IEnumerable<(ColNum From, ColNum To)> Columns,
TypeInfo ApproximateType
);

Expand All @@ -47,7 +47,7 @@ public IEnumerable<Record> Read(IEnumerable<Tuple> source, RecordSetHeader heade
for (int i = 0; i < recordPartCount; i++) {
var columnGroup = columnGroups[i];
var approximateType = columnGroup.TypeInfoRef.Resolve(model);
var columnMapping = new List<Pair<ColNum>>(columnGroup.Columns.Count);
var columnMapping = new List<(ColNum From, ColNum To)>(columnGroup.Columns.Count);
var typeIdColumnIndex = -1;
foreach (var columnIndex in columnGroup.Columns) {
var column = (MappedColumn) columns[columnIndex];
Expand All @@ -57,7 +57,7 @@ public IEnumerable<Record> Read(IEnumerable<Tuple> source, RecordSetHeader heade
if (columnInfo.Name == typeIdColumnName) {
typeIdColumnIndex = column.Index;
}
columnMapping.Add(new Pair<ColNum>(targetColumnIndex, columnIndex));
columnMapping.Add((targetColumnIndex, columnIndex));
}
}
mappings[i] = new RecordPartMapping(typeIdColumnIndex, columnMapping, approximateType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,7 @@ protected override Expression VisitStructureFieldExpression(StructureFieldExpres
var mappingInfo = expression.Fields
.OfType<FieldExpression>()
.Where(f => f.ExtendedType==ExtendedExpressionType.Field)
.OrderBy(f => f.Field.MappingInfo.Offset)
.Select(f => new Pair<ColNum>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
.Distinct()
.ToArray();
.Select(f => (f.Field.MappingInfo.Offset, f.Mapping.Offset));

var columnMap = MaterializationHelper.CreateSingleSourceMap(tuplePrototype.Count, mappingInfo);

Expand Down Expand Up @@ -296,11 +293,8 @@ protected override Expression VisitStructureExpression(StructureExpression expre
var tuplePrototype = typeInfo.TuplePrototype;
var mappingInfo = expression.Fields
.OfType<FieldExpression>()
.Where(f => f.ExtendedType==ExtendedExpressionType.Field)
.OrderBy(f => f.Field.MappingInfo.Offset)
.Select(f => new Pair<ColNum>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
.Distinct()
.ToArray();
.Where(f => f.ExtendedType==ExtendedExpressionType.Field)
.Select(f => (f.Field.MappingInfo.Offset, f.Mapping.Offset));

var columnMap = MaterializationHelper.CreateSingleSourceMap(tuplePrototype.Count, mappingInfo);

Expand Down Expand Up @@ -362,10 +356,8 @@ private Expression CreateEntity(IEntityExpression expression, Expression tupleEx
var mappingInfo = expression.Fields
.OfType<FieldExpression>()
.Where(f => f.ExtendedType==ExtendedExpressionType.Field)
.OrderBy(f => f.Field.MappingInfo.Offset)
.Select(f => new Pair<ColNum>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
.Distinct()
.ToArray();
.Select(f => (f.Field.MappingInfo.Offset, f.Mapping.Offset))
.ToHashSet();

var isMaterializedExpression = Expression.Call(
itemMaterializationContextParameter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the License.txt file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Reflection;
using Xtensive.Core;
using Xtensive.Orm.Internals;
Expand Down Expand Up @@ -34,7 +35,7 @@ internal sealed class ItemMaterializationContext

public TypeInfo GetTypeInfo(int typeId) => typeId == TypeInfo.NoTypeId ? null : typeIdRegistry[typeId];

public Entity Materialize(int entityIndex, int typeIdIndex, TypeInfo type, Pair<ColNum>[] entityColumns, Tuple tuple)
public Entity Materialize(int entityIndex, int typeIdIndex, TypeInfo type, IEnumerable<(ColNum From, ColNum To)> entityColumns, Tuple tuple)
{
var result = entities[entityIndex];
if (result!=null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public TypeIdRegistry TypeIdRegistry
/// </summary>
public Queue<Action> MaterializationQueue { get; set; }

public TypeMapping GetTypeMapping(int entityIndex, TypeInfo approximateType, int typeId, IReadOnlyList<Pair<ColNum>> columns)
public TypeMapping GetTypeMapping(int entityIndex, TypeInfo approximateType, int typeId, IEnumerable<(ColNum From, ColNum To)> columns)
{
TypeMapping result;
ref var cache = ref entityMappings[entityIndex];
Expand All @@ -75,19 +75,12 @@ public TypeMapping GetTypeMapping(int entityIndex, TypeInfo approximateType, int
var keyInfo = type.Key;
var descriptor = type.TupleDescriptor;

var typeColumnMap = columns;
IEnumerable<(ColNum From, ColNum To)> typeColumnMap = columns;
if (approximateType.IsInterface) {
// fixup target index
var newColumns = new Pair<ColNum>[columns.Count];
for (int i = columns.Count; i-- > 0;) {
var pair = columns[i];
var approxTargetIndex = pair.First;
var interfaceField = approximateType.Columns[approxTargetIndex].Field;
var field = type.FieldMap[interfaceField];
var targetIndex = field.MappingInfo.Offset;
newColumns[i] = new Pair<ColNum>(targetIndex, pair.Second);
}
typeColumnMap = newColumns;
var fieldMap = type.FieldMap;
var approximateTypeColumns = approximateType.Columns;
typeColumnMap = columns.Select(p => (fieldMap[approximateTypeColumns[p.From].Field].MappingInfo.Offset, p.To));
}

ArraySegment<ColNum> allIndexes = MaterializationHelper.CreateSingleSourceMap(descriptor.Count, typeColumnMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,13 @@ internal static class MaterializationHelper
public static readonly MethodInfo PrefetchEntitySetMethodInfo = typeof(MaterializationHelper)
.GetMethod(nameof(PrefetechEntitySet), BindingFlags.Public | BindingFlags.Static);

public static ColNum[] CreateSingleSourceMap(int targetLength, IReadOnlyList<Pair<ColNum>> remappedColumns)
public static ColNum[] CreateSingleSourceMap(int targetLength, IEnumerable<(ColNum From, ColNum To)> remappedColumns)
{
var map = new ColNum[targetLength];
Array.Fill(map, MapTransform.NoMapping);

for (int i = 0, count = remappedColumns.Count; i < count; i++) {
var remappedColumn = remappedColumns[i];
var targetIndex = remappedColumn.First;
var sourceIndex = remappedColumn.Second;
map[targetIndex] = sourceIndex;
foreach (var p in remappedColumns) {
map[p.From] = p.To;
}

return map;
}

Expand Down