From ed1cb8fbd3616e63c2d7fb395ebfd8e56285e39b Mon Sep 17 00:00:00 2001 From: rameel Date: Fri, 20 Nov 2026 00:40:32 +0500 Subject: [PATCH] parser: look up the string.Concat overloads once Each + with a string operand called typeof(string).GetMethods and filtered the result again, allocating about 3.3 KB per operator. Store the overloads in a static field instead. --- .../ExpressionBuilder.cs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index 163c304..9120808 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -11,6 +11,17 @@ namespace Ramstack.Parsing; /// The binder that resolves the types, members, and methods referenced in the expression. public partial class ExpressionBuilder(Binder binder) : ExprVisitor { + /// + /// The overloads of the string.Concat method that the + operator with a string operand is bound to. + /// + private static readonly MethodBase[] ConcatMethods = typeof(string) + .GetMethods(BindingFlags.Public | BindingFlags.Static) + .Where(m => + m.Name == nameof(string.Concat) + && (m.CallingConvention & CallingConventions.VarArgs) == 0) + .Cast() + .ToArray(); + /// /// Gets the binder that resolves the types, members, and methods referenced in the expression. /// @@ -96,15 +107,6 @@ protected internal override Expression VisitBinary(Expr.Binary expr) Flatten(lhs); Flatten(rhs); - var methods = typeof(string) - .GetMethods( - BindingFlags.Public | BindingFlags.Static) - .Where(m => - m.Name == nameof(string.Concat) - && (m.CallingConvention & CallingConventions.VarArgs) == 0) - .Cast() - .ToArray(); - if (argumentTypes.Any(t => t != typeof(string))) { for (var i = 0; i < arguments.Count; i++) @@ -115,7 +117,7 @@ protected internal override Expression VisitBinary(Expr.Binary expr) } } - var cmi = (MethodInfo?)MethodResolver.ResolveMethod(methods, argumentTypes.ToArray())!; + var cmi = (MethodInfo?)MethodResolver.ResolveMethod(ConcatMethods, argumentTypes.ToArray())!; Debug.Assert(cmi is not null); var args = CreateInjectingArguments(cmi, arguments);