improved performance acc. to SPRNET-1257 by replacing DynamicInvoke with SafeMethod.Invoke

This commit is contained in:
eeichinger
2009-10-13 22:04:08 +00:00
parent 4033291cc9
commit 9029479963
2 changed files with 55 additions and 8 deletions

View File

@@ -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!

View File

@@ -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);
}
}
}