Răsfoiți Sursa

Reduce allocations due to LINQ and string building. (#28467)

* Reduce allocations due to LINQ and string building.

* Update src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs

Co-authored-by: Pranav K <[email protected]>
Paulo Morgado 5 ani în urmă
părinte
comite
9cfb13398c

+ 15 - 7
src/HealthChecks/HealthChecks/src/DefaultHealthCheckService.cs

@@ -158,15 +158,23 @@ namespace Microsoft.Extensions.Diagnostics.HealthChecks
         private static void ValidateRegistrations(IEnumerable<HealthCheckRegistration> registrations)
         {
             // Scan the list for duplicate names to provide a better error if there are duplicates.
-            var duplicateNames = registrations
-                .GroupBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
-                .Where(g => g.Count() > 1)
-                .Select(g => g.Key)
-                .ToList();
 
-            if (duplicateNames.Count > 0)
+            StringBuilder? builder = null;
+            var distinctRegistrations = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
+
+            foreach (var registration in registrations)
+            {
+                if (!distinctRegistrations.Add(registration.Name))
+                {
+                    builder ??= new StringBuilder("Duplicate health checks were registered with the name(s): ");
+
+                    builder.Append(registration.Name).Append(", ");
+                }
+            }
+
+            if (builder is not null)
             {
-                throw new ArgumentException($"Duplicate health checks were registered with the name(s): {string.Join(", ", duplicateNames)}", nameof(registrations));
+                throw new ArgumentException(builder.ToString(0, builder.Length - 2), nameof(registrations));
             }
         }