Skip to content

Commit 38d9090

Browse files
authored
Merge pull request #185 from servicetitan/upstream/Optimize_TypeInfoCollection
Optimize TypeInfoCollection (#42)
2 parents b42b10d + 4699d05 commit 38d9090

1 file changed

Lines changed: 28 additions & 35 deletions

File tree

Orm/Xtensive.Orm/Orm/Model/TypeInfoCollection.cs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2007-2020 Xtensive LLC.
1+
// Copyright (C) 2007-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
@@ -34,14 +34,9 @@ public sealed class TypeInfoCollection
3434
/// An indexer that provides access to collection items.
3535
/// </summary>
3636
/// <exception cref="ArgumentException">Item was not found.</exception>
37-
public TypeInfo this[Type key] {
38-
get {
39-
TypeInfo result;
40-
if (!TryGetValue(key, out result))
41-
throw new KeyNotFoundException(string.Format(Strings.TypeXIsNotRegistered, key.GetShortName()));
42-
return result;
43-
}
44-
}
37+
public TypeInfo this[Type key] => TryGetValue(key, out var result)
38+
? result
39+
: throw new KeyNotFoundException(string.Format(Strings.TypeXIsNotRegistered, key.GetShortName()));
4540

4641
/// <summary>
4742
/// An indexer that provides access to collection items by their <see cref="TypeInfo.TypeId"/>.
@@ -217,15 +212,13 @@ public IEnumerable<TypeInfo> FindDescendants(TypeInfo item, bool recursive)
217212
{
218213
ArgumentValidator.EnsureArgumentNotNull(item, "item");
219214

220-
HashSet<TypeInfo> result;
221-
if (!descendantTable.TryGetValue(item, out result))
222-
result = new HashSet<TypeInfo>();
223-
224-
foreach (var item1 in result) {
225-
yield return item1;
226-
if (recursive)
227-
foreach (var item2 in FindDescendants(item1, true))
228-
yield return item2;
215+
if (descendantTable.TryGetValue(item, out var result)) {
216+
foreach (var item1 in result) {
217+
yield return item1;
218+
if (recursive)
219+
foreach (var item2 in FindDescendants(item1, true))
220+
yield return item2;
221+
}
229222
}
230223
}
231224

@@ -251,12 +244,10 @@ public IEnumerable<TypeInfo> FindInterfaces(TypeInfo item, bool recursive)
251244
{
252245
ArgumentValidator.EnsureArgumentNotNull(item, "item");
253246

254-
HashSet<TypeInfo> result;
255-
if (!interfaceTable.TryGetValue(item, out result))
256-
result = new HashSet<TypeInfo>();
257-
258-
foreach (var item1 in result)
259-
yield return item1;
247+
if (interfaceTable.TryGetValue(item, out var result)) {
248+
foreach (var item1 in result)
249+
yield return item1;
250+
}
260251

261252
if (!recursive || item.IsInterface)
262253
yield break;
@@ -293,15 +284,13 @@ public IEnumerable<TypeInfo> FindImplementors(TypeInfo item, bool recursive)
293284
{
294285
ArgumentValidator.EnsureArgumentNotNull(item, "item");
295286

296-
HashSet<TypeInfo> result;
297-
if (!implementorTable.TryGetValue(item, out result))
298-
result = new HashSet<TypeInfo>();
299-
300-
foreach (var item1 in result) {
301-
yield return item1;
302-
if (recursive && !item1.IsInterface)
303-
foreach (var item2 in FindDescendants(item1, true))
304-
yield return item2;
287+
if (implementorTable.TryGetValue(item, out var result)) {
288+
foreach (var item1 in result) {
289+
yield return item1;
290+
if (recursive && !item1.IsInterface)
291+
foreach (var item2 in FindDescendants(item1, true))
292+
yield return item2;
293+
}
305294
}
306295
}
307296

@@ -336,9 +325,13 @@ public TypeInfo FindRoot(TypeInfo item)
336325
/// <exception cref="ArgumentNullException">When <paramref name="type"/> is <see langword="null"/>.</exception>
337326
private TypeInfo FindAncestor(Type type)
338327
{
339-
if (type == WellKnownTypes.Object || type.BaseType == null)
328+
if (type == WellKnownTypes.Object) {
340329
return null;
341-
return Contains(type.BaseType) ? this[type.BaseType] : FindAncestor(type.BaseType);
330+
}
331+
return type.BaseType switch {
332+
null => null,
333+
var baseType => TryGetValue(baseType, out var typeInfo) ? typeInfo : FindAncestor(baseType)
334+
};
342335
}
343336

344337
#endregion

0 commit comments

Comments
 (0)