Skip to content
Open
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
34 changes: 19 additions & 15 deletions Orm/Xtensive.Orm/Orm/Linq/Expressions/KeyExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
// Created by: Alexis Kochetov
// Created: 2009.05.05

using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Xtensive.Core;
using Xtensive.Orm.Internals;
using Xtensive.Orm.Linq.Expressions.Visitors;
using Xtensive.Orm.Model;
using TypeInfo = Xtensive.Orm.Model.TypeInfo;

namespace Xtensive.Orm.Linq.Expressions
Expand Down Expand Up @@ -102,18 +99,25 @@ public override KeyExpression RemoveOuterParameter(Dictionary<Expression, Expres

public static KeyExpression Create(TypeInfo entityType, ColNum offset)
{
var mapping = new Segment<ColNum>(offset, entityType.Key.TupleDescriptor.Count);

FieldExpression CreateField(ColumnInfo c) => FieldExpression.CreateField(c.Field, offset);

var fields = entityType.IsLocked
? entityType.Key.Columns.Select(CreateField).ToArray()
: entityType.Columns
.Where(c => c.IsPrimaryKey)
.OrderBy(c => c.Field.MappingInfo.Offset)
.Select(CreateField)
.ToArray();
return new KeyExpression(entityType, fields, mapping, WellKnownMembers.IEntityKey, null, false);
var entityTypeKey = entityType.Key;
IReadOnlyList<FieldExpression> fields;
if (entityType.IsLocked) {
var columns = entityTypeKey.Columns;
var n = columns.Count;
var ar = new FieldExpression[n];
for (int i = 0; i < n; ++i) {
ar[i] = FieldExpression.CreateField(columns[i].Field, offset);
}
fields = ar;
}
else {
List<FieldExpression> list = new(1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my previous experiment showed, that it's more efficient to count array size entityType.Columns.Count(c => c.IsPrimaryKey) and allocate array of fixed size.

foreach (var c in entityType.Columns.Where(c => c.IsPrimaryKey).OrderBy(c => c.Field.MappingInfo.Offset)) {
list.Add(FieldExpression.CreateField(c.Field, offset));
}
fields = list;
}
return new(entityType, fields, new Segment<ColNum>(offset, entityTypeKey.TupleDescriptor.Count), WellKnownMembers.IEntityKey, null, false);
}

internal override Expression Accept(ExtendedExpressionVisitor visitor) => visitor.VisitKeyExpression(this);
Expand Down