From 9029479963efd32c83fb40c0b3d9f8210505327f Mon Sep 17 00:00:00 2001 From: eeichinger Date: Tue, 13 Oct 2009 22:04:08 +0000 Subject: [PATCH] improved performance acc. to SPRNET-1257 by replacing DynamicInvoke with SafeMethod.Invoke --- .../Spring.Core/Expressions/FunctionNode.cs | 7 ++- .../Expressions/FunctionNodeTests.cs | 56 +++++++++++++++++-- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/Spring/Spring.Core/Expressions/FunctionNode.cs b/src/Spring/Spring.Core/Expressions/FunctionNode.cs index 5846f5aa..a3d1ba73 100644 --- a/src/Spring/Spring.Core/Expressions/FunctionNode.cs +++ b/src/Spring/Spring.Core/Expressions/FunctionNode.cs @@ -20,7 +20,8 @@ using System; using System.Collections; -using System.Runtime.Serialization; +using System.Runtime.Serialization; +using Spring.Reflection.Dynamic; namespace Spring.Expressions { @@ -68,7 +69,9 @@ namespace Spring.Expressions // delegate? if (function != null) { - return function.DynamicInvoke(argValues); + SafeMethod m = new SafeMethod(function.Method); + return m.Invoke(function.Target, argValues); +// return function.DynamicInvoke(argValues); } // lambda! diff --git a/test/Spring/Spring.Core.Tests/Expressions/FunctionNodeTests.cs b/test/Spring/Spring.Core.Tests/Expressions/FunctionNodeTests.cs index 022ffbfb..aa6bd5a2 100644 --- a/test/Spring/Spring.Core.Tests/Expressions/FunctionNodeTests.cs +++ b/test/Spring/Spring.Core.Tests/Expressions/FunctionNodeTests.cs @@ -55,22 +55,66 @@ namespace Spring.Expressions public void ExecutesDelegate() { Hashtable vars = new Hashtable(); - vars["ident"] = new IdentityCallback(Identity); + vars["concat"] = new TestCallback(Concat); FunctionNode fn = new FunctionNode(); - fn.Text = "ident"; + fn.Text = "concat"; StringLiteralNode str = new StringLiteralNode(); str.Text = "theValue"; fn.addChild(str); + StringLiteralNode str2 = new StringLiteralNode(); + str2.Text = "theValue"; + fn.addChild(str2); IExpression exp = fn; - Assert.AreEqual(str.Text, exp.GetValue(null, vars)); + Assert.AreEqual(string.Format("{0},{1},{2}", this.GetHashCode(), str.Text, str2.Text), exp.GetValue(null, vars)); } - private delegate object IdentityCallback(object arg); - private object Identity(object arg) + [Category("Performance")] + [Test, Explicit] + public void ExecutesDelegatePerformance() { - return arg; + Hashtable vars = new Hashtable(); + TestCallback concat = new TestCallback(Concat); + vars["concat"] = concat; + + FunctionNode fn = new FunctionNode(); + fn.Text = "concat"; + StringLiteralNode str = new StringLiteralNode(); + str.Text = "theValue"; + fn.addChild(str); + StringLiteralNode str2 = new StringLiteralNode(); + str2.Text = "theValue"; + fn.addChild(str2); + + IExpression exp = fn; + + string result = string.Format("{0},{1},{2}", this.GetHashCode(), str.Text, str2.Text); + + int ITERATIONS = 1000000; + + StopWatch watch = new StopWatch(); + using (watch.Start("Duration SpEL: {0}")) + { + for (int i = 0; i < ITERATIONS; i++) + { + Assert.AreEqual(result, exp.GetValue(null, vars)); + } + } + + using (watch.Start("Duration Direct: {0}")) + { + for (int i = 0; i < ITERATIONS; i++) + { + Assert.AreEqual(result, concat(str.Text, str2.Text)); + } + } + } + + private delegate object TestCallback(object arg1, object arg2); + private object Concat(object arg1, object arg2) + { + return string.Format("{0},{1},{2}", this.GetHashCode(), arg1, arg2); } } } \ No newline at end of file