From 5df80adcbfb486c594dcff84f154029936bd9764 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 13 Aug 2024 22:20:40 +1000 Subject: [PATCH] better perf in SupportedMethods calculation in SqlServerConvertTranslator --- .../Translators/SqlServerConvertTranslator.cs | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/EFCore.SqlServer/Query/Internal/Translators/SqlServerConvertTranslator.cs b/src/EFCore.SqlServer/Query/Internal/Translators/SqlServerConvertTranslator.cs index 5bd07b1a8a9..2efe9fe7391 100644 --- a/src/EFCore.SqlServer/Query/Internal/Translators/SqlServerConvertTranslator.cs +++ b/src/EFCore.SqlServer/Query/Internal/Translators/SqlServerConvertTranslator.cs @@ -41,14 +41,7 @@ public class SqlServerConvertTranslator : IMethodCallTranslator typeof(object) ]; - private static readonly MethodInfo[] SupportedMethods - = TypeMapping.Keys - .SelectMany( - t => typeof(Convert).GetTypeInfo().GetDeclaredMethods(t) - .Where( - m => m.GetParameters().Length == 1 - && SupportedTypes.Contains(m.GetParameters().First().ParameterType))) - .ToArray(); + private static readonly MethodInfo[] SupportedMethods; private readonly ISqlExpressionFactory _sqlExpressionFactory; @@ -63,6 +56,22 @@ public SqlServerConvertTranslator(ISqlExpressionFactory sqlExpressionFactory) _sqlExpressionFactory = sqlExpressionFactory; } + static SqlServerConvertTranslator() + { + var convertInfo = typeof(Convert).GetTypeInfo(); + SupportedMethods = TypeMapping.Keys + .SelectMany( + name => convertInfo.GetDeclaredMethods(name) + .Where( + method => + { + var parameters = method.GetParameters(); + return parameters.Length == 1 + && SupportedTypes.Contains(parameters[0].ParameterType); + })) + .ToArray(); + } + /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in