IL2CPP exception optimization
Is there a significant performance cost to exceptions when using the IL2CPP backend on Android or other platforms? I'm debating using failure return results or raising exceptions for incorrect function use.
Answer by JoshPeterson · Aug 22, 2019 at 12:13 PM
In general exceptions are expensive. I would not recommend using exceptions to indicate normal flow control of a program or to indicate errors that are expected to happen and can be handled. Exceptions should usually only be used for uncommon cases.
Exceptions have cost in two ways:
Binary size cost: Even if exceptions are not used, the final binary my contain data to look up exceptions and handle stack unwinding. You will always pay this price for Unity IL2CPP builds, since .NET requires that exceptions always exist.
Run time performance cost: If an exception is not thrown (even if it could be thrown) there is almost no cost in run time performance. When an exception is thrown, there is a very high cost in run time performance.
As always, I'd recommend profiling your specific code and use case to determine the best code organization/performance tradeoff.
Your answer
Follow this Question
Related Questions
icl2pp not building correctly for android.,IL2cpp not building correctly for android. Mono worked. 1 Answer
I am getting compile error with il2cpp for android (Unity 5.2.2p3) 3 Answers
Failed running il2cpp.exe --platform="Android" --architecture="x86" 1 Answer
black screen and froze UI in android AR app with scripting backend il2cpp build 0 Answers