简体   繁体   English

C#中的通用接口是否可以防止拳击? (.NET vs Mono性能)

[英]Do generic interfaces in C# prevent boxing? (.NET vs Mono performance)

I have a C# interface with certain method parameters declared as object types. 我有一个C#接口,其中某些方法参数声明为object类型。 However, the actual type passed around can differ depending on the class implementing the interface: 但是,传递的实际类型可能因实现接口的类而异:

public interface IMyInterface
{
    void MyMethod(object arg);
}

public class MyClass1 : IMyInterface
{
    public void MyMethod(object arg)
    {
        MyObject obj = (MyObject) arg;
        // do something with obj...
    }
}

public class MyClass2 : IMyInterface
{
    public void MyMethod(object arg)
    {
        byte[] obj = (byte[]) arg;
        // do something with obj...
    }
}

The problem with MyClass2 is that the conversion of byte[] to and from object is boxing and unboxing , which are computationally expensive operations affecting performance. MyClass2的问题在于, byte[]object的转换是装箱和拆箱 ,这是影响性能的计算上昂贵的操作。

Would solving this problem with a generic interface avoid boxing/unboxing? 通用接口解决这个问题会避免装箱/拆箱吗?

public interface IMyInterface<T>
{
    void MyMethod(T arg);
}

public class MyClass1 : IMyInterface<MyObject>
{
    public void MyMethod(MyObject arg)
    {
        // typecast no longer necessary
        //MyObject obj = (MyObject) arg;
        // do something with arg...
    }
}

public class MyClass2 : IMyInterface<byte[]>
{
    public void MyMethod(byte[] arg)
    {
        // typecast no longer necessary
        //byte[] obj = (byte[]) arg;
        // do something with arg...
    }
}

How is this implemented in .NET vs Mono? 这是如何在.NET vs Mono中实现的? Will there be any performance implications on either platform? 这两个平台都会有任何性能影响吗?

Thank you! 谢谢!

You will get the same benefits in Mono that you do in .NET. 您将在.NET中获得与Mono相同的好处。

We strongly recommend that you use Mono 1.9 or Mono 2.0 RCx in general, as generics support only matured with 1.9. 我们强烈建议您一般使用Mono 1.9或Mono 2.0 RCx,因为泛型支持仅适用于1.9。

The problem with MyClass2 is that the conversion of byte[] to and from object is boxing and unboxing, which are computationally expensive operations affecting performance. MyClass2的问题在于,byte []与对象的转换是装箱和拆箱,这是影响性能的计算上昂贵的操作。

There is no boxing involved with array types, even one with value type elements. 数组类型不涉及装箱,即使是具有值类型元素的装箱也是如此。 An array is a reference type. 数组是引用类型。

The overhead on (byte[]) arg is minimal at best. (byte [])arg的开销最多是最小的。

I'm not sure how it is implemented in mono, but generic interfaces will help because the compiler creates a new function of the specific type for each different type used (internally, there are a few cases where it can utilize the same generated function). 我不确定它是如何在单声道中实现的,但是通用接口将有所帮助,因为编译器为每个使用的不同类型创建特定类型的新函数(在内部,在一些情况下,它可以使用相同的生成函数) 。 If a function of the specific type is generated, there is no need to box/unbox the type. 如果生成特定类型的函数,则无需对该类型进行装箱/取消装箱。

This is why the Collections.Generic library was a big hit at .NET 2.0 because collections no longer required boxing and became significantly more efficient. 这就是Collections.Generic库在.NET 2.0中受到重创的原因,因为集合不再需要装箱而且效率明显提高。

Yes, in .Net (MS not sure about mono) generics are implemented at compile time so there is no boxing or unboxing going on at all. 是的,在.Net(MS不确定单声道)泛型是在编译时实现的,因此根本没有装箱或拆箱。 Contrast to java generics which are syntactic sugar that just perform the casts for you in the background (at least it was this way once). 与java泛型相比,这是一种语法糖,它只是在后台为你执行演员表(至少它是这样的一次)。 The main problem with generics is you can't treat generic containers polymorphically, but that is a bit off your topic :-) 泛型的主要问题是你不能多态地处理通用容器,但这有点偏离你的主题:-)

我无法与Mono交谈,但使用通用接口解决MS运行时中的装箱/取消装箱问题。

Given you're using a recent version of mono, 2.0 if you can. 鉴于您正在使用最新版本的mono,如果可以,则使用2.0。

Generic interface performance on Mono is very good, on pair with regular interface dispatch. Mono上的通用接口性能非常好,与常规接口调度配对。

Dispatch of generic virtual methods[1] is terrible on all released versions of mono, it has improved in 1.9 thou. 调度通用虚拟方法[1]在所有已发布的单声道版本上都很糟糕,它在1.9中得到了改进。

The problem is not that bad as the performance issue with generic virtual methods has been fixed for the next release of mono (2.2), which is scheduled to the end of this year. 问题并不是那么糟糕,因为通用虚拟方法的性能问题已经针对下一版本的mono(2.2)进行了修复,该版本计划在今年年底完成。

[1] A generic virtual method is something like: [1]通用虚拟方法类似于:

public interface Foo { 公共接口Foo {

  void Bla<T> (T a, T b);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM