Do generic interfaces provide in C # boxing? (.NET Performance vs. Mono)

I have a C # interface with specific method parameters declared as object types. However, the actual type that passes may vary depending on the class that implements 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 converting byte[] to and from object : boxing and unpacking , which are costly operations that affect performance.

Would you be able to solve this problem with a common interface to 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? Will there be any performance implications on both platforms?

Thanks!

+4
source share
6 answers

I'm not sure how this is implemented in mono, but the common interfaces will help, because the compiler creates a new function of a certain type for each type used (inside, there are several cases where it can use the same generated function). If a function of a certain type is generated, there is no need to insert / delete a type.

This is why the Collections.Generic library was a big hit in .NET 2.0 because the collections no longer required boxing and became significantly more efficient.

+8
source

You will get the same benefits in Mono as in .NET.

We strongly recommend using Mono 1.9 or Mono 2.0 RCx in general, since generic support has only matured since 1.9.

+14
source

The problem with MyClass2 is that converting byte [] to and from an object is boxing and unpacking, which are costly operations that affect performance.

There is no box associated with array types, even with value type elements. An array is a reference type.

The overhead for (byte []) arg is minimal at best.

+11
source

Yes, in .Net (MS is not sure about mono) generics are implemented at compile time, so no boxing or unpacking occurs at all. Contrast with java generics, which are syntactic sugar that just throws for you in the background (at least that was once). The main problem with generics is that you cannot handle generic containers polymorphically, but this is slightly different from your theme :-)

+2
source

I can not talk with Mono, but using a common interface should solve the problem of boxing / unpacking in the MS runtime.

+1
source

Given that you are using the latest mono, 2.0 if you can.

The overall performance of the interface on Mono is very good, coupled with the usual scheduling of the interface.

Sending common virtual methods [1] is terrible in all released mono versions; it improved by 1.9 thousand.

The problem is not so bad, because the performance problem with universal virtual methods has been fixed for the next version of mono (2.2), which is scheduled for the end of this year.

[1] A common virtual method is something like:

open interface foo {

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

}

0
source

Source: https://habr.com/ru/post/1277221/


All Articles