From 71e9aa96ec7157181e5cfddab7f9998fa8f4a7cb Mon Sep 17 00:00:00 2001 From: Sergei Pavlov Date: Tue, 5 Oct 2021 04:19:46 -0700 Subject: [PATCH 1/3] Optimize TypeInfoCollection --- .../Orm/Model/TypeInfoCollection.cs | 67 ++++++++----------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs b/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs index 37a5975838..dff4aff3a8 100644 --- a/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs +++ b/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2020 Xtensive LLC. +// Copyright (C) 2007-2021 Xtensive LLC. // This code is distributed under MIT license terms. // See the License.txt file in the project root for more information. // Created by: Dmitri Maximov @@ -34,14 +34,9 @@ public sealed class TypeInfoCollection /// An indexer that provides access to collection items. /// /// Item was not found. - public TypeInfo this[Type key] { - get { - TypeInfo result; - if (!TryGetValue(key, out result)) - throw new KeyNotFoundException(string.Format(Strings.TypeXIsNotRegistered, key.GetShortName())); - return result; - } - } + public TypeInfo this[Type key] => TryGetValue(key, out var result) + ? result + : throw new KeyNotFoundException(string.Format(Strings.TypeXIsNotRegistered, key.GetShortName())); /// /// An indexer that provides access to collection items by their . @@ -217,15 +212,13 @@ public IEnumerable FindDescendants(TypeInfo item, bool recursive) { ArgumentValidator.EnsureArgumentNotNull(item, "item"); - HashSet result; - if (!descendantTable.TryGetValue(item, out result)) - result = new HashSet(); - - foreach (var item1 in result) { - yield return item1; - if (recursive) - foreach (var item2 in FindDescendants(item1, true)) - yield return item2; + if (descendantTable.TryGetValue(item, out var result)) { + foreach (var item1 in result) { + yield return item1; + if (recursive) + foreach (var item2 in FindDescendants(item1, true)) + yield return item2; + } } } @@ -251,12 +244,10 @@ public IEnumerable FindInterfaces(TypeInfo item, bool recursive) { ArgumentValidator.EnsureArgumentNotNull(item, "item"); - HashSet result; - if (!interfaceTable.TryGetValue(item, out result)) - result = new HashSet(); - - foreach (var item1 in result) - yield return item1; + if (interfaceTable.TryGetValue(item, out var result)) { + foreach (var item1 in result) + yield return item1; + } if (!recursive || item.IsInterface) yield break; @@ -294,14 +285,13 @@ public IEnumerable FindImplementors(TypeInfo item, bool recursive) ArgumentValidator.EnsureArgumentNotNull(item, "item"); HashSet result; - if (!implementorTable.TryGetValue(item, out result)) - result = new HashSet(); - - foreach (var item1 in result) { - yield return item1; - if (recursive && !item1.IsInterface) - foreach (var item2 in FindDescendants(item1, true)) - yield return item2; + if (implementorTable.TryGetValue(item, out result)) { + foreach (var item1 in result) { + yield return item1; + if (recursive && !item1.IsInterface) + foreach (var item2 in FindDescendants(item1, true)) + yield return item2; + } } } @@ -334,12 +324,13 @@ public TypeInfo FindRoot(TypeInfo item) /// instance that is ancestor of specified or /// if the ancestor is not found in this collection. /// When is . - private TypeInfo FindAncestor(Type type) - { - if (type == WellKnownTypes.Object || type.BaseType == null) - return null; - return Contains(type.BaseType) ? this[type.BaseType] : FindAncestor(type.BaseType); - } + private TypeInfo FindAncestor(Type type) => + type == WellKnownTypes.Object + ? null + : type.BaseType switch { + null => null, + var baseType => TryGetValue(baseType, out var typeInfo) ? typeInfo : FindAncestor(baseType) + }; #endregion From 13ae817436f9cf64d4986b4d54ea68d13e0548d6 Mon Sep 17 00:00:00 2001 From: Sergei Pavlov Date: Tue, 5 Oct 2021 05:02:57 -0700 Subject: [PATCH 2/3] Small fix --- Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs b/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs index dff4aff3a8..148303b0e9 100644 --- a/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs +++ b/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs @@ -165,7 +165,7 @@ public override bool Contains(TypeInfo item) /// Finds the type by its full name. /// /// The full name of the type to find. - /// Found type, if any; + /// Found type, if any; /// , if there is no type with specified full name. public TypeInfo Find(string fullName) { @@ -178,7 +178,7 @@ public TypeInfo Find(string fullName) /// Finds the ancestor of the specified . /// /// The type to search ancestor for. - /// instance that is ancestor of specified or + /// instance that is ancestor of specified or /// if the ancestor is not found in this collection. /// When is . public TypeInfo FindAncestor(TypeInfo item) @@ -284,8 +284,7 @@ public IEnumerable FindImplementors(TypeInfo item, bool recursive) { ArgumentValidator.EnsureArgumentNotNull(item, "item"); - HashSet result; - if (implementorTable.TryGetValue(item, out result)) { + if (implementorTable.TryGetValue(item, out var result)) { foreach (var item1 in result) { yield return item1; if (recursive && !item1.IsInterface) @@ -321,7 +320,7 @@ public TypeInfo FindRoot(TypeInfo item) /// Finds the ancestor of the specified . /// /// The type to search ancestor for. - /// instance that is ancestor of specified or + /// instance that is ancestor of specified or /// if the ancestor is not found in this collection. /// When is . private TypeInfo FindAncestor(Type type) => @@ -434,7 +433,7 @@ protected override string GetExceptionMessage(string key) // Constructors - + /// public TypeInfoCollection(Node owner, string name) : base(owner, name) From 4a20258351a26b6fda2db5ed6c6a89faf2a3fe31 Mon Sep 17 00:00:00 2001 From: Sergei Pavlov Date: Tue, 5 Oct 2021 23:17:05 -0700 Subject: [PATCH 3/3] Refactor FindAncestor() --- .../Orm/Model/TypeInfoCollection.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs b/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs index 148303b0e9..54d1bf2ee4 100644 --- a/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs +++ b/Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs @@ -323,13 +323,16 @@ public TypeInfo FindRoot(TypeInfo item) /// instance that is ancestor of specified or /// if the ancestor is not found in this collection. /// When is . - private TypeInfo FindAncestor(Type type) => - type == WellKnownTypes.Object - ? null - : type.BaseType switch { - null => null, - var baseType => TryGetValue(baseType, out var typeInfo) ? typeInfo : FindAncestor(baseType) - }; + private TypeInfo FindAncestor(Type type) + { + if (type == WellKnownTypes.Object) { + return null; + } + return type.BaseType switch { + null => null, + var baseType => TryGetValue(baseType, out var typeInfo) ? typeInfo : FindAncestor(baseType) + }; + } #endregion