Skip to content

Commit 3d6748e

Browse files
committed
Avoid allocating on a per-request basis
1 parent 330cb78 commit 3d6748e

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/Components/Components/src/PersistentState/PersistentServicesRegistry.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ internal sealed class PersistentServicesRegistry
2626

2727
public PersistentServicesRegistry(IServiceProvider serviceProvider)
2828
{
29-
var registrations = serviceProvider.GetRequiredService<IEnumerable<IPersistentServiceRegistration>>();
3029
_serviceProvider = serviceProvider;
31-
_registrations = ResolveRegistrations(registrations);
30+
_registrations = ResolveRegistrations(serviceProvider.GetRequiredService<RegisteredPersistentServiceRegistrationCollection>().Registrations);
3231
}
3332

3433
internal IComponentRenderMode? RenderMode { get; set; }
@@ -140,7 +139,7 @@ private static void RestoreInstanceState(object instance, Type type, PersistentC
140139
}
141140
}
142141

143-
private static IPersistentServiceRegistration[] ResolveRegistrations(IEnumerable<IPersistentServiceRegistration> registrations) => [.. registrations.DistinctBy(r => (r.Assembly, r.FullTypeName)).OrderBy(r => r.Assembly).ThenBy(r => r.FullTypeName)];
142+
internal static IPersistentServiceRegistration[] ResolveRegistrations(IEnumerable<IPersistentServiceRegistration> registrations) => [.. registrations.DistinctBy(r => (r.Assembly, r.FullTypeName)).OrderBy(r => r.Assembly).ThenBy(r => r.FullTypeName)];
144143

145144
private static Type? ResolveType(IPersistentServiceRegistration registration)
146145
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components.Infrastructure;
5+
6+
internal class RegisteredPersistentServiceRegistrationCollection(IEnumerable<IPersistentServiceRegistration> registrations)
7+
{
8+
private readonly IEnumerable<IPersistentServiceRegistration> _registrations =
9+
PersistentServicesRegistry.ResolveRegistrations(registrations);
10+
11+
public IEnumerable<IPersistentServiceRegistration> Registrations => _registrations;
12+
}

src/Components/Components/src/RegisterPersistentComponentStateServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static class RegisterPersistentComponentStateServiceCollectionExtensions
3636
// We resolve the service from the DI container.
3737
// We loop through the properties in the type and try to restore the properties that have SupplyParameterFromPersistentComponentState on them.
3838
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPersistentServiceRegistration>(new PersistentServiceRegistration<TService>(componentRenderMode)));
39+
services.TryAddSingleton<RegisteredPersistentServiceRegistrationCollection>();
3940

4041
return services;
4142
}

0 commit comments

Comments
 (0)