Skip to content

Optimizations of Domain build (#83) #277

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@
</ItemGroup>

<Import Condition="Exists('User.Directory.Build.props')" Project="User.Directory.Build.props" />
<PropertyGroup>
<DefineConstants Condition="'$(DO_SAFE_COLLECTION_WRAPPER)'!='false'">$(DefineConstants);DO_SAFE_COLLECTION_WRAPPER</DefineConstants>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static void InternalPerformanceTest(int nodeCount, int averageConnection
List<Node<int, int>> removedEdges;
var result = TopologicalSorter.SortToList(nodes, out removedEdges);
if (!allowLoops)
Assert.AreEqual(nodeCount, result.Count);
Assert.AreEqual(nodeCount, result.Count());
}
GC.GetTotalMemory(true);
}
Expand Down
25 changes: 23 additions & 2 deletions Orm/Xtensive.Orm/Core/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
using System;
using System.Collections;
using System.Collections.Generic;

using System.Linq;
using System.Runtime.CompilerServices;

namespace Xtensive.Core
{
Expand Down Expand Up @@ -119,5 +120,25 @@ public static void EnsureIndexIsValid(this IList list, int index)
if (index < 0 || index >= list.Count)
throw new IndexOutOfRangeException(Strings.ExIndexOutOfRange);
}

#if DO_SAFE_COLLECTION_WRAPPER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IReadOnlyList<T> AsSafeWrapper<T>(this List<T> list) => list.AsReadOnly();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IReadOnlyList<T> AsSafeWrapper<T>(this IReadOnlyList<T> list) => Array.AsReadOnly(list.ToArray());

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IReadOnlyList<T> AsSafeWrapper<T>(this T[] array) => Array.AsReadOnly(array);
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IReadOnlyList<T> AsSafeWrapper<T>(this List<T> list) => list;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IReadOnlyList<T> AsSafeWrapper<T>(this IReadOnlyList<T> list) => list;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IReadOnlyList<T> AsSafeWrapper<T>(this T[] array) => array;
#endif
}
}
}
16 changes: 8 additions & 8 deletions Orm/Xtensive.Orm/Linq/ExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Xtensive.Linq
/// </summary>
public abstract class ExpressionVisitor : ExpressionVisitor<Expression>
{
protected override ReadOnlyCollection<Expression> VisitExpressionList(ReadOnlyCollection<Expression> expressions)
protected override IReadOnlyList<Expression> VisitExpressionList(ReadOnlyCollection<Expression> expressions)
{
bool isChanged = false;
var results = new List<Expression>(expressions.Count);
Expand All @@ -28,7 +28,7 @@ protected override ReadOnlyCollection<Expression> VisitExpressionList(ReadOnlyCo
results.Add(p);
isChanged |= !ReferenceEquals(expression, p);
}
return isChanged ? results.AsReadOnly() : expressions;
return isChanged ? results.AsSafeWrapper() : expressions;
}

/// <summary>
Expand All @@ -38,8 +38,8 @@ protected override ReadOnlyCollection<Expression> VisitExpressionList(ReadOnlyCo
/// <returns>Visit result.</returns>
protected virtual ElementInit VisitElementInitializer(ElementInit initializer)
{
ReadOnlyCollection<Expression> arguments = VisitExpressionList(initializer.Arguments);
if (arguments!=initializer.Arguments) {
var arguments = VisitExpressionList(initializer.Arguments);
if (arguments != initializer.Arguments) {
return Expression.ElementInit(initializer.AddMethod, arguments);
}
return initializer;
Expand All @@ -50,7 +50,7 @@ protected virtual ElementInit VisitElementInitializer(ElementInit initializer)
/// </summary>
/// <param name="original">The original element initializer list.</param>
/// <returns>Visit result.</returns>
protected virtual ReadOnlyCollection<ElementInit> VisitElementInitializerList(ReadOnlyCollection<ElementInit> original)
protected virtual IReadOnlyList<ElementInit> VisitElementInitializerList(ReadOnlyCollection<ElementInit> original)
{
var results = new List<ElementInit>();
bool isChanged = false;
Expand All @@ -60,7 +60,7 @@ protected virtual ReadOnlyCollection<ElementInit> VisitElementInitializerList(Re
results.Add(p);
isChanged |= !ReferenceEquals(originalIntializer, p);
}
return isChanged ? results.AsReadOnly() : original;
return isChanged ? results.AsSafeWrapper() : original;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -253,7 +253,7 @@ protected virtual MemberMemberBinding VisitMemberMemberBinding(MemberMemberBindi
/// </summary>
/// <param name="original">The original binding list.</param>
/// <returns>Visit result.</returns>
protected virtual ReadOnlyCollection<MemberBinding> VisitBindingList(ReadOnlyCollection<MemberBinding> original)
protected virtual IReadOnlyList<MemberBinding> VisitBindingList(ReadOnlyCollection<MemberBinding> original)
{
var results = new List<MemberBinding>();
bool isChanged = false;
Expand All @@ -263,7 +263,7 @@ protected virtual ReadOnlyCollection<MemberBinding> VisitBindingList(ReadOnlyCol
results.Add(p);
isChanged |= !ReferenceEquals(originalBinding, p);
}
return isChanged ? results.AsReadOnly() : original;
return isChanged ? results.AsSafeWrapper() : original;
}

protected virtual MemberListBinding VisitMemberListBinding(MemberListBinding binding)
Expand Down
5 changes: 3 additions & 2 deletions Orm/Xtensive.Orm/Linq/ExpressionVisitor{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using Xtensive.Reflection;
using Xtensive.Core;



Expand Down Expand Up @@ -142,14 +143,14 @@ protected virtual TResult Visit(Expression e)
/// </summary>
/// <param name="expressions">The expression list.</param>
/// <returns>Visit result.</returns>
protected virtual ReadOnlyCollection<TResult> VisitExpressionList(ReadOnlyCollection<Expression> expressions)
protected virtual IReadOnlyList<TResult> VisitExpressionList(ReadOnlyCollection<Expression> expressions)
{
var results = new List<TResult>(expressions.Count);
for (int i = 0, n = expressions.Count; i < n; i++) {
var p = Visit(expressions[i]);
results.Add(p);
}
return results.AsReadOnly();
return results.AsSafeWrapper();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void BuildReversedAssociation(BuildingContext context, Association
AssociationInfo existing;
if (!context.Model.Associations.TryGetValue(association.Name, out existing)) {
context.Model.Associations.Add(association);
association.Ancestors.AddRange(field.Associations);
association.AddAncestors(field.Associations);

var associationsToRemove = field.Associations
.Where(a => a.TargetType == association.TargetType)
Expand Down
8 changes: 5 additions & 3 deletions Orm/Xtensive.Orm/Orm/Building/Builders/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private void PreprocessAssociations()
.ToList();
if (pairedAssociations.Count > 0) {
foreach (var paired in pairedAssociations) {
paired.First.Ancestors.AddRange(interfaceAssociations);
paired.First.AddAncestors(interfaceAssociations);
if (paired.First.TargetType.IsInterface || typesWithProcessedInheritedAssociations.Contains(paired.First.TargetType))
AssociationBuilder.BuildReversedAssociation(context, paired.First, paired.Second);
else {
Expand Down Expand Up @@ -288,11 +288,13 @@ bool associationFilter(AssociationInfo a)
var interfaceAssociationsToRemove = interfaceAssociations
.Where(ia => ia.OwnerType != association.OwnerType)
.ToList();
association.Ancestors.AddRange(interfaceAssociationsToRemove);
association.AddAncestors(interfaceAssociationsToRemove);
foreach (var interfaceAssociation in interfaceAssociationsToRemove)
interfaceAssociations.Remove(interfaceAssociation);
}
refField.Associations.AddRange(interfaceAssociations);
if (interfaceAssociations.Count > 0) {
refField.Associations.AddRange(interfaceAssociations);
}
foreach (var association in inheritedAssociations) {
if (!refField.Associations.Contains(association.Name))
refField.Associations.Add(association);
Expand Down
23 changes: 13 additions & 10 deletions Orm/Xtensive.Orm/Orm/Building/Builders/TypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using Xtensive.Core;
using Xtensive.Orm.Building.Definitions;
using Xtensive.Orm.Building.DependencyGraph;
Expand Down Expand Up @@ -52,7 +53,7 @@ public TypeInfo BuildType(TypeDef typeDef)
MappingSchema = typeDef.MappingSchema,
HasVersionRoots = TypeHelper.GetInterfacesUnordered(typeDef.UnderlyingType)
.Any(static type => type == typeof(IHasVersionRoots)),
Validators = validators.AsReadOnly(),
Validators = validators.AsSafeWrapper(),
};

if (typeDef.StaticTypeId != null) {
Expand Down Expand Up @@ -234,6 +235,16 @@ private FieldInfo BuildDeclaredField(TypeInfo type, FieldDef fieldDef)
{
BuildLog.Info(nameof(Strings.LogBuildingDeclaredFieldXY), type.Name, fieldDef.Name);

var validators = fieldDef.Validators;

if (fieldDef.IsStructure && DeclaresOnValidate(fieldDef.ValueType)) {
validators.Add(new StructureFieldValidator());
}

if (fieldDef.IsEntitySet && DeclaresOnValidate(fieldDef.ValueType)) {
validators.Add(new EntitySetFieldValidator());
}

var fieldInfo = new FieldInfo(type, fieldDef.Attributes) {
UnderlyingProperty = fieldDef.UnderlyingProperty,
Name = fieldDef.Name,
Expand All @@ -244,17 +255,9 @@ private FieldInfo BuildDeclaredField(TypeInfo type, FieldDef fieldDef)
Length = fieldDef.Length,
Scale = fieldDef.Scale,
Precision = fieldDef.Precision,
Validators = fieldDef.Validators,
Validators = validators,
};

if (fieldInfo.IsStructure && DeclaresOnValidate(fieldInfo.ValueType)) {
fieldInfo.Validators.Add(new StructureFieldValidator());
}

if (fieldInfo.IsEntitySet && DeclaresOnValidate(fieldInfo.ValueType)) {
fieldInfo.Validators.Add(new EntitySetFieldValidator());
}

type.Fields.Add(fieldInfo);

if (fieldInfo.IsEntitySet) {
Expand Down
4 changes: 2 additions & 2 deletions Orm/Xtensive.Orm/Orm/Building/BuildingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public sealed class BuildingContext
/// <summary>
/// Gets all available <see cref="IModule"/> implementations.
/// </summary>
public ICollection<IModule> Modules { get; private set; }
public IReadOnlyList<IModule> Modules { get; }

/// <summary>
/// Gets all available <see cref="IModule2"/> implementations.
/// </summary>
public ICollection<IModule2> Modules2 { get; private set; }
public IReadOnlyList<IModule2> Modules2 { get; }

internal ModelDefBuilder ModelDefBuilder { get; set; }

Expand Down
3 changes: 1 addition & 2 deletions Orm/Xtensive.Orm/Orm/Building/Definitions/FieldDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public string PairTo
/// <summary>
/// Gets of <see cref="IPropertyValidator"/> instances associated with this field.
/// </summary>
public List<IPropertyValidator> Validators { get; private set; }
public List<IPropertyValidator> Validators { get; } = new();

internal bool IsDeclaredAsNullable
{
Expand Down Expand Up @@ -325,7 +325,6 @@ internal FieldDef(Type valueType, Validator validator)
if ((valueType.IsClass || valueType.IsInterface) && !IsStructure)
attributes |= FieldAttributes.Nullable;
ValueType = valueType;
Validators = new List<IPropertyValidator>();

// Nullable<T>
if (valueType.IsNullable()) {
Expand Down
4 changes: 1 addition & 3 deletions Orm/Xtensive.Orm/Orm/Building/Definitions/TypeDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public NodeCollection<TypeDef> Implementors
/// <summary>
/// Gets <see cref="IObjectValidator"/> instances associated with this type.
/// </summary>
public List<IObjectValidator> Validators { get; private set; }
public List<IObjectValidator> Validators { get; } = new();

/// <summary>
/// Gets or sets the type discriminator value.
Expand Down Expand Up @@ -219,8 +219,6 @@ internal TypeDef(ModelDefBuilder builder, Type type, Validator validator)
implementors = IsInterface
? new NodeCollection<TypeDef>(this, "Implementors")
: NodeCollection<TypeDef>.Empty;

Validators = new List<IObjectValidator>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Xtensive.Orm.Internals.Prefetch
{
internal interface IHasNestedNodes
{
ReadOnlyCollection<BaseFieldNode> NestedNodes { get; }
IReadOnlyList<BaseFieldNode> NestedNodes { get; }

IReadOnlyCollection<Key> ExtractKeys(object target);

IHasNestedNodes ReplaceNestedNodes(ReadOnlyCollection<BaseFieldNode> nestedNodes);
IHasNestedNodes ReplaceNestedNodes(IReadOnlyList<BaseFieldNode> nestedNodes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class KeyExtractorNode<T> : Node, IHasNestedNodes
{
public Func<T, IReadOnlyCollection<Key>> KeyExtractor { get; }

public ReadOnlyCollection<BaseFieldNode> NestedNodes { get; }
public IReadOnlyList<BaseFieldNode> NestedNodes { get; }

IReadOnlyCollection<Key> IHasNestedNodes.ExtractKeys(object target)
{
Expand All @@ -27,10 +27,8 @@ public IReadOnlyCollection<Key> ExtractKeys(T target)
return KeyExtractor.Invoke(target);
}

public IHasNestedNodes ReplaceNestedNodes(ReadOnlyCollection<BaseFieldNode> nestedNodes)
{
return new KeyExtractorNode<T>(KeyExtractor, nestedNodes);
}
public IHasNestedNodes ReplaceNestedNodes(IReadOnlyList<BaseFieldNode> nestedNodes) =>
new KeyExtractorNode<T>(KeyExtractor, nestedNodes);

public override Node Accept(NodeVisitor visitor)
{
Expand All @@ -42,7 +40,7 @@ protected override string GetDescription()
return $"KeyExtraction<{typeof(T).Name}>";
}

public KeyExtractorNode(Func<T, IReadOnlyCollection<Key>> extractor, ReadOnlyCollection<BaseFieldNode> nestedNodes)
public KeyExtractorNode(Func<T, IReadOnlyCollection<Key>> extractor, IReadOnlyList<BaseFieldNode> nestedNodes)
: base("*")
{
ArgumentValidator.EnsureArgumentNotNull(extractor, nameof(extractor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Xtensive.Core;

namespace Xtensive.Orm.Internals.Prefetch
{
Expand All @@ -26,7 +27,7 @@ public static IList<KeyExtractorNode<T>> Aggregate(IEnumerable<KeyExtractorNode<
return result;
}

public override ReadOnlyCollection<BaseFieldNode> VisitNodeList(ReadOnlyCollection<BaseFieldNode> nodes)
public override IReadOnlyList<BaseFieldNode> VisitNodeList(IReadOnlyList<BaseFieldNode> nodes)
{
var result = new List<BaseFieldNode>();
foreach (var group in nodes.Where(n => n!=null).GroupBy(n => n.Path)) {
Expand All @@ -36,11 +37,11 @@ public override ReadOnlyCollection<BaseFieldNode> VisitNodeList(ReadOnlyCollecti
result.Add(node);
else {
var nodeToVisit = (BaseFieldNode) container.ReplaceNestedNodes(
new ReadOnlyCollection<BaseFieldNode>(group.Cast<IHasNestedNodes>().SelectMany(c => c.NestedNodes).ToList()));
group.Cast<IHasNestedNodes>().SelectMany(c => c.NestedNodes).ToList().AsSafeWrapper());
result.Add((BaseFieldNode) Visit(nodeToVisit));
}
}
return new ReadOnlyCollection<BaseFieldNode>(result);
return result.AsSafeWrapper();
}

// Constructor
Expand Down
6 changes: 3 additions & 3 deletions Orm/Xtensive.Orm/Orm/Internals/Prefetch/Nodes/NodeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2012-2020 Xtensive LLC.
// Copyright (C) 2012-2020 Xtensive LLC.
// This code is distributed under MIT license terms.
// See the License.txt file in the project root for more information.
// Created by: Denis Krjuchkov
Expand Down Expand Up @@ -119,9 +119,9 @@ private IEnumerable<BaseFieldNode> VisitMemberAccess(MemberExpression access)
return EnumerableUtils.One(result);
}

private static ObjectModel.ReadOnlyCollection<BaseFieldNode> WrapNodes(IEnumerable<BaseFieldNode> nodes)
private static IReadOnlyList<BaseFieldNode> WrapNodes(IEnumerable<BaseFieldNode> nodes)
{
return nodes.ToList().AsReadOnly();
return nodes.ToList().AsSafeWrapper();
}

private IEnumerable<BaseFieldNode> VisitChildren(Expression expression)
Expand Down
Loading