IL2CPP vs C++ Performance

Hello,

I’m sorry if this question is already discussed, couldn’t find anything.

Ok, here is the thing…I build my games using IL2CPP and pretty happy with this in spite of troubles which popup from time to time.

In one of these games - Match3 - I have logic which is looking for combinations and other stuff which is basically algorithms/calcs and so on. You may probably think that this is not a big deal and that’s not very performance depending code. But when you connect AI which tests/plays this logic and call it thousands times during calculation of better move then it (logic) is getting extremely performance depending.

I’m not gonna discuss all my algorithms here because of this is out of my question. Let’s just assume that all these algorithms were optimized as much as possible using C#.

The question is: if I move some of my logic, like iterations through arrays and etc to native c++ plugin will it boost my performance comparing to IL2CPP?
I know there’s no 100% guarantee until you see source code but hypothetically… let’s just assume we are talking about huge iteration through array.

I’ve seen some articles where people do benchmarks of native plugin in Unity Editor which is ridiculous imho. Of course array initialization and iteration will be faster in native part comparing to editor’s mono with all this managed things.
Also (as far as I know) IL2CPP generates native code for us which is better for performance but it actually emulate managed environment with its own garbage collector and stuff.
So here’s question: will my native c++ plugin with marshalling (btw what happens to marshalling when I build using IL2CPP) be faster comparing to c++ generated by IL2CPP.

sry for long question, Thanks!

I can’t give you a definitive answer about performance, but I can provide a bit of insight that might help.

IL2CPP generates native code for us which is better for performance but it actually emulate managed environment with its own garbage collector and stuff

It is correct that IL2CPP is running a .NET VM, which includes GC, thread management, etc. So anytime your code uses features of the .NET VM, you will pay the same price on IL2CPP and Mono.

IL2CPP really helps with performance of floating point operates, as Mono is configured to do all floating point calculations on double types, even for float types. IL2CPP doesn’t have this restriction, so performs much better.

what happens to marshalling when I build using IL2CPP

IL2CPP must follow all of the same marshaling rules as any .NET virtual machine. If the code is marshaling blittable types, then you won’t pay much overhead for the native to managed transition. With IL2CPP, you can inspect the generated code to see what is happening with marshaling to get some idea of the performance cost.

Generally, I’d recommend trying some isolated case that is cheap to implement in a native library, then profile to see if it is beneficial.