这个问题是关于部分代表,以及部分是关于泛型。
This question is partly about delegates, and partly about generics.
由于简化了code:
internal sealed class TypeDispatchProcessor
{
private readonly Dictionary<Type, Delegate> _actionByType
= new Dictionary<Type, Delegate>();
public void RegisterProcedure<T>(Action<T> action)
{
_actionByType[typeof(T)] = action;
}
public void ProcessItem(object item)
{
Delegate action;
if (_actionByType.TryGetValue(item.GetType(), out action))
{
// Can this call to DynamicInvoke be avoided?
action.DynamicInvoke(item);
}
}
}
我读elsewhere在SO 的直接调用委托(带括号)是数量级比调用 DynamicInvoke
,这是有道理的速度更快。
I read elsewhere on SO that invoking a delegate directly (with parenthesis) is orders of magnitude faster than calling DynamicInvoke
, which makes sense.
对于上面的code样,我不知道我是否可以进行类型检查,并以某种方式提高性能。
For the code sample above, I'm wondering whether I can perform the type checking and somehow improve performance.
某些方面:的我有得到外包给各种处理程序对象流,并且这些处理器可以被注册/取消注册在运行时。上述模式的功能完好,我的目的,但我想,使之迅捷如果可能的话。
Some context: I have a stream of objects that get farmed out to various handlers, and those handlers can be registered/unregistered at runtime. The above pattern functions perfectly for my purposes, but I'd like to make it snappier if possible.
一种选择是将存储动作&LT;对象&gt;
在词典
,并包裹了动作&LT; T&GT;
代表与其他代表。我还没有比较性能的变化,这第二个间接调用将影响。
One option would be to store Action<object>
in the Dictionary
, and wrap the Action<T>
delegates with another delegate. I haven't yet compared the performance change that this second indirect call would affect.
推荐答案
我强烈怀疑,包裹接听的话会有很多比使用 DynamicInvoke
更有效。您的code将被:
I strongly suspect that wrapping the calls would be a lot more efficient than using DynamicInvoke
. Your code would then be:
internal sealed class TypeDispatchProcessor
{
private readonly Dictionary<Type, Action<object>> _actionByType
= new Dictionary<Type, Action<object>>();
public void RegisterProcedure<T>(Action<T> action)
{
_actionByType[typeof(T)] = item => action((T) item);
}
public void ProcessItem(object item)
{
Action<object> action;
if (_actionByType.TryGetValue(item.GetType(), out action))
{
action(item);
}
}
}
这是值得基准,但我想你会发现这是一个很多更有效。 DynamicInvoke
必须检查所有与反思等方面,而不是简单的倒在包裹委托的参数。
It's worth benchmarking it, but I think you'll find this a lot more efficient. DynamicInvoke
has to check all the arguments with reflection etc, instead of the simple cast in the wrapped delegate.
相关推荐
最新文章