IL2CPP Internals:

Il2CPP Reverse:

Tutorial:

Adventures:

Honkai Impact:

What is shared by IL2CPP?

Currently, IL2CPP shares generic method implementations for a generic type SomeGenericType<t> when T is:

  • Any reference type (e.g. string, object, or any user defined class)
  • Any integer or enum type

IL2CPP does not share generic method implementations when T is a value type because the size of each value type will differ (based on the size of its fields).

Practically, this means that adding a new usage of SomeGenericType, where T is a reference type will have a minimal impact on the executable size.However, if T is a value type, the executable size will be impacted. This behavior is the same for both the Mono and IL2CPP scripting backends. If you want to know more, read on, it’s time to dig into some implementation details!

The setup

I’ll be using Unity 5.0.2p1 on Windows, and building for the WebGL platform. I’ve enabled the “Development Player” option in the build settings,and the “Enable Exceptions” option is set to a value of “None”. The script code for this post starts with a driver method to create instances of the generic types we will investigate:

                public void DemonstrateGenericSharing()
{
var usesAString = new GenericType();
var usesAClass = new GenericType();
var usesAValueType = new GenericType();
var interfaceConstrainedType = new InterfaceConstrainedGenericType();
}               
            

Next, we define the types used in this method:

                
class GenericType< T > {
public T UsesGenericParameter(T value) {
return value;
}

public void DoesNotUseGenericParameter() {}

public U UsesDifferentGenericParameter< U >(U value) {
return value;
}
}

class AnyClass {}

interface AnswerFinderInterface {
int ComputeAnswer();
}

class ExperimentWithInterface : AnswerFinderInterface {
public int ComputeAnswer() {
return 42;
}
}

class InterfaceConstrainedGenericType< T > where T : AnswerFinderInterface {
public int FindTheAnswer(T experiment) {
return experiment.ComputeAnswer();
}
}

And all of code is nested in a class named HelloWorld derived from MonoBehaviour.

If you view the command line for il2cpp.exe, note that it does not contain the --enable-generic-sharing option, as described in the first post in this series.However, generic sharing is still occurring. It is no longer optional, and happens in all cases now.