Skip to content
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

Improve MauiFactory performance by avoiding reflection to get base classes #24775

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 10 additions & 8 deletions src/Core/src/Hosting/Internal/MauiFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Maui.Hosting.Internal
Expand All @@ -11,6 +13,7 @@ class MauiFactory : IMauiFactory
{
static readonly Type EnumerableType = typeof(IEnumerable<>);
static readonly Type ListType = typeof(List<>);
static readonly ConcurrentDictionary<Type, IReadOnlyList<Type>> ServiceBaseTypes = new();

readonly IMauiServiceCollection _collection;

Expand Down Expand Up @@ -98,23 +101,22 @@ protected bool TryGetServiceDescriptors(ref Type serviceType, out ServiceDescrip
return false;
}

static List<Type> GetServiceBaseTypes(Type serviceType)
static IReadOnlyList<Type> GetServiceBaseTypes(Type serviceType) =>
ServiceBaseTypes.GetOrAdd(serviceType, ServiceBaseTypesFactory);

static IReadOnlyList<Type> ServiceBaseTypesFactory(Type type)
{
var types = new List<Type> { serviceType };
var types = new List<Type> { type };

Type? baseType = serviceType.BaseType;
Type? baseType = type.BaseType;

while (baseType != null)
{
types.Add(baseType);
baseType = baseType.BaseType;
}

foreach (var interfac in serviceType.GetInterfaces())
{
if (typeof(IView).IsAssignableFrom(interfac))
types.Add(interfac);
}
types.AddRange(type.GetInterfaces().Where(typeof(IView).IsAssignableFrom));

return types;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ public static void AssertWarnings(this List<WarningsPerFile> actualWarnings, Lis
"Microsoft.Maui.Hosting.Internal.MauiFactory.GetServiceBaseTypes(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter '#0' of method 'Microsoft.Maui.Hosting.Internal.MauiFactory.GetServiceBaseTypes(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.",
}
},
new WarningsPerCode
{
Code = "IL2070",
Messages = new List<string>
{
"Microsoft.Maui.Hosting.Internal.MauiFactory.ServiceBaseTypesFactory(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter '#0' of method 'Microsoft.Maui.Hosting.Internal.MauiFactory.ServiceBaseTypesFactory(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.",
}
},
Comment on lines +383 to +390
Copy link
Member

Choose a reason for hiding this comment

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

We should have 0 trimmer warnings going forward in the net9.0 branch. Should we target net9.0 for this change instead?

Ideally, no new trimmer warnings are introduced, and this might be some side-effect of not using .NET 9.

}
},
new WarningsPerFile
Expand Down
Loading